Replacing Google Maps with OpenStreetMap

The BlackBerry Runtime for Android apps currently doesn't support Google Maps. However, if your app uses Google Maps, there are still two ways you can support a mapping application. One option is to replace the Google Maps library with a web view that integrates the web version of Google Maps. For more information on this process, see Mapping Support. Otherwise, you can use OpenStreetMap, which is a free worldwide map.

Download the OpenStreetMap library

You can download the source code for the OpenStreetMap library. After you download it, compile the source code again so that it's 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

You can change the imported library easily, since most class names remain the same. 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.

Currently, English is the only language that this class supports, so you need to create your own class to support other languages.

 ResourceProxyImpl resProxyImp = new
 ResourceProxyImpl(this); 

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 for updating the location, include the following:

imlp.setLocationUpdateMinDistance(1000);

To set a minimum time for updating the location, include the following:

imlp.setLocationUpdateMinTime(60000);

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

mapView.getOverlays().add(mMyLocationOverlay);

Add a marker overlay

Using the ItemizedIconOverlay class, create an overlay and add it to the map as shown here:

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);
comments powered by Disqus