Hi am trying get GPS using network provider and GPS provider. but am getting the same value.the value is not updated.ie my onlocation value is noy updated.
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));
}
I would take a good look at this blog post: A Deep Dive Into Location. It will provide you with a number of best practices. Calling
getLastKnownLocationis not going to start the location providers. You should use requestLocationUpdates. See snippet below: