您好我正在尝试使用网络提供商和GPS提供商获取GPS。但我得到相同的值。该值未更新。我的onlocation值是noy更新。
private Location networkLoc;
private Location gpsLoc;
private static double lat;
private static TextView latituteField1;
private static TextView longitudeField1;
private static double lng ;
private static double lat1;
private static double lng1 ;
provider = locationManager.getBestProvider(criteria, false);
networkLoc = locationManager.getLastKnownLocation(provider);
gpsLoc = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if (networkLoc != null) {
System.out.println("Provider " + provider + " has been selected.");
lat1 = (double) (networkLoc.getLatitude());
lng1 = (double) (networkLoc.getLongitude());
latituteField.setText(Double.toString(lat1));
longitudeField.setText(Double.toString(lng1));
}
if ( gpsLoc != null) {
System.out.println("Provider " + provider + " has been selected.");
lat = (double) (gpsLoc.getLatitude());
lng = (double) (gpsLoc.getLongitude());
latituteField1.setText(Double.toString(lat));
longitudeField1.setText(Double.toString(lng));
}
@Override
public void onLocationChanged(Location location) {
lat1 = (double) (networkLoc.getLatitude());
lng1 = (double) (networkLoc.getLongitude());
latituteField.setText(Double.toString(lat1));//these two values are same
longitudeField.setText(Double.toString(lng1));
lat = (double) (gpsLoc.getLatitude());
lng = (double) (gpsLoc.getLongitude());
latituteField1.setText(Double.toString(lat));//these two values are same
longitudeField1.setText(Double.toString(lng));
}
答案 0 :(得分:0)
我会好好看一下这篇博文:A Deep Dive Into Location。它将为您提供一些最佳实践。调用getLastKnownLocation
不会启动位置提供程序。您应该使用requestLocationUpdates。请参阅以下代码段:
LocationManager lm = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
Criteria fineCriteria.setAccuracy(Criteria.ACCURACY_FINE);
String fineProvider = lm.getBestProvider(fineCriteria, true);
// Start the fine location listener
lm.requestLocationUpdates(fineProvider, 0, 0, locationListenerFine);
private LocationListener locationListenerFine = new LocationListener() {
public void onLocationChanged(Location location) {
}
public void onProviderDisabled(String provider) {
}
public void onProviderEnabled(String provider) {
}
public void onStatusChanged(String provider, int status, Bundle extras) {
}
};