In this and the following tutorials I will show you how to include a OpenStreetMap in your activity, create offline maps for OpenStreetMaps and how to define minimum and maximum Zoom Levels (because you surely don’t want to provide all zoom levels for offline mode).
1. Get the needed osmdroid’s library, I used the osmdroid-android-3.0.2.jar
here
2. Lets create a new simple Android project (you know how that is done)
3. Add the osmdroid-library to your Android project.
Depends on the IDE you use, in IntelliJ right-click on your project and select Open Module Settings. Goto “Libraries”, create a new library and attach the osmdroid-jar to it.
4. Now the fun begins:
In our AndroidManifest.xml define the permissions needed:
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/> <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/> <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" /> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> <uses-permission android:name="android.permission.INTERNET" /> <uses-feature android:name="android.hardware.location.network" /> <uses-feature android:name="android.hardware.location.gps" /> <uses-feature android:name="android.hardware.wifi" />
Let’s create the easiest MapView.
In the Activity’s onCreate-method create a new Object of Type MapView (exactly: org.osmdroid.views.MapView) like this:
MapView mapView = new MapView(this, 256);
That uses the context as the first parameter, and the tile size of 256px (needed later).
We want to be able to zoom in and out, so let’s define that:
mapView.setClickable(true); mapView.setBuiltInZoomControls(true);
And let’s set the ContentView:
setContentView(mapView);
So that was our onCreate-method. You may now start in the emulator or on your device and you should see the OpenStreetMap of the whole world. By clicking the MapView the standard zoom-in/zoom-out buttons appear and that should work, too, given you have an internet connection.
5. Play around with the options
Let’s set the zoom level (the standard one is funny, but not really helpful). Thats done via an instance of the MapController, you can easily grab the MapView’s MapController by calling
mapView.getController().setZoom(10);
Play around with the numbers, to see how close to earth you can get
But maybe you also want to set a specific point as the map’s center. That’s also done via the MapController:
mapView.getController().setCenter(new GeoPoint(53.550657, 9.995499));
Use a GeoPoint to specify the coordinates (first param is latitude, second longitude) and use this as paramter for setCenter(). Start the app again and look where I directed you: the most beautiful city in the world
6. Have fun playing with coordinates and zoom levels.
7. BUT! What if you want offline maps?
Let’s say you’re on holiday in the US, Germany, England, India, China, (insert your favorite country) and need a map on your mobile device, but you don’t want to pay thousands of €/$/¥/(insert your favorite currency) just for displaying a map?
Also, how do we set set minimum and maximum zoom levels?
Next time…
This is the complete onCreate method:
/** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); mapView = new MapView(this, 256); mapView.setClickable(true); mapView.setBuiltInZoomControls(true); mapView.getController().setZoom(10); mapView.getController().setCenter(new GeoPoint(39.461078, 2.856445)); setContentView(mapView); }
18. März 2011 um 10:44 am
[...] Using OpenStreetMaps with osmdroid on Android Mrz [...]
9. April 2011 um 8:39 am
Hi. First of all great tutorial =D, I just started working with osmdroid, due some limitations Google maps has *sighs*…and I was able to kinda do the same you did in this tutorial ( just a pity I didnt find it when I started it D: )… but anyway… I’m currently with a problem that may sound really noobish, but while using Google Maps was actually easy, the lack of documentation of this is making it particulary dificult…. and I mean about the simple task of adding custom icons into the map, after concluding this task ( displaying the map ).
Could I ask you for help on this matter =( ?
8. Juli 2011 um 4:04 am
Hi,
Great tutorial, but I’m facing an issue that mine device don’t have an sd card.
Can I still use map in offline mode? Could You please give me a tip how to redirect it to a different storage than sd card to read map from ?
Thanks in advance.
11. Oktober 2011 um 6:21 pm
Thanks for the tutorial, I was completely lost as to how to use the osmdroid package until I read it.
I initially ran into an issue when trying to launch my android app, it would immediately crash. Debugging showed it to be a NoClassDefFoundError when creating the MapView object. As it turned out, this was because it was not enough to just add the jar files to the project as a library, rather I had to copy them into my assets folder and then add them (Right Click->Build Path->Add External Archives…). This meant that the jars were then packaged up in my app, fixing the problem. I’m not sure if this is the best practice (this is my first android app, and I have not programmed with java for a while), but it seems to work for me.
Cheers
I
17. Oktober 2011 um 10:23 am
Hi,
Nice tutorial, exactly what I needed but the application stops working before the map is displayed. I did everything just like you explained but it just wont work and it shows no error, just stops.
I’m using eclipse and android virtual machine.
I’ve also tried Matt’s solution with including .jar in the project, still doesn’t work.
Any suggestion?
21. Oktober 2011 um 3:55 am
Got the solution.
You have to include slf4j-android-1.6.1.-RC1.jar.
I’m not sure what this jar does but after including it in my project everything works fine.
12. November 2011 um 5:04 am
Vielen Dank für dieses Tutorial. Ich habe dadurch endlich meinen Fehler gefunden, der ständig die NoDefFoundClass Exception geworfen hatte: Da ich es von google maps noch so gewohnt war, war die entsprechende Activity von MapActivity abgeleitet. AUTSCH! Der Fehler hat mich fast zur Verzweiflung gebracht.
Aber eine Frage hätte ich da noch: Beim bauen installiert er die Open Street Map View apk auch mit aufs Handy, so dass ich dann zwei Apps (meine und “Open Map”) auf dem Telefon habe. WIe kann ich das ändern?
12. November 2011 um 5:07 am
Hat sich gerade erledigt. Dadurch dass ich nicht merh von MapAcitivity ableite, brauche ich das Projekt OpenStreetMapView nicht mehr im Java Build Path zu haben -> Er installiert die Open Map nicht mehr! Danke!