如何在方法中获取GPS坐标

时间:2012-06-13 10:39:18

标签: android gps

我有一个注册侦听器并请求位置更新的方法。我想在当前方法本身的位置坐标,而不是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中的变量并访问它,但是是否有替代或更好的解决方案?

1 个答案:

答案 0 :(得分: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