【问题标题】:How to get the GPS coordinates in a method如何在方法中获取 GPS 坐标
【发布时间】:2012-06-13 18:50:26
【问题描述】:

我有一个注册监听器并请求位置更新的方法。我想在当前方法本身中定位坐标,而不是在 onLocationChanged 方法中。

我的代码是:-

myclass {

   onCreate {
      location_Manager = (LocationManager) getSystemService (Context.LOCATION_SERVICE);
      locLstnr = new myLocationlistener();
      locMgr.requestLocationUpdates(LocationManager.GPS_PROVIDER, Loc_listener.MINIMUM_TIME_BETWEEN_UPDATES, Loc_listener.MINIMUM_DISTANCE_CHANGE_FOR_UPDATES, locLstnr);
   }

   class myLocationListener () {
      onLocationChanged() { ... }
      onProviderDisabled() { ... }
      onProviderEnabled() { ... }
   }

}

我知道我可以得到 getLastKnownLocation() ,但是如果它不可用,难道根本就不能得到位置吗? Thisthis 是类似的问题,但对我没有帮助。使用 onLocationChanged 方法没有问题,但我需要在 onCreate 方法中有位置。那可能吗?一种方法可能是将位置存储到 myLocationListener 中的变量中并访问它,但是否有替代方案或更好的解决方案?

【问题讨论】:

    标签: android gps


    【解决方案1】:

    我认为如果 getLastKnownLocation() 给出 null 那么你必须等待回调......

    只要能做就可以请求更新

    locationManager.requestLocationUpdates(locationProvider, 0, 0, locationListener); 
    

    http://devdiscoveries.wordpress.com/2010/02/04/android-use-location-services/

    来自link

    getLastKnownLocation() is faster after a connection has been established with the GPS satellite. For the first time, it will return null, or no value till no connection is established. You can add a GpsListener to know when the location is obtained. Search about "how to get a gps fix" and you might get answer to your question
    

    并参考link

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-10-11
      相关资源
      最近更新 更多