I’m here agan. I got another question about google maps in android. I’m trying to update the map displayed in the map view in such a way that when the location of the user is updated through gps, the map will display the map of that location.
I’m currently using GeoPoint and MapController but I wasn’t getting the result. Instead it is animating to a wrong location. I tried interchanging the longitude and latitude values but the result is still the same.
Here is the source code for my activity.
public class MyActivity extends MapActivity {
LocationManager manager;
Location currentLocation;
MapController mapController;
TextView locationView;
MapView mapView;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
locationView = (TextView) findViewById(R.id.textView);
mapView = (MapView) findViewById(R.id.mapview);
mapView.setBuiltInZoomControls(true);
mapController = mapView.getController();
mapController.setZoom(5);
manager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
}
@Override
protected void onPause() {
super.onPause();
manager.removeUpdates(listener);
}
@Override
protected void onResume() {
super.onResume();
if (!manager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Location Manager");
builder.setMessage("We want to use your location, but GPS is currently disabled.\nWould you like to change these settings now?");
builder.setPositiveButton("Yes",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Intent i = new Intent(
Settings.ACTION_LOCATION_SOURCE_SETTINGS);
startActivity(i);
}
});
builder.setNegativeButton("No",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
finish();
}
});
builder.create().show();
}
currentLocation = manager
.getLastKnownLocation(LocationManager.GPS_PROVIDER);
updateDisplay();
int minTime = 5000;
float minDistance = 0;
manager.requestLocationUpdates(LocationManager.GPS_PROVIDER, minTime,
minDistance, listener);
}
private void updateDisplay() {
if (currentLocation == null) {
locationView.setText("Determining your location...");
} else {
locationView.setText(String.format("Your location:\n%.2f, %.2f",
currentLocation.getLatitude(),
currentLocation.getLongitude()));
}
}
private LocationListener listener = new LocationListener() {
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
@Override
public void onProviderEnabled(String provider) {
}
@Override
public void onProviderDisabled(String provider) {
}
@Override
public void onLocationChanged(Location location) {
currentLocation = location;
GeoPoint point = new GeoPoint((int) currentLocation.getLatitude(),
(int) currentLocation.getLongitude());
mapController.setCenter(point);
updateDisplay();
}
};
@Override
protected boolean isRouteDisplayed() {
return false;
}
}
I’m quite sure there is no problem in the xml and manifest since the Google Map itself is being displayed. Thanks for the help.
Did you remember to convert to microdegrees? From the documentation:Constructs a GeoPoint with the given latitude and longitude, measured in microdegrees (degrees * 1E6). So, you need to multiply the lat and long you got from the location listener by 1E6 otherwise it will animate most likely to a location off the coast of Africa.