我用獲取當前位置驗證碼:我怎樣才能在Android當前的位置?
LocationManager lm = (LocationManager) this
.getSystemService(Context.LOCATION_SERVICE);
List<String> providers = lm.getProviders(true);
/*
* Loop over the array backwards, and if you get an accurate location,
* then break out the loop
*/
Location l = null;
for (int i = providers.size() - 1; i >= 0; i--) {
l = lm.getLastKnownLocation(providers.get(i));
if (l != null)
break;
}
if (l==null) {
return;
}
Toast.makeText(this, String.valueOf(l.getLatitude()), Toast.LENGTH_LONG).show();
但我從來沒有看到一個緯度,因爲l爲null始終。我的Manifest文件包含ACCESS_FINE_LOCATION和INTERNET權限。我犯了什麼錯誤?
請參閱http://developer.android.com/guide/topics/location/obtaining-user-location.html。您需要添加位置偵聽器並請求更新。 – blessenm
爲什麼你使用這段代碼?如果不necesory使用它然後使用此:http://stackoverflow.com/questions/1513485/how-do-i-get-the-current-gps-location-programmatically-in-android和這裏是指開發者網站:HTTP://developer.android.com/reference/android/location/LocationManager.html –
這可能會爲你工作,我猜: http://stackoverflow.com/a/3145655/265167 –