Logo Icon Logo
A Crowd-sourced Cookbook on Writing Great Android® Apps
GitHub logo Twitter logo OReilly Book Cover Art

Getting Location Updates with OpenStreetMap Maps

Author: Rachee Singh -- Published? true -- FormatLanguage: W


You need to react to the changes in the device's location and move the map focus to changed location


Using LocationListener, an application can request for location updates and then react to these changes in location


  • The activity that includes the OSM MapView needs to implement "LocationListener" to be able to request for changes in the device's location. An activity implementing LocationListener will also need to add the unimplemented (abstract) methods from LocationListener interface (Eclipse will do this for you). We set the center of the map to the GeoPoint named mapCenter so that the application starts with map focussed around that point.
  • Now we need to get an instance of "LocationManager" and use to to request for location updates using the requestLocationUpdates method.
  • One of the overridden methods (which were abstract in the LocationListener interface) named 'onLocationChanged' we can write the code that we want to be executed when the location of the device changes.
  • In the onLocationChanged method we obtain the latitude and longitude of the new location and set the map's center to the new GeoPoint.
public class LocationChange extends Activity implements LocationListener {
    private LocationManager myLocationManager;
	private MapView mapView;
	private MapController mapController;

    public void onCreate(Bundle savedInstanceState) {
        mapView = (MapView)findViewById(R.id.mapview);
        mapController = this.mapView.getController();
        GeoPoint mapCenter = new GeoPoint(53554070, -2959520); 
        myLocationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
        myLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 100, this);

	public void onLocationChanged(Location location) {
        int latitude = (int) (location.getLatitude() * 1E6);
        int longitude = (int) (location.getLongitude() * 1E6);
        GeoPoint geopoint = new GeoPoint(latitude, longitude);

	public void onProviderDisabled(String arg0) {

	public void onProviderEnabled(String arg0) {

	public void onStatusChanged(String arg0, int arg1, Bundle arg2) {

When the application starts, the map is centered around the mapCenter GeoPoint. Since the application is listening to location changes, the icon in the top bar of the phone is visible:

Now using the Emulator controls new GPS coordinates (-122.094095, 37.422006) are sent to the emulator. The application reacts to it and centers the map around then new coordinates:

Similarly, different GPS coordinates are given from the Emulator controls and the application centers the map around the new location:

Also, to allow the application to listen for location changes, include these permissions in the AndroidManifest.

<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/> 
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" />


The source code for this project can be downloaded from https://docs.google.com/leaf?id=0B_rESQKgad5LNGViMzhmM2ItZGFiZC00NGVhLWJmNjctNTRjNTA0M2QzMjdh&hl=en_US.
No records found.