Android Tutorial OSMDroid - MapView for B4A tutorial

You can find the OSMDroid library thread here: http://www.b4x.com/forum/additional...tes/16309-osmdroid-mapview-b4a.html#post92643.

AIM: Create and initialize a MapView, enable the map zoom controller and multitouch controller, set a zoom level then center the map on a location.

B4X:
Sub Process_Globals
End Sub

Sub Globals
   Dim MapView1 As MapView
End Sub

Sub Activity_Create(FirstTime As Boolean)
   If File.ExternalWritable=False Then
      '   OSMDroid requires the use of external storage to cache tiles
      '   if no external storage is available then the MapView will display no tiles
      Log("WARNING NO EXTERNAL STORAGE AVAILABLE")
   End If
   
   '   no EventName is required as we don't need to listen for MapView events
   MapView1.Initialize("")
   Activity.AddView(MapView1, 0, 0, 100%x, 100%y)
   
   '   by default the map will zoom in on a double tap and also be draggable - no other user interface features are enabled
   
   '   enable the built in zoom controller - the map can now be zoomed in and out
   MapView1.SetZoomEnabled(True)
   
   '   enable the built in multi touch controller - the map can now be 'pinch zoomed'
   MapView1.SetMultiTouchEnabled(True)
   
   '   set the zoom level BEFORE the center (otherwise unpredictable map center may be set)
   MapView1.Zoom=14
   MapView1.SetCenter(52.75192, 0.40505)
End Sub

Sub Activity_Resume
End Sub

Sub Activity_Pause (UserClosed As Boolean)
End Sub

The code is pretty self-explanatory.

I've added the code to check if the device has available external storage as OSMDroid will not display any tiles if no external storage is available.
External storage is used to save/cache all downloaded tiles - no external storage means no map!
(I'll omit that check from future tutorials but it's something to bear in mind - not that i know of any Android devices that have no external storage).

Create and initialize a MapView, add it to the Activity 100% width and 100% height.
Enable the zoom and multi-touch controller.
Zoom in to level 14 then set the MapView center to a location (sunny Norfolk, UK!).

I've found that setting the map center and then immediately setting the zoom level does not work as expected.
I think that while the MapView is setting the map center it also zooms in so the end result is unpredictable.

Pan and zoom the map, now rotate your device and you'll see the map returns to it's initial state of zoom level 14, center (52.75192, 0.40505).

I shall show you how to save and restore the MapView state next...

Martin.
 

Attachments

  • 01 - SimpleMap.zip
    5.8 KB · Views: 4,624
Last edited:

ValDog

Active Member
Licensed User
Longtime User
Hi abdula,

We've discussed this thuroughly in the previous posts in this thead.

You'll need to read Martins comments on the legalities of using Goodle Maps.

Then you have to create your own Google Maps Tiles.zip.
---

I used Mobile Atlas Creator v1.8 for creating Google Maps tiles.

Kinda a pain as the more tiles you create the bigger the zip.
And the longer it takes.

Copilot Live has a 3 gig file for maps for Australia.

So I limited my tiles creation to only the area in which I would likely travel
on a daily basis thruought the year.

Then if I were to take a holiday I would generate tiles for that area under a differnet zip file and pick item in my app.

Then if I had to go somewhere I didn't have tiles for I'd just use Google Maps Apps for the interum.
---

Then update your apps script to use your tiles.zip.
Also, discussed in the previous posts of this thread.

hth


Barry,

You mention that you used Mobile Atlas Creator v1.8 for creating Google Maps tiles. Could I ask just how you did that?

-- Val
 

canalrun

Well-Known Member
Licensed User
Longtime User
Added Later:
I didn't initially see that you mentioned Google Maps. I may have asked about Google Maps, but I have never created data with or for use with Google Maps.


Hello,
I hope I'm the correct "Barry" :D

