我想通过最好的提供商从gps / netwrok获取位置,但总是返回相同的位置,我也可以看到谷歌地图上有一个标志到我的正确位置。 请任何人都可以告诉我我做错了什么?
我的活动是:
public class MainMenu2 extends Activity implements LocationListener, OnClickListener
在我的onCreate上我有:
if(isGooglePlay()){
setContentView(R.layout.menu_2);
setUpMapIfNeeded();
}
是Google Play方法:
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!"));
}
我也试图在这里搜索similer问题,但我没有得到任何东西..
答案 0 :(得分:9)
在像Samsung,自定义Android版本等平台上,LocationProvider存在一个错误。您需要调用此代码(实际上什么都不做),这会触发手机的后台缓存以更新其当前位置。
如果您不相信我,请将您的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) {
}
});
编辑
OR
使用新的api的LocationClient而不是LocationProvider。