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 **

Martin.
 

Attachments

  • OSMDroid_3_0_8_v3.60.zip
    361.9 KB · Views: 4,080
Last edited:

warwound

Expert
Licensed User
Longtime User
Hi.

Can you test this library update?

I've added a single new method to the MarkersBalloonOverlay object, named UnsetFocusedMarker.

Typical usage:

B4X:
MarkersBalloonOverlay1.UnsetFocusedMarker
MapView1.Invalidate

I tested it and it seems to work ok, if you can confirm it works too then i'll get the update uploaded.

Thanks.

Martin.
 
Last edited:

warwound

Expert
Licensed User
Longtime User
OSMDroid updated to version 3.52

This update adds a single new method to the MarkersBalloonOverlay object:

UnsetFocusedMarker
Clears the focused Marker, effectively closing the Balloon.

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

Martin.
 

Vader

Well-Known Member
Licensed User
Longtime User
Is it possible to put an OSMDroid map onto a b4a panel (you tell me what I need to use) so that it only forms a part of the activity viewed?
 

warwound

Expert
Licensed User
Longtime User
Hi.

Yes that's possible.
An OSMDroid is 'just another View' (actually it's a ViewGroup) so you can add a MapView to any other container View such as a Panel:

B4X:
Sub Process_Globals
End Sub

Sub Globals
   Dim MapView1 As MapView
   Dim Panel1 As Panel
End Sub

Sub Activity_Create(FirstTime As Boolean)
   Panel1.Initialize("")
   MapView1.Initialize("")
   
   MapView1.SetMultiTouchEnabled(True)
   MapView1.SetZoomEnabled(True)
   
   '   set the zoom level BEFORE the center (otherwise unpredictable map center may be set)
   MapView1.Zoom=14
   MapView1.SetCenter(52.75192, 0.40505)
   
   Activity.AddView(Panel1, 25, 10, 256dip, 256dip)
   Panel1.AddView(MapView1, 0, 0, Panel1.Width, Panel1.Height)
End Sub

Sub Activity_Resume
End Sub

Sub Activity_Pause (UserClosed As Boolean)
End Sub

Martin.
 

Attachments

  • MapViewInPanel.zip
    5.7 KB · Views: 227

Vader

Well-Known Member
Licensed User
Longtime User
Thanks Martin, that's great.

Now another question, and apologies if you have covered it in your extensive examples:
Is it possible to pre-cache different zoom levels (say onto SD), and load them if they are available?

ie if no Internet access, can you grab a bunch of tiles at specific heights, and package them so they are displayed?

Thanks in advance.

Dave
 

schimanski

Well-Known Member
Licensed User
Longtime User
Hello!!

With MarkersBalloonOverlay, it is possible to show a balloon with some informations about the marker. Is it possible to show a smal kind of flag with a short information to each marker at the same time?

I thought something like this:
 

Attachments

  • marker.png
    marker.png
    440 bytes · Views: 198

warwound

Expert
Licensed User
Longtime User
Hello!!

With MarkersBalloonOverlay, it is possible to show a balloon with some informations about the marker. Is it possible to show a smal kind of flag with a short information to each marker at the same time?

I thought something like this:

There's no way in the OSMDroid methods to do this, but take a look here: http://www.b4x.com/forum/additional...ras-advanced-click-handling-3.html#post157394.

The OSMDroid balloon uses an Android TextView to display info and a TextView is the same as a B4A Label.

So you might be able to embed IMG tags in your marker description text and use LabelExtras to add the image.
If you try can you post with your results - it'd be interesting to know if it works.

Martin.
 

schimanski

Well-Known Member
Licensed User
Longtime User
Hello,

is it possible to show maptiles with .png-ending? All the tiles of osmdroid ends with .png.tile, but my tile-downloader only gets tiles with in .png-format...

Thanks for your efforts..

Gesendet von meinem XT910 mit Tapatalk 2
 

warwound

Expert
Licensed User
Longtime User
Yes that's certainly possible.

Just set the TileFilenameEnding parameter in the TilesOverlay Initialize method to .png.

OSMDroid saves cached tiles with a .png.tile file type so that your device doesn't automatically index these cached tiles for the Gallery application.
You'd not want to see 100s or 1000s of cached tiles in your Gallery?

There is an alternative method to prevent the device from indexing image files - by placing a file named .nomedia in the directory that contains images you don't want indexed.
Take a look here: Disable Media Scanning in Specific Android Directories With a .Nomedia File.

Excluding tiles from the Gallery index will not just prevent the Gallery from getting cluttered with 'junk' files, it will also speed up the device preventing it wasting CPU cycles indexing the tiles.

Martin.
 

schimanski

Well-Known Member
Licensed User
Longtime User
Very much thanks for your fast answer. It is much easier than I thought....thanks

Gesendet von meinem XT910 mit Tapatalk 2
 

schimanski

Well-Known Member
Licensed User
Longtime User
Hello warwound!

Sorry, but it seems not to be as simple as I thought..

I use in my app the Tilesources with MapView1.GetTileSources. So, it is not clear for my how to use TilesOverlay.Initialize. I think, that I than have to use all the urls to get the TileSources.

Than I have tried this:

B4X:
...
..
MapView1.Initialize("")
   
   If FirstTime Then
      MapCenter.Initialize(0, 0)
      ZoomLevel=2
      
      Dim CurrentTilesSources As List
      CurrentTilesSources=MapView1.GetTileSources
      If CurrentTilesSources.IndexOf("Mapnik2")=-1 Then
         Dim MyXYTileSource As XYTileSource         
         MyXYTileSource.Initialize("Mapnik2", 0, 18, 256, ".png", "http://tile.openstreetmap.org/")
         MapView1.AddXYTileSource(MyXYTileSource)
      End If
      TileSource="Mapnik2"
   End If
   
   TileSourceSpinner.Initialize("TileSourceSelect")
...
...

Now, this code loads the tiles in the folder Mapnik2/, but the tiles ends all with .png.tile and not with .png????

What is wrong??
 

warwound

Expert
Licensed User
Longtime User
I'm not sure what you're trying to do...

Do you have a set of already downloaded tiles in the directory osmdroid/Mapnik2 and you're trying to load these already downloaded tiles BUT they have been saved with the .png.tile file type?

Or are you successfully downloading and displaying some other tiles and the problem is that these tiles are being cached with a .png.tile file type?

I tried your posted code but it fails, the log shows a 404 file not found for all tiles at tile.openstreetmap.org.

Give me an idea of what you are trying to acheive - are you wanting to display tiles that are on the device external memory or tiles from an online tileserver?

Martin.
 

schimanski

Well-Known Member
Licensed User
Longtime User
Hello, thanks for your answer.

I have a mapnik-rendering tool, that render the tiles only with the ending .png. So, I have a lot of mapnik-tiles on my sd-card with the ending.png. Now, I'm not able to show these tiles in the mapview of osmdroid. The problem is, that the mapnik-tool only cache the tiles with .png, there are no settings to modify the ending-format.

Gesendet von meinem XT910 mit Tapatalk 2
 

warwound

Expert
Licensed User
Longtime User
The best solution is to package these rendered tiles into a tile archive created by Mobile Atlas Creator(MOBAC).
Another link here: MobileAtlasCreator - osmdroid - How to use Mobile Atlas Creator. - OpenStreetMap-Tools for Android - Google Project Hosting.

MOBAC can be configured to create a tile archive from various sources.
You need to read the MOBAC FAQs: Mobile Atlas Creator - FAQ and consult the MOBAC README file.

You can configure MOBAC to create a tile archive from a directory of tiles on your PC or from a tool that serves tiles over an HTTP connection.

Can you 'mapnik-rendering tool' serve tiles over HTTP (just as a standard tile server does)?
If so then here's an example config file that, along with the readme file, should help you configure MOBAC:

B4X:
<?xml version="1.0" encoding="UTF-8"?>
<customMapSource>
<name>amd1055t-ubuntu</name>
<minZoom>0</minZoom>
<maxZoom>18</maxZoom>
<tileType>png</tileType>
<tileUpdate>IfNoneMatch</tileUpdate>
<url>http://amd1055t-ubuntu/osm/{$z}/{$x}/{$y}.png</url>
<backgroundColor>#000000</backgroundColor>
</customMapSource>

Here amd1055t-ubuntu is a virtual machine running apache/mapnik and renderd (the standard OSM tile server package).
I have that XML file saved as amd1055t-ubuntu.xml in my MOBAC mapsources directory.
When i run MOBAC i can select this custom tile source - it's listed along with all the others in MOBAC.
Now i can draw a rectangle on the map, select the zoom levels i want to include in the tile archive and hit the GO button.
Time for a coffee and once it's complete i have a tile archive all ready for use in OSMDroid.

There's a tutorial to use a tile archive in OSMDroid here: http://www.b4x.com/forum/basic4andr...smdroid-mapview-b4a-tutorial-2.html#post92702.

Something similar can be done to use a directory of tiles as a MOBAC source (that is file based not HTTP based access), take a look here: Mobile Atlas Creator - Readme.
This isn't something i hav done so can't provide an example.

Either method should i hope work for you.
A tile archive is a more efficient use of external memory - a single tile archive will take far less space than a directory with hundreds or thousands of tiles.

Martin.
 

schimanski

Well-Known Member
Licensed User
Longtime User
Thanks for your efforts, warwound!

I'm not a fan of the MOBAC, because it is not possible to download bigger areas without getting a lot of 'access blocked tiles'. So, I had to look for other tools to download map-tiles for offline cache.

I have posted a modified example of your xytilesource-demo to show you, what I mean. For all I have initialized the TileSource with .png, the example only saves the tiles with .png.tile:confused:

B4X:
MyXYTileSource.Initialize("Mapnik2", 0, 18, 256, ".png", "http://tile.openstreetmap.org/")

Is it not possible to save and load these tiles as .png?
 

warwound

Expert
Licensed User
Longtime User
But if you configure MOBAC to create a tile archive from local tiles - either using HTTP or file access - then there is no restrictions on usage, you're free to use your own tiles as much as you want...

There are limits when you try to put a lot of tiles into a single archive but that's a limit to ensure performance isn't degraded with massive tile archives.
(This limit is 500,000 tiles per tile archive).

If you look in the MOBAC settings under the Network tab you'll see you can set the maximum number of parallel connections to a tile server and also the maximum download bandwidth.
If i use MOBAC to create a tile archive from the tile server i have running on my own computer i set the number of connections to the maximum of 6 and set unlimited bandwidth.

I can select a rectangle area, select the zoom levels i want included in the tile archive and click GO.
It hammers my tile server and after a while completes the creation of the tile archive - no problems.
I have no usage limits configured in my tile server.

I created my tile server using this tutorial: switch2osm*|* Building a tile server from packages.

Can you not do the same - allow MOBAC unlimited use of your mapnik rendered?

Martin.
 
Top