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:

warwound

Expert
Licensed User
Longtime User
To comply with the terms and conditions of the licenses just display the 2 licenses 'somewhere' in your app.
So displaying the 2 licenses in a WebView means you're complying with the terms and conditions of the licenses.
That's all you have to do.

There's no need to contact anyone to get written or verbal permission.
 

GiovanniPolese

Well-Known Member
Licensed User
Longtime User
Hi to all.
Warwound library is quite interesting and works well with online maps. Now, perhaps with a long delay, I am examining the tutorials, namely that on off-line maps. Probably I missed something, but using Mobac seems not aligned with the tutorial explanations. For example I don't find MapNik in the list of atlases. MapNik doen't exist at the indicated link. I did some "blind" trials using OSMDroid zip Atlas format, but the resulting file, despite being added to Files folder, is not added at compile time (b4a explicitly says "test.zip is not added to your project" in the log.. but the file is there). I did other trials, but, in such conditions it is a nonsense to go on. Can anybody tell me what I am missing? Thanks in advance.
 

warwound

Expert
Licensed User
Longtime User
b4a explicitly says "test.zip is not added to your project" in the log.. but the file is there

I wonder why that is...
Is test.zip a very large file?
Is it too big to be compiled into your apk?
A quick Google suggests that an .apk file is limited to a maximum of 50MBs.
 

GiovanniPolese

Well-Known Member
Licensed User
Longtime User
Hi. It should not be large. In fact I did just one 5° tile, at same resolutions of your example. I will redo the procedure, perhaps problems on my computer (to day i have strange very delayed answers on some win10 OS commands; some files seem to be in a folder (with b4a files folder sync) but they could have been erased, and Windows Waits minutes to answer). I will confirm this fact and, in case, put here the zip file. The project is exactly yours, with just the Center and the tile provider and filename string changed. Anyway, what about MapNik? To obtain the zip I used OSMDroid zip format. Is it correct? Moreover, you mention several strings for the tile providers.. I could be wrong, but none of them appears in the Mobac list of atlases. At least things are very obscure in this aspect. Thanks for your attention..
 

GiovanniPolese

Well-Known Member
Licensed User
Longtime User
Hi. No, the problem of the file doesn't exist. It was due to something else. Now the app displays a white screen with a grid. This could be due to the map Center. I retrieved it from GoogleEarth, with decimal angles. A problem could be here and I must check. Forget about. Just give me, if possible, some hints on the maps providers listed by Mobac. Thanks.
 

GiovanniPolese

Well-Known Member
Licensed User
Longtime User
Final test. Used Atlas for selecting a tile. (See Atlas.png). In the center there is the town of "Spresiano". Went to GoogleEarth to find the coordinates of Spresiano (see google_earth_position.png). Modified the basic example (see b4a_changes.png). Running the example, a tile near the south-east limits of England appears. Probably the center is not a problem, because the tile appears on top-left screen, as perhaps should be. I cannot attach the zip file which si slightly more than 1100 kb. Thanks anyway.
 

Attachments

  • Atlas.png
    Atlas.png
    396.8 KB · Views: 530
  • google_earth_position.png
    google_earth_position.png
    346.8 KB · Views: 534
  • b4a_changes.png
    b4a_changes.png
    92 KB · Views: 501

BW17

Member
Licensed User
Longtime User
Hi to all

My app use OsmDroid 3.08 and works fine.
But i wish use OsmDroid 4.01

I use offline tile with XYTileSource in OsmDroid 3.08. as this :
B4X:
    Dim MyXYTileSource As XYTileSource

        MyXYTileSource.Initialize("my_offline_tile", 4, 15, 256, ".png",  "http://localhost")
myMap1.AddXYTileSource(MyXYTileSource)

I'm looking for the same thing under 4.01 but can't find it.

Could you help me please ?

(sorry for my very bad english).
 

MarcoRome

