Replacing Google Maps with OpenStreetMap

Download the source code for the OpenStreetMap library from GitHub. After you download the source code, compile it so the library is current.

Change the layout

In your source code, locate the following section of code:

<com.google.android.maps.MapView
    android:id="@+id/mapview"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:apiKey="@+string/mapviewkey"
    android:clickable="true"
/>

Change it to the code below.

<org.osmdroid.views.MapView
    android:id="@+id/mapview"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
/>

Unlike Google Maps, OpenStreetMap requires no key.

Change the imported library

Locate the following code:

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;

Change it to the code below.

import org.osmdroid.util.GeoPoint;
import org.osmdroid.views.MapController;
import org.osmdroid.views.MapView;

If you want to include PINs or markers, add the following code:

import org.osmdroid.views.overlay.ItemizedIconOverlay;
import org.osmdroid.views.overlay.ItemizedIconOverlay.OnItemGestureListener;
import org.osmdroid.views.overlay.OverlayItem;
import org.osmdroid.views.overlay.OverlayManager;
import org.osmdroid.views.overlay.mylocation.GpsMyLocationProvider;
import org.osmdroid.views.overlay.mylocation.MyLocationNewOverlay;

The default class for using resources is DefaultResourceProxyImpl.

ResourceProxyImpl resProxyImp = new
ResourceProxyImpl(this);

English is the only language that this class supports at this time. You must create your own class to support other languages.

Customize your map

Locate the following code to customize the mapView and mMyLocationOverlay classes:

mapView = (MapView) findViewById(R.id.mapview);
mapView.setBuiltInZoomControls(true);
mMyLocationOverlay = new MyLocationOverlay(this, mapView);
mapView.getOverlays().add(mMyLocationOverlay);

Change it to the code below.

mapView = (MapView) findViewById(R.id.mapview);
mapView.setUseSafeCanvas(false);

To enable zoom controls, include the following:

mapView.setBuiltInZoomControls(true);

To enable multitouch controls, include the following:

mapView.setMultiTouchControls(true);

The GpsMyLocationProvider class provides information about an object's position using GPS and cell towers. You can replace this class with your own class or add it using the following code:

GpsMyLocationProvider imlp = new
GpsMyLocationProvider(this.getBaseContext());

To set a minimum distance (in meters) for updating the location, include the following:

/**
 * public void setLocationUpdateMinDistance(final float meters)
 */
imlp.setLocationUpdateMinDistance(1000);

To set a minimum time (in milliseconds) for updating the location, include the following:

/**
 * public void setLocationUpdateMinTime(final long milliSeconds)
 */
imlp.setLocationUpdateMinTime(60000);

Create the mMyLocationOverlay object.

mMyLocationOverlay = new
MyLocationNewOverlay(this.getBaseContext(), imlp, mapView, resProxyImp);
mMyLocationOverlay.setUseSafeCanvas(false);
mMyLocationOverlay.setDrawAccuracyEnabled(true);

Add the mMyLocationOverlay object to the MapView overlay collection.

mapView.getOverlays().add(mMyLocationOverlay);

Add a marker overlay

You can use the ItemizedIconOverlay class to create an overlay and add it to the map:

ItemizedIconOverlay markersOverlay = new
ItemizedIconOverlay<OverlayItem>(new LinkedList<OverlayItem>(),
myMarker, null, resProxyImp);
mapView.getOverlays().add(markersOverlay);

Next, add the markers:

OverlayItem ovm = new OverlayItem("title", "description", new
GeoPoint(s.LatitudeE6(), s.LongitudeE6()));
ovm.setMarker(myMarker);
markersOverlay.addItem(ovm);


Got questions about leaving a comment? Get answers from our Disqus FAQ.

comments powered by Disqus