Android 如何在一种方法中获取GPS坐标
我有一个注册侦听器并请求位置更新的方法。我希望在当前方法本身中使用位置坐标,而不是在onLocationChanged方法中使用位置坐标 我的代码是:-Android 如何在一种方法中获取GPS坐标,android,gps,Android,Gps,我有一个注册侦听器并请求位置更新的方法。我希望在当前方法本身中使用位置坐标,而不是在onLocationChanged方法中使用位置坐标 我的代码是:- myclass { onCreate { location_Manager = (LocationManager) getSystemService (Context.LOCATION_SERVICE); locLstnr = new myLocationlistener(); locMgr.requ
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(),但如果它不可用,是否可以获取位置?这是一个类似的问题,但对我没有帮助。我使用onLocationChanged方法没有问题,但是我需要在onCreate方法中有位置。可能吗?一种方法可能是将位置存储到myLocationListener中的一个变量上并访问它,但是否有替代方案或更好的解决方案?我认为如果getLastKnownLocation()给出null,则必须等待回调 只要能做就可以提出更新请求
locationManager.requestLocationUpdates(locationProvider, 0, 0, locationListener);
从
及转介
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