使用此代碼Id它工作。
private LocationManager locationManager;
private Location location;
private boolean hasGpsProvider, hasNetwrokProvider;
public Location getLocation(Context mContext) {
if (locationManager == null) {
locationManager = (LocationManager) mContext
.getSystemService(Context.LOCATION_SERVICE);
}
hasGpsProvider = locationManager
.isProviderEnabled(LocationManager.GPS_PROVIDER);
hasNetwrokProvider = locationManager
.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
if (hasGpsProvider) {
locationManager.requestLocationUpdates(
LocationManager.GPS_PROVIDER, 0, 100, locationListenerGps);
location = locationManager
.getLastKnownLocation(LocationManager.GPS_PROVIDER);
return location;
}
if (hasNetwrokProvider) {
locationManager.requestLocationUpdates(
LocationManager.NETWORK_PROVIDER, 0, 100,
locationListenerNetwork);
location = locationManager
.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
return location;
}
return location;
}
並調用
if (location != null) {
lat = location.getLatitude();
lng = location.getLongitude();
}
正在使用設備或模擬器? – Jitendra 2013-02-22 11:36:35
我正在使用Nexus 4 – 2013-02-22 11:44:32