如何自动更新纬度和经度的TextView?

时间:2018-01-03 05:28:41

标签: java android

当我的位置发生变化时,如何自动更新纬度和经度的TextView

我构建了一个应用程序,每隔x次在Mysql BD中保存我的位置,只有在位置发生变化时才需要我的TextView更新。

下面我查看我的应用程序的预览图像

纬度和经度的TextView

    public void onLocationChanged(Location location) {
    TextView longitude =  findViewById(R.id.t_longitude);
    TextView latitude =  findViewById(R.id.t_latitude);

    longitude.setText("Longitude: " + location.getLongitude());
    latitude.setText("Latitude: " + location.getLatitude());

 }

这是我的预览

2 个答案:

答案 0 :(得分:0)

您必须实施LocationListener才能使用onLocationChanged

的方法
  

每当用户访问时,LocationListener都会返回新的纬度和经度   位置变化。

首先实施LocationListener然后覆盖它的方法onLocationChanged as,

@Override
public void onLocationChanged(Location newLocation) {

      // Here's "newLocation" is the change in location of user.   
      // Do here what you want to do on change in location of user.     

}

有关详细信息,请查看此https://github.com/adityasonel/AndroidLocationChecker/blob/master/app/src/main/java/com/adityasonel/androidlocationchecker/FusedLocationProviderApiTracker.java

答案 1 :(得分:-1)

有很多方法可以完成它

  • 回调接口
  • 计时器线程
  • 广播
  • 活动巴士

最好的方法是使用接口或事件总线进行回叫,在活动中实现接口并提供对位置管理器类的引用。一旦位置改变了调用接口方法就通知该类。

相关问题