I switched to using Maperiitive, but yes I have used Mobile Atlas Creator. I see version 1.9.8 on my computer, but I may have upgraded since the post you referred to.

Part of the help file associated with one of my apps walks through the process of using Maperiitive, http://android.canalrun.com/#gpstrak (scroll down a little till you see "Acquiring off-line map data"). Mobile Atlas Creator is sort of similar.

With Mobile Atlas Creator I remember there were few tricks: In the Map Source drop-down I had to select "OpenStreetMap Mapnik", in the Zoom Levels check box array the zoom levels for which you want to create tiles must be checked – the higher zoom levels produce tons and tons of data and take a relatively long time to create, there is a tool to highlight the region for map creation on the map (the region becomes highlighted in pink) that must be used before trying to create map tles, and I think I remember having to zoom in and out initially to move the map display to show the region of interest.

After the tile files were created I had to move them into a zip file then copy to my device. I used FTP to copy to my device.

There is a very good tutorial from Warwound (Martin) for creating map data. This is what I initially followed. If you have any specific questions I would be happy to answer.

Barry.
 
Last edited:

monki

Active Member
Licensed User
Longtime User
Hello Martin,
I have a problem with your OSMDroid libary.Ich Use in one app, the Google Map lib and your OSM LIB.Unfortunately is a conflict with the marker type its exists in both Google Maps and in OSM.is it possible to change the name to "OSMMarker......" ??

Greeting monki
 

warwound

Expert
Licensed User
Longtime User
I've recompiled the library so that the OSMDroid Marker object is now the OSMDroid_Marker object.
I shan't make any changes to the 'official' version of the library but hopefully this re-compiled version will be compatible with the Google Map library.

Can you give it a test?

Martin.
 

Attachments

  • OSMDroid_Marker.zip
    342.1 KB · Views: 557

monki

Active Member
Licensed User
Longtime User
HI Martin,

Many thanks for your quick Response.
The new osmdroid Lib works great with Google Maps.(no Conflikt)
many many thanks.

monki

:):):):):)
 

mjas

Member
Licensed User
Longtime User
HI Martin,

first of all this is a great job!
Many thanks.

And Monki said:
The new osmdroid Lib works great with Google Maps.(no Conflikt)

Can you, please, tell how do you show Google Maps with this?

Thanks,
Mjas
 

warwound

Expert
Licensed User
Longtime User
Look at post #224.

The OSMDroid library and GoogleMap library both have an object named Marker - that means you cannot use both libraries in a single b4a application.
There would be a conflict and i'm not sure what would happen.
Whether the application would fail to compile or whether it would compile but one of the libraries would fail i am not sure.

So i recompiled OSMDroid and changed the name of the OSMDroid Marker object to OSMDroid_Marker, that modified version of OSMDroid library is attached to post #225.
With the modified version of OSMDroid you can now use both OSMDroid and GoogleMap libraries in a single b4a application.

Martin.
 

mjas

Member
Licensed User
Longtime User
Look at post #224.

The OSMDroid library and GoogleMap library both have an object named Marker - that means you cannot use both libraries in a single b4a application.
There would be a conflict and i'm not sure what would happen.
Whether the application would fail to compile or whether it would compile but one of the libraries would fail i am not sure.

So i recompiled OSMDroid and changed the name of the OSMDroid Marker object to OSMDroid_Marker, that modified version of OSMDroid library is attached to post #225.
With the modified version of OSMDroid you can now use both OSMDroid and GoogleMap libraries in a single b4a application.

Martin.

Thank you for your answer Martin,

I thought that would be possible use Mobile Atlas Creator, or some program like that, to create the map tiles.
By the way, I tried Mobile Atlas Creator to create the tiles for sqlite and it works fine, it grabs the tiles in "blob" format".

And again congratulations for this great library.
Manuel
 

ValDog

