下面粘贴的是我从用户那里获取坐标的课程。但有时,我将纬度和经度设为空。我知道这是因为我没有足够的网络,或者我在建筑物内,或者其他什么......但奇怪的是,在那一刻,我去谷歌地图或其他地图应用程序,并且“地理定位我”按钮完美运行。
我错过了什么别人没有?
public class LocationActivity extends BaseActivity {
private LocationManager locationManager = null;
LocationListener onLocationChange=new LocationListener()
{
public void onLocationChanged(Location loc) {
LocalSession.lat = loc.getLatitude();
LocalSession.lng = loc.getLongitude();
}
public void onProviderDisabled(String provider) {}
public void onProviderEnabled(String provider) {}
public void onStatusChanged(String provider, int status, Bundle extras) {}
};
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
}
@Override
public void onPause() {
super.onPause();
locationManager.removeUpdates(onLocationChange);
}
@Override
public void onDestroy() {
super.onDestroy();
locationManager.removeUpdates(onLocationChange);
}
@Override
public void onResume() {
super.onResume();
try {
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,0,10000.0f,onLocationChange);
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,0,10000.0f,onLocationChange);
} catch (Exception e) {
}
}
}