Expert
Licensed User
Longtime User
Hi Worwound and thank you for your great work.
Right now I have made a donation ( 88W96424XV693531T ).
I have two questions.
1) I found a lot example for OSM 3 but few for OSM 4.
2) Is possibile a example about lines, polylines, etc.
Thank you again for your great work
Bye
Marco
 

warwound

Expert
Licensed User
Longtime User
@MarcoRome

I have no examples for lines and polylines on this computer.
Can you explain what you are trying to draw and i'll try to help.
 

MarcoRome

Expert
Licensed User
Longtime User
Hi Warwound and thank you for your time.
Example this:

Immagineosm.jpg



Another question, do you have a few examples ( general ) that work in OSM Rel. 4 ?

Thank you again.
Regards
Marco
 

warwound

Expert
Licensed User
Longtime User
Look here for some version 4.1 examples: http://b4a.martinpearman.co.uk/osmdroid/
  • OSMDroid_4.1_CircleOverlay.zip
  • OSMDroid_4.1_UrlTileSource.zip
  • OSMDroid_v4.1_examples.zip

I've just uploaded the CircleOverlay example - found it on this computer but have not re-tested it - i'm assuming it still works.

Try those examples and let me know if you still need a polygon example.
You might also wanna read through the version 4.1 reference page: http://b4a.martinpearman.co.uk/osmdroid/OSMDroid_4_1.html
 

sasidhar

Active Member
Licensed User
Longtime User
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.


Hi,

In designer I have added Customview and named it as MapView1, But customYpe I could not see anything, though i have added OSM library in reference.
please let me know, which control I have to add. while loading layout it is giving error.

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.LoadLayout("1")
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=15
MapView1.SetCenter(55.3286,25.2153)
End Sub
 

warwound

Expert
Licensed User
Longtime User
The OSMDroid MapView was never updated to be used in the Designer as a CustomView.
A MapView can only be created and added to an Activity using code.
 

MichalK73

Well-Known Member
Licensed User
Longtime User
Hello.

How do I add or change other basic maps with OSM, is standardly Mapnik?
In the same way whether it is possible to add overlays to the map. For example, I have an overlay ortophotomap Polish for my country and I would like it to impose on the base map.

Example map from Poland
 

StefanoAccorsi

Member
Licensed User
Longtime User
Hi. First of all thank you @warwound for this great library.

I've got a question. I need to draw a circle overlay but with translucency. I read that it is possible to set an alpha value in a pathoverlay, but I didn't find the same method in circleoverlay or other overlays.

I read a similar question from @MarcoRome up in this page but I didn't find anything useful in the examples. Thank you so much.
 

Ydm

Active Member
Licensed User
Longtime User
Did you Zoom Level maximum 11? Should I upgrade?

MapView1.Initialize("MapView1")
MapView1.SetZoomEnabled(True)
MapView1.SetMultiTouchEnabled(True)
MyLocationOverlay1.Initialize(MapView1, "MyLocationOverlay1")
SimpleLocationOverlay1.Initialize(MapView1)
MapCenter=MapView1.GetCenter

Log(MapView1.GetMaxZoomLevel) >>>>>>>>>>>>>> 11
 

Ydm

Active Member
Licensed User
Longtime User
I wrote everything right application.
This did not solve the problem of zoom. I looked at the examples 14-20 are using, such as zoom level. Bente maximum zoom 11.
I would appreciate if you can help ...
 

PCowling

Member
Licensed User
Longtime User
Hello

I have a question when using the 4.01 framework.

I have written an app (well you have, I needed your code to help!!) This takes tiles from the Thunderforest tiles on their hobby license.

I have limited the zoom so that the user can not zoom past level 13, however I would when this is hit allow the user to stretch in effect the tile. This is so that we do not zoom in any more (as this resizes and the names and map features) but so that they also become larger and allowing some spacial context to be kept on the screen.

I have seen an demo you wrote where the comments said that this would happen when limiting the zoom level, however on mine it just stops pinch zooming. As does the demo when I compile and run that.

Thanks to anyone with any help.

Phil
 
Top