我有一个简单的类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