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,809
Last edited:

entolium

Member
Licensed User
Longtime User
Hello,

I'm running all examples with overlay and always get the same error. Png not found (/resorces/org/osmdroid/...). I'm searching, reading many posts and tried hundreds of things, but is impossible, always the same error. I'm desperate.
Is possible to copy all pngs in my device? where?

Thanks a million!!!
 

Spectre

Active Member
Licensed User
Longtime User
Hi!
I do not remember how it fits an personal icon that in Maylocation overlay,
instead of the triangle of direction...
 

Spectre

Active Member
Licensed User
Longtime User
Look here: http://www.chartbundle.com/charts/
Scroll to the section named 'Mobile Atlas Creator(MOBAC)'.

You can use MOBAC to download the ChartBundle tiles on a computer and create an offline tile archive.
You can then use this offline tile archive with OSMDroid.
Look at this forum search for more info on using MOBAC:
https://www.b4x.com/android/forum/pages/results/?query=mobac&page=1&prefix=0

An offline tile archive has a few disadvantages:
  • Covering anything but a small area will result in a large (or very large) tile archive.
    Including a large tile archive with your compiled .apk is generally impractical.
    So you then need to host the tile archive online and download it to your b4a application once the application is installed.
  • If the Chartbundle data changes then your offline tiles will be out of date and display old and/or invalid data.
    You need to monitor the Chartbundle data and be sure to update your offline tile archive whenever the Chartbundle data is changed.

The Chartbundle page (linked to above) offers for download various .xml files that can be used to configure MOBAC.
Look at this one, 'Sectional Charts':

B4X:
<?xml version="1.0" encoding="UTF-8"?>
<customMapSource>
<name>Chartbundle US Sectional Charts</name>
<minZoom>4</minZoom>
<maxZoom>13</maxZoom>
<tileType>png</tileType>
<tileUpdate>None</tileUpdate>
<url>http://wms.chartbundle.com/tms/v1.0/sec/{$z}/{$x}/{$y}.png?type=google</url>
<backgroundColor>#000000</backgroundColor>
</customMapSource>

This xml tells MOBAC all it needs to know in order for it to connect to and download tiles from this tile server.
Version 4.1 of my b4a OSMDroid library has an object named OSMDroid_UrlTileSource that's used to display tiles from an online tile server.
The OSMDroid_UrlTileSource object's Initialize method is documented:



So i'm thinking that you might be able to use OSMDroid version 4.1 and it's UrlTileSource.
Get the urls of the tile servers from the various MOBAC xml configuration files and try those urls with the UrlTileSource.
With a bit of luck it'll work perfectly and display tiles on the map with no need for an offline tile archive.

Version 4.1 of OSMDroid was never properly uploaded to the forum, it's available for download from my server here:
http://b4a.martinpearman.co.uk/osmdroid/
That link should show some version 4.1 examples available to download and one example is OSMDroid_4.1_UrlTileSource.zip which you should find useful.

Version 4.1 also has better support for map rotation - i'm not sure if the problem with blank areas appearing was fixed or not.



Very nice .. I'm sorry but I did not understand how it should be the structure of the server hosting the tiles of the map!!
 

warwound

Expert
Licensed User
Longtime User
Very nice .. I'm sorry but I did not understand how it should be the structure of the server hosting the tiles of the map!!

I'm not sure i understand.
Are you hoping to bulk download the chartbundle tiles then host them on a server - then display them on an OSMDroid map using UrlTileSource?
The parameters to initialize the UrlTileSource depend entirely on the location of the tiles on your server.
 

Spectre

Active Member
Licensed User
Longtime User
I'm not sure i understand.
Are you hoping to bulk download the chartbundle tiles then host them on a server - then display them on an OSMDroid map using UrlTileSource?
The parameters to initialize the UrlTileSource depend entirely on the location of the tiles on your server.

HI! sorry my English...

I would like to load the map on my server and then distribute it to the apk.

Use as you said OSMDroid_UrlTileSource.

But I have not figured out how to be the folder structure that I have to put on the server.

