B4A Library OSMDroid - MapView for B4A

Here we have my latest library - OSMDroid provides a MapView for B4A.

More info on the original (native Android) OSMDroid project can be found here: osmdroid - OpenStreetMap-Tools for Android - Google Project Hosting.

Library reference is no longer included in this post due to limits on the number of characters allowed in a single post.

I have created some tutorials to show basic usage of the library and will update this thread with a link to them as soon as i have them all uploaded.

** Your attention is drawn to the included file Apache License Version 2.0.txt, which is a copy of the Apache License Version 2.0 under which the native Android OSMDroid library is released **



  • OSMDroid_3_0_8_v3.60.zip
    361.9 KB · Views: 3,986
Last edited:


Licensed User
Longtime User
There's no built in MapView method to capture the currently displayed map and export it as an image.

All map tiles, once downloaded, are cached to the device's external memory so could in theory be stitched together to create a map image.
That stitched together image wouldn't display any Markers etc that are currently being displayed in your MapView of course.

The map is a massive Canvas object with all tiles and Markers (and any other overlays) drawn onto it.
It may be possible to get access to that canvas and export it as an image - i'll look into that when i have a bit of spare time.

Otherwise you option would be to capture the entire Activity as a screengrab - that's been discussed on the forum recently, with a code example here: http://www.b4x.com/forum/basic4android-updates-questions/16440-scrren-capture.html.



Licensed User
Longtime User
Can mapview in specified in the longitude and latitude location, add Label?:sign0085:


Licensed User
Longtime User
Can we see if we can add mouseDownOnMap, mouseMoveMap, mouseUpOnMap events?

Tap and LongTap events could be implemented by creating a 'dummy' overlay layer...

The dummy overlay layer would not display any markers etc but be used to detect tap and long tap events and trigger events that B4A could listen for.

Hopefully the order that overlay layers are added to the map could be used to control tap detection.
Add the dummy overlay first and then an overlay containing markers - a tap on a marker would be handled by the overlay containing that marker and a tap on the map would be handled by the dummy overlay layer.
I'm not sure if that is how it would work - i'll have to do some tests.

Tap and long tap events are i think the only ones that could be detected as all other events are already used by the MapView and not passed on to child views such as overlay layers.

I'll try and make time to look at creating the new dummy overlay layer this week but have a busy schedule so won't make any promises.



Licensed User
Longtime User
OSMDroid updated to version 3.21

This update adds the TilesOverlay layer to the library.

TilesOverlay is similar to the XYTileSource, you use it to add a new TileSource to your map.
A TilesOverlay however displays on top of the currently selected TileSource.
You use it to add a transparent tile layer to your map.

TilesOverlay has methods and properties:

DataConnectionEnabled As Boolean
Get or Set whether this TilesOverlay will use the device's data connection.

LoadingBackgroundColor As Int

Get or Set the background color displayed while waiting for a tile to be downloaded.
Setting a value of Colors.TRANSPARENT will disable the display of any background color.
The default value is Colors.TRANSPARENT, no background color is displayed.


Get or Set the line color used to separate the background color drawn while waiting a tile to be downloaded.
The line color will only be drawn if the background color is NOT set to transparent.
The default value is Colors.rgb(200, 192, 192).

Initialize (Name As String, MinZoomLevel As Int, MaxZoomLevel As Int, TileSize As Int, TileFilenameEnding As String, BaseUrl As String)

Initialize2 (Name As String, MinZoomLevel As Int, MaxZoomLevel As Int, TileSize As Int, TileFilenameEnding As String, BaseUrls As String)

Initialize the TilesOverlay, the TileSource must implement the Slippy map tile naming convention.
A single BaseUrl or an Array of BaseUrls can be used.

I'll upload a demo showing use of TilesOverlay shortly, meanwhile version 3.21 of OSMDroid is attached to the first post in this thread.



Licensed User
Longtime User
OSMDroid updated to version 3.22

This update adds the TouchEventsOverlay to the library.

TouchEventsOverlay is used to detect Clicks and LongClicks on the MapView.

TouchEventsOverlay has just one method:

Initialize (EventName As String)

TouchEventsOverlay raises two events:

Click (GeoPoint1 As GeoPoint)

