How to replace Google Maps with OpenStreet Maps in your BlackBerry 10 Android App

Android Development

On our developer website, we provide a seamless solution consisting of replacing the Google Maps library for Android with a web view integrating the web version of Google Maps. More information can be found by following this link.

That said, a fellow Italian developer (the maker of Prezzi Benzina), just sent me detailed information on how they easily replaced Google Maps with Open Street Maps, a free worldwide map providing open data under the ODBL (Open Data Commons Open Database License).

The result? Outstanding! And since it is a very common issue faced by many Android developers making use of Google Maps in their Android applications, we wanted to share in this blog how they did it.

Download

First of all, you must download the Open Street Maps library from the following link: https://code.google.com/p/osmdroid/

You can either download the jar ready to use osmdroid-android-3.0.8.jar or download the source code and compile it (recommended).

Step 1: Layout Change

Pretty easy. You code goes from

<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" />

to

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

Unlike Google Maps, no key is needed for OSM.

Step 2: Modify Import

Most class names remain the same, so it’s very straight forward..

Your code goes from:

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

to

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

In addition, you might want to add the following:

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;

WARNING: resources

The default class for resources is: DefaultResourceProxyImpl. It handles English only, so you probably have to create your own with the proper translations.

    ResourceProxyImpl resProxyImp = new ResourceProxyImpl(this);

Step 3: Map Customization

From

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

to

mapView = (MapView) findViewById(R.id.mapview);
mapView.setUseSafeCanvas(false);
//enable zoom controls
mapView.setBuiltInZoomControls(true);

//enable multitouch
mapView.setMultiTouchControls(true);
//GpsMyLocationProvider can be replaced by your own class. It provides the position information through GPS or Cell towers.
GpsMyLocationProvider imlp = new GpsMyLocationProvider(this.getBaseContext());
//minimum distance for update
imlp.setLocationUpdateMinDistance(1000);
//minimum time for update
imlp.setLocationUpdateMinTime(60000);       
mMyLocationOverlay = new MyLocationNewOverlay(this.getBaseContext(),imlp , mapView, resProxyImp);
mMyLocationOverlay.setUseSafeCanvas(false);
mMyLocationOverlay.setDrawAccuracyEnabled(true);

mapView.getOverlays().add(mMyLocationOverlay);

Step 4: Add Marker

In OSMDroid there are some classes ready to use. The best is ItemizedIconOverlay.

Create an overlay and add it to the map:

ItemizedIconOverlay markersOverlay = new ItemizedIconOverlay(new LinkedList(), myMarker, null, resProxyImp);
mapView.getOverlays().add(markersOverlay);
Add markers
OverlayItem ovm = new OverlayItem("titolo", "descrizione", new GeoPoint(s.LatitudeE6(), s.LongitudeE6()));
ovm.setMarker(myMarker);
markersOverlay.addItem(ovm);

Of course, this is just the basic part of the story. There is much more available on OSMDroid – feel free to explore and bring your Android app to BlackBerry 10 easily!

About Luca Filigheddu

I currently manage the developer ecosystem and developer communities globally for BlackBerry. Engineer, developer since 1992, spent the past few years evangelising developers about the BlackBerry 10 platform. Previously I worked for a few startups and companies within the telecommunication industry in various management roles. Very active on Twitter (@filos), I love gadgets, playing piano and taekwondo. Husband and father, too

Join the conversation

Show comments Hide comments
+ -
  • http://geodatapolicy.wordpress.com/2013/03/14/mapping-the-growth-of-openstreetmap/ Mapping the Growth of OpenStreetMap | GEODATA POLICY

    […] How to replace Google Maps with OpenStreet Maps in your BlackBerry 10 Android App (devblog.blackberry.com) […]

  • http://blog.openstreetmap.cl/2013/04/04/resumen-semanal-osm-65/ OpenStreetMap Chile » Blog Archive » Resumen Semanal OSM #65

    […] puedes reemplazar GoogleMaps por mapas de OSM en tu teléfono Blackberry? Un artículo de blog por Luca […]

  • http://blog.openstreetmap.org/2013/03/27/weekly-osm-summary-65/ Weekly OSM Summary #65 | OpenStreetMap Blog

    […] can you replace GoogleMaps with OSM maps on your Blackberry phone? A blog post by Luca […]

  • http://blackberrydeveloper.es/reemplazar-google-maps-openstreet-maps-blackberry-10-android-app/ Cómo reemplazar Google Maps con OpenStreet Maps en tu BlackBerry 10 Android App | BlackBerry Developer Groups Spain

    […] Por supuesto, esto es sólo una pequeña introducción. OSMDroid tiene muchísimas más utilidades que pueden ser muy interesantes para convertir tu aplicación Android a BlackBerry 10. El post original lo puedes encontrar aquí. […]

  • http://blackberrydeveloper.es/android-open-street-maps-blackberry-10/ Cómo reemplazar Google Maps por Open Street Maps en tu aplicación Android para BlackBerry 10 | BlackBerry Developer Groups Spain

    […] Por supuesto, esto es sólo una pequeña introducción. OSMDroid tiene muchísimas más utilidades que pueden ser muy interesantes para convertir tu aplicación Android a BlackBerry 10. El post original lo puedes encontrar aquí. […]

  • http://www.berryreview.com/2013/03/14/dev-explains-how-to-replace-google-maps-with-open-street-maps-in-blackberry-10-android-apps/ Dev Explains How to Replace Google Maps With Open Street Maps in BlackBerry 10 Android Apps - BerryReview - Mobile Edition

    […] Check out the details in Luca’s post on the DevBlog […]

  • http://appgong.com/1326/ How to replace Google Maps with OpenStreet Maps in your BlackBerry 10 Android App | BlackBerry Developer Blog - appgong

    […] How to replace Google Maps with OpenStreet Maps in your BlackBerry 10 Android App | BlackBerry Devel… […]

blog comments powered by Disqus