我有一個簡單的類GPSListener
其獲取GPS座標:的Android位置服務不更新requestLocationUpdates
public class GPSListener implements LocationListener {
public static double latitude;
public static double longitude;
@Override
public void onLocationChanged(Location loc)
{
loc.getLatitude();
loc.getLongitude();
latitude = loc.getLatitude();
longitude = loc.getLongitude();
Log.d("GPSLISTENER ", "lat: "+latitude+" long:"+longitude);
} ...
然後試圖利用這個類在我的活動,我簡單地調用這個類在我onCreate()
功能我的活動:
LocationManager mlocManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
LocationListener mlocListener = new GPSListener();
Criteria criteria = new Criteria();
String bestProvider = mlocManager.getBestProvider(criteria, false);
mlocManager.requestLocationUpdates(bestProvider, 0, 0, mlocListener);
mlocManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, mlocListener);
if (mlocManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
lat = GPSListener.latitude;
lon = GPSListener.longitude;
Log.d("GPS", "lat: "+lat+" long:"+lon);
} else {
// TODO: GPS not enabled
Log.d("GPSERROR", "GPS not enabled");
}
但每當我運行應用程序,在我的活動lat
和lon
始終爲零。我不太清楚如何解決這個問題。
當日志:
Log.d("GPSLISTENER ", "lat: "+latitude+" long:"+longitude);
它返回正確的緯度和經度,它只是需要一兩秒鐘的活動開始後。
Log.d("GPSSUCCESS", "lat: "+lat+" long:"+lon);
僅返回0.0。在if語句執行之前,我的印象是.requestLocationUpdates
會將值傳遞給lat
和lon
。我怎樣才能做到這一點?
你添加的權限(android.permission.ACCESS_FINE_LOCATION)在清單文件中的位置? – prijupaul
@prijupaul是的。 –
如果您正在仿真器上進行測試,您需要將座標傳遞給它,或者如果您在設備上,請重新啓動設備並檢查。 – Shrikant