我正在编写代码以便能够使用本地化(如果可用,则基于 GPS,否则基于网络)。如果我在手机上打开 GPS 进入我的活动,一切正常。但是,如果我在关闭 GPS 的情况下进入活动,然后在活动中启用 GPS,则没有任何反应,并且基于网络的位置仍然处于活动状态,而不是切换到基于 GPS。
我错过了什么 ?谢谢
public class MapActivity extends Activity implements LocationListener {
//Used CameraPosition attributes
private final int MAP_BEARING = 69;
private final int SETUP_ZOOM = 16;
private final int AT_POSITION_ZOOM = 19;
private final int UPDATE_FREQUENCY = 20000; //Update frequency (ms)
private final LatLng MAP_CENTER = new LatLng(44.80736, -0.596572);
private CheckBox mServices, mRestauration, mBuildings;
private Resources mResources;
private GoogleMap mMap;
private LocationManager mLocationManager;
private Marker mCurrentLocation;
private ArrayList<Marker> mServicesMarkers, mRestaurationMarkers, mBuildingsMarkers;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_map);
mResources = getResources();
mServices = (CheckBox) findViewById(R.id.services_check);
mRestauration = (CheckBox) findViewById(R.id.restauration_check);
mBuildings = (CheckBox) findViewById(R.id.buildings_check);
setUpLocationServices();
}
public void setUpLocationServices() {
mLocationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
Criteria criteria = new Criteria();
criteria.setAccuracy(Criteria.ACCURACY_FINE);
String provider = mLocationManager.getBestProvider(criteria, true);
Location location = mLocationManager.getLastKnownLocation(provider);
double latitude = location.getLatitude();
double longitude = location.getLongitude();
LatLng currentPosition = new LatLng(latitude, longitude);
mCurrentLocation = mMap.addMarker(new MarkerOptions()
.position(currentPosition)
.title("Votre position")
.icon(BitmapDescriptorFactory.fromResource(R.drawable.user_location_marker)));
if (location != null)
onLocationChanged(location);
if (isGpsEnabled())
mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);
else
mLocationManager.requestLocationUpdates(provider, UPDATE_FREQUENCY, 0, this);
}
@Override
public void onLocationChanged(Location location) {
mCurrentLocation.remove();
LatLng currentPosition = new LatLng(location.getLatitude(), location.getLongitude());
mCurrentLocation = mMap.addMarker(new MarkerOptions()
.position(currentPosition)
.title("Votre position")
.icon(BitmapDescriptorFactory.fromResource(R.drawable.user_location_marker)));
}
public boolean isGpsEnabled() {
LocationManager service = (LocationManager) getSystemService(LOCATION_SERVICE);
return service.isProviderEnabled(LocationManager.GPS_PROVIDER);
}
@Override
public void onProviderDisabled(String provider) {
mLocationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
if (isGpsEnabled())
mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);
else
mLocationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, UPDATE_FREQUENCY, 0, this);
}
@Override
public void onProviderEnabled(String provider) {
mLocationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
if (isGpsEnabled())
mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);
else
mLocationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, UPDATE_FREQUENCY, 0, this);
}
@Override
public void onStatusChanged(String arg0, int arg1, Bundle arg2) {
// TODO Auto-generated method stub
}
}