如何获取用户的 GPS 位置并在每次位置发生变化时实现文本视图?

发布于 2024-12-06 04:54:57 字数 1539 浏览 0 评论 0原文

我正在尝试做一个简单的活动,读取用户的 GPS 位置,并在每次用户位置发生变化时实现一个简单的文本视图。

我在谷歌上找到了一些例子,但它们都不是很好的例子,因为只捕获用户一次的位置,并且我需要每次位置随着用户的新纬度和经度发生变化时,文本视图都会被实际化。

我尝试做一个线程,但它失败了,我认为没有必要做一个线程,我的方式是错误的。

欢迎代码示例

编辑:我正在添加用户 NickT 提出的解决方案。 这个解决方案失败了。我不知道为什么,但只实现了文本视图的两倍,其中前两个 GPS 值是我用 DDMS 传递给模拟器的......在此之后,thextview 没有被实现更多次...为什么?我在 onLocationChanged 中设置了一个断点,并且只有在我发送 GPS 位置的前两次才被调用......但再也不会了。怎么了?

public class GpsMiniActivity extends Activity implements LocationListener{

private LocationManager mLocMgr;
private TextView tv1;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    FrameLayout rl = new FrameLayout(this.getApplicationContext());
    LinearLayout ll= new LinearLayout(this.getApplicationContext());
    ll.setOrientation(LinearLayout.VERTICAL);

    setContentView(rl);
    rl.addView(ll);

    tv1=new TextView(getApplicationContext());
    ll.addView(tv1);

    //setContentView(R.layout.main);
    mLocMgr = (LocationManager) getSystemService(LOCATION_SERVICE);
    mLocMgr.requestLocationUpdates(LocationManager.GPS_PROVIDER,
            500, 0, this);
}
@Override
public void onLocationChanged(Location location) {
    tv1.setText("Lat " +   location.getLatitude() + " Long " + location.getLongitude());
}

@Override
public void onProviderDisabled(String provider) {}

@Override
public void onProviderEnabled(String provider) {}

@Override
public void onStatusChanged(String provider, int status, Bundle extras) {}

}

I'm trying to make a simple activity that reads the GPS position of the user and actualices a simple textview everytime the position of the user changes.

I find some examples en google but all of them are not nice examples, because only capture the position of the user ONE time, and i need that the textview get's actualiced everytime the position changes with the new latitude and longitude of the user.

I tryed to do a thread but it fails and i think it is not necesary to do a thread, im in the wrong way.

Code examples are welcome

EDIT: i'm adding the solution proposed by the user NickT. This solution fails. I dont know why but only actualizes two times the textview, with the two first GPS values that i pass to the emulator with DDMS.... after this the thextview isn't getting actualiced more times... ¿why?. I make a breakpoint in onLocationChanged, and it only get's called the first two times i send a gps positiones... but never more. ¿what is happening?

public class GpsMiniActivity extends Activity implements LocationListener{

private LocationManager mLocMgr;
private TextView tv1;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    FrameLayout rl = new FrameLayout(this.getApplicationContext());
    LinearLayout ll= new LinearLayout(this.getApplicationContext());
    ll.setOrientation(LinearLayout.VERTICAL);

    setContentView(rl);
    rl.addView(ll);

    tv1=new TextView(getApplicationContext());
    ll.addView(tv1);

    //setContentView(R.layout.main);
    mLocMgr = (LocationManager) getSystemService(LOCATION_SERVICE);
    mLocMgr.requestLocationUpdates(LocationManager.GPS_PROVIDER,
            500, 0, this);
}
@Override
public void onLocationChanged(Location location) {
    tv1.setText("Lat " +   location.getLatitude() + " Long " + location.getLongitude());
}

@Override
public void onProviderDisabled(String provider) {}

@Override
public void onProviderEnabled(String provider) {}

@Override
public void onStatusChanged(String provider, int status, Bundle extras) {}

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

素年丶 2024-12-13 04:54:57

绝对最小示例:

public class GpsMiniActivity extends Activity implements LocationListener{

    private LocationManager mLocMgr;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        mLocMgr = (LocationManager) getSystemService(LOCATION_SERVICE);
        mLocMgr.requestLocationUpdates(LocationManager.GPS_PROVIDER,
                500, 0, this);
    }
    @Override
    public void onLocationChanged(Location location) {
        TextView tv = (TextView) findViewById(R.id.tv1);
        tv.setText("Lat " +   location.getLatitude() + " Long " + location.getLongitude());
    }

    @Override
    public void onProviderDisabled(String provider) {}

    @Override
    public void onProviderEnabled(String provider) {}

    @Override
    public void onStatusChanged(String provider, int status, Bundle extras) {}

}

您还需要清单中的 ACCESS_FINE_LOCATION 权限和 main.xml 中的 Textview id tv1

Absolute minimum example:

public class GpsMiniActivity extends Activity implements LocationListener{

    private LocationManager mLocMgr;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        mLocMgr = (LocationManager) getSystemService(LOCATION_SERVICE);
        mLocMgr.requestLocationUpdates(LocationManager.GPS_PROVIDER,
                500, 0, this);
    }
    @Override
    public void onLocationChanged(Location location) {
        TextView tv = (TextView) findViewById(R.id.tv1);
        tv.setText("Lat " +   location.getLatitude() + " Long " + location.getLongitude());
    }

    @Override
    public void onProviderDisabled(String provider) {}

    @Override
    public void onProviderEnabled(String provider) {}

    @Override
    public void onStatusChanged(String provider, int status, Bundle extras) {}

}

You'll also need permission ACCESS_FINE_LOCATION in your manifest and a Textview id tv1 in main.xml

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文