To generate the tile use MapTiler (http://www.maptiler.com/).

http://www.Myserver/MAyMAps/10
11
12
10,11,12 is folder on tile png
 

warwound

Expert
Licensed User
Longtime User
Read about slippy map tile naming.
This is a common way to structure and name tiles on a server.

Now look at the UrlTileSource demo:

B4X:
Dim MyTileSource As OSMDroid_UrlTileSource
MyTileSource.Initialize(MapView1, MyTileSourceName, "http://a.tile2.opencyclemap.org/transport/%3$d/%1$d/%2$d.png", 0, 16, 256)

Finally the documentation for the UrlTileSource Initialize method:

Initialize (MapView1 As MapView, TileSourceName As String, UrlTemplate As String, MinZoomLevel As Int, MaxZoomLevel As Int, TileSizePixels As Int)
Initialize the UrlTileSource.
UrlTemplate - a String which is the Url to a tile with X, Y and zoom parameters replaced:
Here the standard OSM slippy map tilenaming is used.
http:///<domain>/%3$d/%1$d/%2$d.png Url parameters are %1$d tileX, %2$d tileY, %3$d zoom

Does MapTiler save your tiles in a folder structure that's identical to the slippy map tile naming scheme?
 

Spectre

Active Member
Licensed User
Longtime User
Ok perfect , the problem was simply in the wrong folder structure ... Thanks Warwound... PS how it fitsan personal icon that in Maylocation overlay,
instead of the triangle of direction... ?
 

warwound

Expert
Licensed User
Longtime User
PS how it fitsan personal icon that in Maylocation overlay,
instead of the triangle of direction... ?

There's no built in method to change the MyLocation overlay icon.
If you open the library file OSMDroid_4_1.jar with WinRAR or another archive manager and look in the folder /org/osmdroid/ you'll see the various built in icons used by the library.
direction_arrow.png is the icon used by MyLocation.
So if you replace direction_arrow.png in the library .jar file you should see your new icon on your map.

It's not an ideal solution but it is currently the only solution.
 

Spectre

Active Member
Licensed User
Longtime User
B4X:
Dim MyTileSourceName As String="HikeBike"
    Dim TileSourceFactory1 As OSMDroid_TileSourceFactory
        Dim MyTileSource As OSMDroid_UrlTileSource
        MyTileSource.Initialize(MapView1, MyTileSourceName, "http://a.tiles.wmflabs.org/hikebike/%3$d/%1$d/%2$d.png",0, 17, 256)
        TileSourceFactory1.AddTileSource(MyTileSource)
 
Last edited:

Spectre

Active Member
Licensed User
Longtime User
They will " melt " together the two tile to get the nuances altimeter overlaid on the map??

ikebikemap
By end of August 2014, the Wikimedia Toolserver will cease operation. This means you will need to get the tiles from a different location, which is

http://{a,b,c}.tiles.wmflabs.org/hikebike/{z}/{x}/{y}.png

Example: http://a.tiles.wmflabs.org/hikebike/12/2208/1372.png

These tiles will look slightly different than the old ones but are hopefully missing no important features.

hillshading
By end of August 2014, the Wikimedia Toolserver will cease operation. This means you will need to get the hillshading tiles from a different location, which is

http://{a,b,c}.tiles.wmflabs.org/hillshading/{z}/{x}/{y}.png

Example: http://a.tiles.wmflabs.org/hillshading/12/2208/1372.png

These tiles are unchanged from the ones that were hosted on the Wikimedia Toolserver.
 

jndaniels1

Member
Licensed User
Longtime User
Greetings Warwound, Spectre
(And other osm guru's)


Does offline tile caching work in the same manner as in the previous v3 library? Creating a osmdroid.zip or osmdroid.sqlite tile cache with
MOBAC and placing it in the ext stroage /osmdroid ??

I hope this isn't a repeated question; I did search before posting.

I got both zip and sqlite working successfully with the previous warwound code example "11 - OfflineMaps" using sqlite (with pretty much ease).
Zip Vs Sqlite .. Sqlite is way faster!


Thanks warwound for a awesome library! And all who help!

Kind Regards
J Daniels
 

warwound

Expert
Licensed User
Longtime User
Does offline tile caching work in the same manner as in the previous v3 library?

Yes there's no difference in operation between v3.0.8 and v4.1.
There's syntax differences between the 2 versions but the underlying features remain unchanged.
 

anglia

Member
Licensed User
Longtime User
I'm fairly new to B4a and I'm very interested in the excellent work Martin has done developing this library.
I've read most of the posts in this thread, but I'm having trouble understanding the use of offline maps for 'non-geographical' applications.

Back in post #12, Martin mentioned that the library could possibly be used for circuit diagrams, floor plans etc.
I've got a large chart which I have cut into 800 png tiles (40x20 each 256x256 pixels).

I am reasonably familiar with the way that the zoom factor and 'slippy tiles' work, but I can't see how to use them as there are no geopoints, latitudes, longitudes involved.
Could someone point me in the right direction please?

Thanks
 

warwound

Expert
Licensed User
Longtime User
@anglia

Displaying a non geographical image in a MapView and then referencing cartesian (x, y) coordinates on the image requires you to be able to convert those cartesian coordinates into geographical (latitude, longitude) coordinates and vice versa.

OSMDroid uses the WGS84 geographical coordinate system.

Which version of OSMDroid are you using?
There are 2 versions available: 3.0.8 and 4.1.
I never had time to properly upload version 4.1 to the forum but it is available from my website: http://b4a.martinpearman.co.uk/osmdroid/.

To convert between cartesian and geographical coordinates you need to use the OSMDroid Projection object.
The Projection object was never included in version 3.0.8 but is included in version 4.1.
Look at this page: http://b4a.martinpearman.co.uk/osmdroid/OSMDroid_4_1.html, search the page for OSMDroid_Projection and you'll see there are various methods to make these coordinate conversions.
 

anglia

Member
Licensed User
Longtime User
Thanks Warwound.

I'll look into the OSMDroid Projection object and give it a go.

I'm using version 3.0.8 (v 3.52). I know that version is 4.1 is available and I have downloaded it (version 4.1 (0.04) and tried to use it . However, when I use v4.1 and try to compile your example code for '11 - Offline mapsupdated' - (post #12 of this thread), I get the following error message-

B4A version: 5.20
Parsing code. Error
Error parsing program.
Error description: Unknown type: geopoint
Are you missing a library reference?
Occurred on line: 12 (Main)
Dim MapCenter As GeoPoint

If I go back to using version 3.08, the code compiles and runs ok.

Have I missed some modification I need to make so that I can use v4?

Thanks again
 

warwound

Expert
Licensed User
Longtime User
@anglia

The object names in version 4.1 are all prefixed with OSMDroid_.
Version 3.0.8's GeoPoint object is named OSMDroid_GeoPoint in version 4.1.
 
Top