I am trying to get coordinates from GPS (this actually gives the values for lat and long on a Toast.) i am trying to get that values and use it as Map coordinates and to show the current location but it is not showing it. where have i gone wrong?
public class Map extends MapActivity {
MapController mControl;
GeoPoint GeoP;
MapView mapV;
public double lat;
public double longi;
private static final long MINIMUM_DISTANCE_CHANGE_FOR_UPDATES = 1; // in Meters
private static final long MINIMUM_TIME_BETWEEN_UPDATES = 1000; // in Milliseconds
protected LocationManager locationManager;
@Override
public void onCreate (Bundle saveInstanceState){
super.onCreate(saveInstanceState);
setContentView(R.layout.map);
mapV = (MapView)findViewById(R.id.mapview);
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
locationManager.requestLocationUpdates(
LocationManager.GPS_PROVIDER,
MINIMUM_TIME_BETWEEN_UPDATES,
MINIMUM_DISTANCE_CHANGE_FOR_UPDATES,
new MyLocationListener()
);
Location location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if (location != null) {
lat = location.getLatitude();
longi = location.getLongitude();
}
String message = String.format(
"New Location \n Longitude: %1$s \n Latitude: %2$s",
location.getLongitude(), location.getLatitude()
);
Toast.makeText(Map.this, message, Toast.LENGTH_LONG).show();
GeoP = new GeoPoint ((int)(lat*1E6),(int)(longi * 1E6));
mControl = mapV.getController();
mControl.animateTo(GeoP);
mControl.setZoom(13);
}
protected boolean isRouteDisplayed() {
// TODO Auto-generated method stub
return false;
}
private class MyLocationListener implements LocationListener {
public void onLocationChanged(Location location) {
String message = String.format(
"New Location \n Longitude: %1$s \n Latitude: %2$s",
location.getLongitude(), location.getLatitude()
);
Toast.makeText(Map.this, message, Toast.LENGTH_LONG).show();
}
@Override
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
}
@Override
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub
}
}
}
I wonder why your are not tring to display location in onLocationChanged Callback.
When any time your device fetch some location this method gets called at first.
So include following code in onLocationChanged
It should be something like this.