我使用下面的code,让我的当前位置。但我面临的问题是,它总是返回0.0的经度和纬度。我打开了手机上的GPS设置,并设置所有权限。不过还是我面对这一点。
I am using the following code to get my current location. But the problem I am facing is, it always returns 0.0 for latitude and longitude. I have turned on the GPS settings in the phone and set all the permissions. But still I am facing this.
locationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
customLocationListener = new CustomLocationListener();
locationManager.requestLocationUpdates(
LocationManager.GPS_PROVIDER,
0,
0,
customLocationListener);
class CustomLocationListener implements LocationListener{ ............
public void onLocationChanged(Location argLocation) {
if(location != null) {
int latitude=(int)(argLocation.getLatitude()*1E6);
int longitude=(int)(argLocation.getLongitude()*1E6);
}
} ........ }
你知道的任何一个,为什么?
Do any one of you know why?
注:我的活动来延长MapActivity没有位置监听器
Note : My activity extends MapActivity not Location Listener
试试这样
locationManager = (LocationManager) context
.getSystemService(Context.LOCATION_SERVICE);
Criteria locationCritera = new Criteria();
String providerName = locationManager.getBestProvider(locationCritera,
true);
if(providerName!=null)
location = locationManager.getLastKnownLocation(providerName);
locationListener = new MyLocationListener();
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0,
0, locationListener);
在mylocationlistener设置你的位置对象
in mylocationlistener set your location object
private class MyLocationListener implements LocationListener {
public void onLocationChanged(Location loc) {
if (loc != null) {
location = loc;
}
}
public void onProviderDisabled(String provider) {
}
public void onProviderEnabled(String provider) {
}
public void onStatusChanged(String provider, int status, Bundle extras) {
}
}
上一篇:如何解决ANR错误,而调用摄像机?如何解决、摄像机、错误、ANR
下一篇:setRequestProperty方法的给java.lang.IllegalStateException:不能设置方法是由连接后方法、是由、setRequestProperty、java