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.
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.
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
Last edited: