
Image from
http://failblog.cheezburger.com/
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!