Active Member
Licensed User
Longtime User
Referring to the Offline maps discussions and example code, I would like to be able to select a map file from a number of map files I may have in the OSMDroid directory - and then back out of the map, change my map selection to another map and reload the map display again. I've not had success in changing the map selection so far and can't figure out which method or function actually determines the map that is to be shown. Can anyone shed any light on this?


Update: I'm beginning to think that the OSMDroid cache folder should only contain one map file at a time - the one that is selected. If so, I guess I would need to store my map files elsewhere, and transfer them to the cache folder as needed. Is this correct?
 
Last edited:

warwound

Expert
Licensed User
Longtime User
When you say a 'map file' i presume you mean a .zip archive of tiles?
(An offline tile archive).

Any such offline tile archive that exists in the folder osmdroid on the device's external memory is available as a TileSource.
The name of the TileSource is the name of the root folder in the offline tile archive.

So if you have two offline tile archives in the osmdroid folder you can select either as the TileSource:

B4X:
MapView1.SetTileSource("RootFolder")

Where RootFolder is the name of the root folder in the archive.

If you look at this example you'll see that the root folder in the offline tile archive offline_tile_cache_mapnik.zip is named Mapnik and MapView1.SetTileSource("Mapnik") is all that's required to select that archive.

If you have more than one offline tile archive in the osmdroid folder and the root folders in each archive do not have unique names then you might have problems - but i'd expect it to work as long as each archive contains a root folder with a unique name.

Martin.
 

ValDog

Active Member
Licensed User
Longtime User
When you say a 'map file' i presume you mean a .zip archive of tiles?
(An offline tile archive).

Any such offline tile archive that exists in the folder osmdroid on the device's external memory is available as a TileSource.
The name of the TileSource is the name of the root folder in the offline tile archive.

So if you have two offline tile archives in the osmdroid folder you can select either as the TileSource:

B4X:
MapView1.SetTileSource("RootFolder")

Where RootFolder is the name of the root folder in the archive.

If you look at this example you'll see that the root folder in the offline tile archive offline_tile_cache_mapnik.zip is named Mapnik and MapView1.SetTileSource("Mapnik") is all that's required to select that archive.

If you have more than one offline tile archive in the osmdroid folder and the root folders in each archive do not have unique names then you might have problems - but i'd expect it to work as long as each archive contains a root folder with a unique name.

Martin.


So Martin, I'm a bit confused. I created my tile archive file (zip file) using Maperitive, and the only folders in the file are titled corresponding to the zoom levels I selected - like 18, 17, 16, 15,14... Do I need to create another zip file with a (uniquely-named) root folder and add my current folder under that root folder?


Update: OK, I did exactly what I described above - and got an error message saying that the tile source was not recognized. My zip file is named VM_Home.zip, with a folder within it named VM_Home and subfolders are the zoom level folders discussed previously. The statement that crashes is "MapView1.SetTileSource("VM_Home")" - and the zip file is definitely in the osmdroid directory on my android. Now what?
 
Last edited:

warwound

Expert
Licensed User
Longtime User
Have you had any luck whatsoever getting a tile archive created using Maperitive to work with OSMDroid?

MobileAtlasCreator is the recommended tool to use to create offline tile archives.

