Android getLastKnownLocation()始终返回相同的位置
我正试图通过最佳提供商从gps/netwrok获取位置,但它总是返回相同的位置,而且我可以看到在谷歌地图上有一个指向我正确位置的标志。 谁能告诉我我做错了什么 我的活动是:Android getLastKnownLocation()始终返回相同的位置,android,android-location,Android,Android Location,我正试图通过最佳提供商从gps/netwrok获取位置,但它总是返回相同的位置,而且我可以看到在谷歌地图上有一个指向我正确位置的标志。 谁能告诉我我做错了什么 我的活动是: public class MainMenu2 extends Activity implements LocationListener, OnClickListener 在onCreate上,我有: if(isGooglePlay()){ setContentView(R.layout.menu_2);
public class MainMenu2 extends Activity implements LocationListener, OnClickListener
在onCreate上,我有:
if(isGooglePlay()){
setContentView(R.layout.menu_2);
setUpMapIfNeeded();
}
iGoogle播放方法:
private boolean isGooglePlay() { // looks if googlemaps is available
int status = GooglePlayServicesUtil.isGooglePlayServicesAvailable(this);
if(status == ConnectionResult.SUCCESS){
return true;
}
else{
((Dialog) GooglePlayServicesUtil.getErrorDialog(status, this, 10)).show();
//Toast.makeText(this, "Google Play is not available", Toast.LENGTH_SHORT).show();
return(false);
}
}//isGooglePlay
SetupMapIfNeed方法:
private void setUpMapIfNeeded() {
// Do a null check to confirm that we have not already instantiated the map.
if (mMap == null) {
mMap = ((MapFragment) getFragmentManager().findFragmentById(R.id.menu_map))
.getMap();
// Check if we were successful in obtaining the map.
if (mMap != null) {
// The Map is verified. It is now safe to manipulate the map.
mMap.setMyLocationEnabled(true);
locationManager = (LocationManager) this.getSystemService(LOCATION_SERVICE);
Criteria criteria = new Criteria();
criteria.setAccuracy(ACCURACY_FINE);
provider = locationManager.getBestProvider(criteria, true);
if (provider == null){
onProviderDisabled(provider);
}
locationManager.requestLocationUpdates(provider, 100, 1, this);
loc = locationManager.getLastKnownLocation(provider);
//---here the privider is "gps" but always the same location---//
if (loc != null){
onLocationChanged(loc);
}
mMap.setMapType(GoogleMap.MAP_TYPE_NORMAL); // set Map Type
}
}
}//setUpMap
和覆盖方法:
@Override
public void onLocationChanged(Location location) {
// TODO Auto-generated method stub
LatLng latlng = new LatLng(location.getLatitude(),location.getLongitude());
mMap.moveCamera(CameraUpdateFactory.newLatLng(latlng));
mMap.animateCamera(CameraUpdateFactory.zoomTo(10));
double pLong = location.getLongitude();
double pLat = location.getLatitude();
Latstr = String.valueOf(pLong);
Lngstr = String.valueOf(pLat);
latitude = pLat;
longitude = pLong;
}
@Override
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
Toast.makeText(this, "Please turn ON the GPS", Toast.LENGTH_SHORT).show();
}
@Override
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub
}
@Override
protected void onPause() {
super.onPause();
locationManager.removeUpdates(this);
}
@Override
protected void onResume() {
super.onResume();
myLatLng = new LatLng(latitude,longitude);
CameraUpdate update = CameraUpdateFactory.newLatLngZoom(myLatLng, 15);
mMap.animateCamera(update);
mMap.addMarker(new MarkerOptions().position(new LatLng(latitude,longitude )).title("Find me here!"));
}
我也试着在这里搜索类似的问题,但我什么也没有得到。在一些平台上,比如三星,定制Android版本,LocationProvider有一个bug。您需要调用此代码(实际上什么也不做),它会触发手机的后台缓存以更新其当前位置 如果你不相信我,将你的Gps位置更改到200米以外的其他位置,加载你的应用程序,你不会注意到Gps位置的更改。现在,只需在手机上加载地图应用程序,你的应用程序就会突然显示当前位置 要执行此操作,请以编程方式将这些行放在
getLastKnownLocation
调用之前
HomeScreen.getLocationManager().requestLocationUpdates(
LocationManager.NETWORK_PROVIDER, 0, 0, new LocationListener() {
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
@Override
public void onProviderEnabled(String provider) {
}
@Override
public void onProviderDisabled(String provider) {
}
@Override
public void onLocationChanged(final Location location) {
}
});
编辑
或
使用新api的LocationClient,而不是LocationProvider。非常感谢!!几天后你帮了我很多忙。我猜“HomeScreen.getLocationManager()”是指我的Locationmanager是吗?是的。我是说位置经理。这是一只很难捕捉的大臭虫。我花了两周的时间才弄明白,因为为了测试,我必须出去至少走200米:)三星对这个错误没有反应。像我们这样的开发者没有地方向三星发布bug。如果你在谷歌上发布,很明显他们会关闭它。哈哈……也许他们有自己的兴趣……我不知道,但我很高兴我们有像你这样的人分享所有这些信息!您正在混合getLastLocation的新api和旧api,请遵循以下步骤