我遇到了一个问题,无法在 android 设备中获取 lat 和 long 。看看我的代码如下: -
public NewLocationActivity(final Context mContext) {
locationManager = (LocationManager) mContext
.getSystemService(Context.LOCATION_SERVICE);
locationListener = new LocationListener() {
public void onLocationChanged(Location location) {
updateLocation(location);
mSharedPreferences=mContext.getSharedPreferences(HomeSAFEPref.HomeSAFELocationPref,Context.MODE_PRIVATE);
SharedPreferences.Editor prefEditor=mSharedPreferences.edit();
String latitude=String.valueOf(currentLatitude);
String longitude=String.valueOf(currentLongitude);
String heading=String.valueOf(currentheading);
String horizAccuracy=String.valueOf(currenthorizAccuracy);
prefEditor.putString("Latitude", latitude);
prefEditor.putString("Longitude", longitude);
prefEditor.putString("Heading", heading);
prefEditor.putString("HorizAccuracy", horizAccuracy);
prefEditor.commit();
}
public void onStatusChanged(String provider, int status,
Bundle extras) {
}
public void onProviderEnabled(String provider) {
}
public void onProviderDisabled(String provider) {
}
};
locationManager.requestLocationUpdates(
LocationManager.NETWORK_PROVIDER, 0, 0, locationListener);
}
void updateLocation(Location location) {
currentLocation = location;
this. currentLatitude = currentLocation.getLatitude();
this. currentLongitude = currentLocation.getLongitude();
geoField=new GeomagneticField(
Double.valueOf(location.getLatitude()).floatValue(),
Double.valueOf(location.getLongitude()).floatValue(),
Double.valueOf(location.getAltitude()).floatValue(),
System.currentTimeMillis()
);
this.currentheading=geoField.getInclination();
this.currenthorizAccuracy=currentLocation.getAccuracy();
this.speed=(int) currentLocation.getSpeed();
}
当我创建类 NewLocationActivity 构造函数的对象时。但我的问题是 UpdateLocation 方法没有运行。为什么我不知道......但有时在其他设备上它运行完美,我很容易得到纬度和经度。请让我知道为什么我得到 lat 和 long 0,0.Thanks提前..