This page states:
You can use 'Maperitive' (http://maperitive.net/)

It works in much the same way, but you'll have to package the generated map tiles into a .zip file manually to use them with osmdroid.

So it looks as if you have an additional step to perform in order to use the Maperitive tile archive.

Martin.
 

ValDog

Active Member
Licensed User
Longtime User
Have you had any luck whatsoever getting a tile archive created using Maperitive to work with OSMDroid?

MobileAtlasCreator is the recommended tool to use to create offline tile archives.

This page states:


So it looks as if you have an additional step to perform in order to use the Maperitive tile archive.

Martin.



Martin,

Yes, I have been able to create tile archives using Maperitive that run under OSMDroid. Maperitive is just fine for creating the map tile archives, and you are correct - one has to create the tile archive file manually. My limited testing shows that the root directory, whether created using MobileAtlasCreator or manually with Maperitive, needs to be named not just with a unique name (as indicated in one of your postings) but with a valid tile source name - like "Mapnik." So, it seems that keeping multiple archives in the osmdroid subdirectory (for offline use) that are all created using the same tile source, say Mapnik, would be problematic - since they would all have Mapnik as the root directory. I'm still mulling over how to make this work...
 

ValDog

Active Member
Licensed User
Longtime User
Added Later:
I didn't initially see that you mentioned Google Maps. I may have asked about Google Maps, but I have never created data with or for use with Google Maps.


Hello,
I hope I'm the correct "Barry" :D

I switched to using Maperiitive, but yes I have used Mobile Atlas Creator. I see version 1.9.8 on my computer, but I may have upgraded since the post you referred to.

Part of the help file associated with one of my apps walks through the process of using Maperiitive, http://android.canalrun.com/#gpstrak (scroll down a little till you see "Acquiring off-line map data"). Mobile Atlas Creator is sort of similar.

With Mobile Atlas Creator I remember there were few tricks: In the Map Source drop-down I had to select "OpenStreetMap Mapnik", in the Zoom Levels check box array the zoom levels for which you want to create tiles must be checked – the higher zoom levels produce tons and tons of data and take a relatively long time to create, there is a tool to highlight the region for map creation on the map (the region becomes highlighted in pink) that must be used before trying to create map tles, and I think I remember having to zoom in and out initially to move the map display to show the region of interest.

After the tile files were created I had to move them into a zip file then copy to my device. I used FTP to copy to my device.

There is a very good tutorial from Warwound (Martin) for creating map data. This is what I initially followed. If you have any specific questions I would be happy to answer.

Barry.


Barry,

Take a look at this thread. I've been discussing with Martin how to have multiple offline maps and be able to select one for use. What has been your experience?
 

merlin2049er

Well-Known Member
Licensed User
Longtime User
Can you add the mapview in the designer? I've got a couple of tabs I want to use (using tabhost), and the offline maps are on one of those tabs.
 

merlin2049er

Well-Known Member
Licensed User
Longtime User
Barry,

Take a look at this thread. I've been discussing with Martin how to have multiple offline maps and be able to select one for use. What has been your experience?

Working on the same type of app, where I'm selecting from a spinner the map to load (from sqlite table) which will have the route, name of the offline mapnik files (in zip) format, description of the map (or route) - I then have a secondary table with the routes and POI interests.
 

synapse

Member
Licensed User
Longtime User
Martin, In your Overlaymap example you use a spinner to select the tile source. You manage to achieve the same effect - white background, black text - for different versions of android something I and others have had trouble with in other applications. We get varied dark or light backgrounds on the drop down. I wonder if you are prepared to share your secret please? The code I could see in Overlaymap seemed perfectly standard....Many thanks
 

warwound

Expert
Licensed User
Longtime User
Lols there's no secret...

I wrote those examples a while ago and i think what you're looking for is what's contained in the manifest of the examples.
In earlier versions of B4A the default targetSDK (or other attributes) in the manifest were different.

Open a new b4a project, save it and open the manifest editor.

Now also open the OverlayMap example and look at the manifest editor.

See the difference?
I'm sure if you use the older manifest values in a new b4a project you will get the Spinner style you seek.

Martin.
 

davelew1s

Active Member
Licensed User
Longtime User
I am trying to place a marker on a map... i have search this tutorial but didn't find any info ,but there's so much i may have missed it.
I want to place a marker so that the cemter of the icon is on the lat and long supplied:-
Marker1.Initialize("... lat,lon,icon)at the moment it places the icon with the bottom center on the lat/lon. Is there anyway to change this?
I've tried a - lat offset which works but only in one zoom level....any ideas?
Thanks Dave.
 
Top