gpt4 book ai didi

android - getLastKnownLocation() 总是返回相同的位置

转载 作者:塔克拉玛干 更新时间:2023-11-02 09:05:25 27 4
gpt4 key购买 nike

我正在尝试通过最佳提供商从 gps/netwrok 获取位置,但它总是返回相同的位置,而且我可以在谷歌地图上看到我正确位置的标志。谁能告诉我我做错了什么?

我的 Activity 是:

 public class MainMenu2 extends Activity implements LocationListener, OnClickListener

在我的 onCreate 上我有:

 if(isGooglePlay()){
setContentView(R.layout.menu_2);
setUpMapIfNeeded();
}

isGooglePlay 方法:

 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

setUpMapIfNeeded 方法:

   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!"));


}

我也试着在这里搜索类似的问题,但我一无所获..

最佳答案

LocationProvider 在三星、自定义 Android 构建等某些平台上存在错误。您需要调用此代码(实际上什么都不做),它会触发手机的后台缓存以更新其当前位置。

如果您不相信我,请将您的 Gps 位置更改为大约 200 米外的其他位置,加载您的应用程序,您不会注意到位置 gps 发生变化。现在,只需在您的手机上加载 map 应用程序,您的应用程序就会突然显示当前位置。

要以编程方式执行此触发器,请将这些行放在您的 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。

关于android - getLastKnownLocation() 总是返回相同的位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17250968/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com