【发布时间】:2013-06-10 21:34:16
【问题描述】:
我想获取移动当前位置,当我安装应用程序时,比第一次得到正确的纬度,但比每次显示第一次获取位置后。请给我正确的代码,以获得每次正确的纬度。 _ 在排序中我的位置未更新,或位置缓存不清楚。 ->我的代码
公共类 MyLocationListener 实现 LocationListener {
private LocationManager mLocationManager;
private Location currentLocation;
public MyLocationListener(Context context) {
mLocationManager = (LocationManager) context
.getSystemService(Context.LOCATION_SERVICE);
currentLocation = mLocationManager
.getLastKnownLocation(LocationManager.GPS_PROVIDER);
mLocationManager.requestLocationUpdates(
LocationManager.GPS_PROVIDER, 0, 0, this);
}
public Location getBestLocationAvailable() {
return currentLocation;
}
@Override
public void onLocationChanged(Location newBestLocation) {
mLocationManager.removeUpdates(this);
currentLocation = newBestLocation;
}
@Override
public void nProviderDisabled(String arg0) {
}
@Override
public void onProviderEnabled(String arg0) {
}
@Override
public void onStatusChanged(String arg0, int arg1, Bundle arg2) {
}
}
MyLocationListener loc = new MyLocationListener(MainActivity.this);
位置 location = loc.getBestLocationAvailable(); system.out.println(location.getLatitude()+" "+location.getLongitude);
给我写答案,为什么我会老拉长?
【问题讨论】:
-
可能位置未更新。您确定调用了 onLcoationChanged 方法吗?