-2
我正在嘗試訪問用戶設備的GPS位置,似乎無處可去。無法獲取GPS位置
在我的課程中,我有以下代碼,它會檢查GPS提供程序是否已啓用,然後嘗試獲取用戶的最後已知位置。
它認識到GPS提供商是enabled
,但是當我嘗試獲取最後一個已知位置時,我什麼也沒得到,myLocation
總是返回爲null
。
// try to get GPS location
LocationManager locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
final boolean gpsEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
if (gpsEnabled) {
//request for location updates
LocationListener locationListener = new MyLocationListener();
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 100, 0, locationListener);
Location myLocation;
myLocation = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if (myLocation != null) {
//we have a location!!
}
} // end if gps enabled
下面是從LocationListener
public class MyLocationListener implements LocationListener
{
public void onLocationChanged(Location location) {
location.getLatitude();
location.getLongitude();
// TODO get accuracy, direction and speed.
// this method is unfinished...
}
}
的代碼,這裏是從清單文件的權限
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
顯然是有,我俯瞰,以獲得設備位置的東西..爲什麼最後一個已知位置總是返回空值?