关于这个问题,我已经浏览了很多关于 SO 的帖子:尝试了Here、 Here和Here中的所有内容,没有任何效果。每次位置都是空的。在设备和模拟器上:(。我的 GPS 已打开,互联网已打开,我的清单具有以下权限:
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
我使用以下代码来获取位置,即设备的经度和纬度......但是在每种情况下我都location
得到null
public void find_Location() {
Log.d("Find Location", "in find_location");
String location_context = Context.LOCATION_SERVICE;
locationManager = (LocationManager) getApplicationContext()
.getSystemService(location_context);
List<String> providers = locationManager.getProviders(true);
for (String provider : providers) {
locationManager.requestLocationUpdates(provider, 1000, 0,
new LocationListener() {
public void onLocationChanged(Location location) {}
public void onProviderDisabled(String provider) {
}
public void onProviderEnabled(String provider) {
}
public void onStatusChanged(String provider,
int status, Bundle extras) {
}
});
Location location = locationManager.getLastKnownLocation(provider);
if (location == null) {
Toast.makeText(LbsGeocodingActivity.this, "Location Not found",
Toast.LENGTH_LONG).show();
} else {
double latitude = location.getLatitude();
double longitude = location.getLongitude();
Toast.makeText(LbsGeocodingActivity.this,
"LAT..." + latitude + "\nLONG..." + longitude,
Toast.LENGTH_LONG).show();
// addr=ConvertPointToLocation(latitude,longitude);
// String temp_c=SendToUrl(addr);
}
}
}