Get last location latitude and longitude
This is my code.
public class MyLocationListener implements LocationListener {
@Override
public void onLocationChanged(Location loc) {
Location location = getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
LastLocationLat = location.getLati开发者_开发技巧tude();
LastLocationLongi = location.getLongitude();
LocationLat = loc.getLatitude();
LocationLongi = loc.getLongitude();
if(loc.hasSpeed()) {
float mySpeed = loc.getSpeed();
Toast.makeText(getApplicationContext(), ""+mySpeed, 2000).show();
}
}
@Override
public void onProviderDisabled(String provider) {
Toast.makeText(getApplicationContext(), "Gps Disabled", Toast.LENGTH_SHORT).show();
}
@Override
public void onProviderEnabled(String provider) {
Toast.makeText(getApplicationContext(), "Gps Enabled", Toast.LENGTH_SHORT).show();
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
Toast.makeText(getApplicationContext(), "Gps onStatusChanged", Toast.LENGTH_SHORT).show();
}
}
But i did not get last location in this code i got same latitude and longitude
You are using LocationManager.NETWORK_PROVIDER to get locations. Your code implies you want GPS ("Gps Disabled")? In this case you should use LocationManager.GPS_PROVIDER.
LocationManager.NETWORK_PROVIDER is not very accurate (few hundred meters if locking to cell towers), so you might not detect the location change if you move less then 100m.
On the other hand, GPS is usually unavailable indoors or between tall buildings. Also GPS uses power so should not be used all the time (when app is in the background).
Read about Obtaining User Location, for the best approach to getting location with regards to accuracy vs service availability.
Hope this can help:
http://www.tutorials-android.com/learn/How_to_get_the_last_known_GPS_location.rhtml
精彩评论