LongClick (GeoPoint1 As GeoPoint)

All self-explanatory - you add a TouchEventsOverlay to a MapView using the MapView AddOverlay method and creates Subs to listen for either or both events.
The event Subs are pass the point clicked or long-clicked as a GeoPoint object.

I shall upload a demo to the tutorial thread shortly.

Version 3.22 is attached to the first post in this thread.



Licensed User
Longtime User
That looks very interesting...

Creates MBTiles though not 'standard' PNG tiles so i'm not sure if a library update would be required to enable those MBTiles to be used.

I've been trying to get TileMill to create an MBTiles archive from a KML file but have had no luck yet - i'll keep trying and post again when i succeed.

Being able to render a dataset as a tile layer could considerably increase the performance of the MapView - it's much more efficient to display a few tiles than render hundreds of Markers.



Licensed User
Longtime User
hi, i've just purchased basic4android. thanks for the awesome work!
i have a question about mapview: i know that google map tiles cannot be integrate with this, but is there any chance that b4a will use MapView of google api for android (https://developers.google.com/maps/documentation/android/hello-mapview)?

Have a read of this thread: http://www.b4x.com/forum/libraries-developers-questions/11304-mapview-non-starter.html#post63169

Nothing is impossible they say but adding an Android MapView to a B4A Activity is impossible!

Android MapView's work in MapActivity based Activities - no way to get an Android MapView to work in a standard Activity.
The B4A Activity is a standard Activity.

There is a chance you could create a MapActivity based Activity in Eclipse and add that to a B4A application (same as you'd create a library and add that to an application) - but read thru the thread i linked to.
I tried that and the B4A compiler failed to compile the B4A application when i tried this approach.



Licensed User
Longtime User
Have you ever tested the output of Tilemill? TileMill | Fast and beautiful maps

Seems to be a beautiful map creator.


I've had some time to look more at this and the B4A OSMDroid library will require an update to enable it to use the MBTiles archive that TileMill produces.

The native Android OSMDroid library has support for MBTiles so i can create a wrapper class around the native Android code to enable support for MBTiles in the B4A OSMDroid library.

Don't have time for that this weekend though - i'll make an effort to get it done next weekend.



Well-Known Member
Licensed User
Longtime User
Was just an idea and I came across this editor, looks pretty nice. Used it a little and seems promising.

Anyway, not a real need for me right now so don't feel rushed to support it :)


Licensed User
Longtime User
OSMDroid updated to version 3.23

This update adds a new object and a new MapView method:

  • Html As String
    Get the licensing and attribution Strings as HTML.
  • LibraryName As String
    Get the name of the 3rd party library that this AttributionObject refers to.
  • PlainText As String
    Get the licensing and attribution Strings as plain text.

GetAttributionObjects As AttributionObject()
Returns an Array of AttributionObjects.
An AttributionObject contains licensing and attribution Strings relating to 3rd party libraries used in this B4A library.
You are required to display some or all of these Strings in your application if you use the related B4A library object(s).

This is a result of this thread: http://www.b4x.com/forum/libraries-...ice-about-libraries-license-2.html#post109368 where it was discussed to better document the terms and conditions of B4A libraries that wrap other 3rd party libraries.

The attribution text is available as plain text or HTML - you can display it in a TextView, or add it to a WebView and the HTML contains links that refer back to the original licence.

Here's some example code:

Sub Process_Globals
End Sub

Sub Globals
   Dim MapView1 As MapView
End Sub

Sub Activity_Create(FirstTime As Boolean)
   Activity.AddView(MapView1, 0, 0, 100%x, 100%y)
   MapView1.SetCenter(52.75192, 0.40505)
   '   the new GetAttributionObjects method enables you to ensure your B4A application more properly complies with the terms & conditions
   '   of the various 3rd party components which make up the B4A OSMDroid library
   Dim AttributionObjects() As AttributionObject
   For i=0 To AttributionObjects.Length-1
End Sub

Sub Activity_Resume
End Sub

Sub Activity_Pause (UserClosed As Boolean)
End Sub

And that logs:

** Activity (main) Create, isFirst = true **


OSMDroid is licensed under the terms and conditions of the GNU Lesser General Public Licence.
<a href=http://code.google.com/p/osmdroid/>OSMDroid</a> is licensed under the terms and conditions of the <a href=http://www.gnu.org/licenses/lgpl.html>GNU Lesser General Public Licence</a>.
OSMDroid displays map data provided by the OpenStreetMap project. This map data is licensed under the terms and conditions of the Creative Commons Attribution 3.0 Unported Licence.
OSMDroid displays map data provided by the <a href=http://www.openstreetmap.org/>OpenStreetMap</a> project.<br />This map data is licensed under the terms and conditions of the <a href=http://creativecommons.org/licenses/by-sa/3.0/>Creative Commons Attribution 3.0 Unported Licence</a>.
MarkersBalloonOverlay is a modification of the Android MapViewBalloons project. MapViewBalloons is licensed under the terms and conditions of the Apache License, Version 2.0.

MarkersBalloonOverlay is a modification of the Android <a href=https://github.com/jgilfelt/android-mapviewballoons>MapViewBalloons</a> project.<br />MapViewBalloons is licensed under the terms and conditions of the <a href=http://www.apache.org/licenses/LICENSE-2.0>Apache License, Version 2.0</a>.

So to make sure your B4A application complies with the various 3rd party licences you now have the info you need - for example, if your application has an About Activity then you could display the attribution text there.
Otherwise it is up to you - the developer - to ensure that somewhere in your application you properly display the attributions.

I have made the B4A library source code publicly available here: Index of /b4a/osmdroid/src so as long as you display the attributions in your app, the terms and conditions of the various 3rd party licences are satisfied.

Versions 3.23 of OSMDroid is attached to the first post in this thread.

Version 3.23 contains a new resource file 'values\attribution_strings.xml' that must be added to your project's 'Objects\res' foler.
Ensure you make it READ-ONLY so that the B4A compiler does not delete it.



  • AttributionDemo.zip
    6.5 KB · Views: 281


New Member
Licensed User
Longtime User
Marker icons on high res screens

When using custom icons on devices with high res screens the icons gets scaled, and does not look good.

After hours of reading threads I give up. Sorry if I just missed something: is there any way of using high res custom icons as markers?


Licensed User
Longtime User

There is a way to force Android NOT to scale a drawable depending on the device screen density.

The solution is to add the drawables - your PNG or whatever format - image files to a folder in your project Objects\res\drawable-nodpi.

You might have already created this folder if you used some of the drawables that you downloaded with the OSMDroid library.
Otherwise create it manually and add your drawable image files to it.
IMPORTANT - make sure that all of the files that you add to this folder are made read-only otherwise the B4A compiler will delete them when it compiles your project.

Now you need a way to use those drawables in your B4A project, look at my AndroidResources library.

GetApplicationDrawable or GetApplicationDrawables are the methods to look at.

Let's say you add your image file my_nice_icon.png to the Objects\res\drawable-nodpi folder (and it's set to read-only).

Dim AndroidResources1 As AndroidResources

Dim MyDrawable as BitmapDrawable
' now you have a non-scaled BitmapDrawable to use

Some methods accept a BitmapDrawable, others require a Bitmap.
BitmapDrawable has a Bitmap property that will return the same image as a Bitmap.



Licensed User
Longtime User
There seems to be problems running OSMDroid on ICS (maybe Honeycomb too) if the device has hardware acceleration enabled.

If the device has hardware acceleration enabled and you add a ScaleBarOverlay to your MapView you'll probably see your app force close with an exception such as:

at android.view.GLES20Canvas.drawPicture(GLES20Canvas .java:900)
at uk.co.martinpearman.b4a.osmdroid.views.overlays.My ScaleBarOverlay.draw(MyScaleBarOverlay.java:287)
at org.osmdroid.views.overlay.OverlayManager.onDraw(OverlayManager.java:119)
at org.osmdroid.views.MapView.dispatchDraw(MapView.java:867)

OSMDroid is trying to use features that are available with software rendering but not available with hardware rendering.

There may be other OSMDroid objects that will cause similar exceptions, so if you are using OSMDroid on a device that has hardware acceleration enabled and your app force closes, please post in this thread - copy/paste the unfiltered log that shows the exception and i'll do my best to get things fixed.