MapView - a work in progress!!
At last i have made some progress and have a working MapView!!
I had given up on ever developing a MapView for B4A as there just seemed to be no way to create an activity that extends the MapActivity class within B4A.
However on my internet travels i came across
OSMDroid.
osmdroid provides Tools / Views to interact with OpenStreetMap-Data.
The OpenStreetMapView is a (almost) full/free replacement for Androids MapView class.
OSMDroid can be used in a 'normal' Android Activity - it does not require that the activity extends the MapActivity class.
So after a couple of days i have a working, but very basic, MapView library.
What i shall be doing next is to learn more about the OSMDroid API so i can add more features to the library and then i'll start a new thread.
Until then i'll release this working prototype so those that are interested can have a look.
The library has just a few methods/properties:
Center() As Double
Returns the current map center as an array of two Double types.
The array elements are Latitude, Longitude.
This property is read-only.
GetTileSources As List
Returns a List of Strings which are the names of all the available TileSources which can be selected.
Values that i have found to work are "CycleMap", "Mapnik", "MapquestAerial", "MapquestOSM" and "OSMPublicTransport".
The List will contain other TileSources BUT these seem to require an API key or a licence to access those tiles.
The default TileSource is "Mapnik".
setCenter (Latitude As Double, Longitude As Double)
Set the map center.
setMultiTouchEnabled (MultiTouchEnabled As Boolean)
Enable or disable multi-touch zoom in the MapView.
setTileSource (TileSource As String)
Set the TileSource.
Valid Strings are those returned by the GetTileSources method.
setZoomEnabled (ZoomEnabled As Boolean)
Enable or disable the MapView zoom control.
Zoom As Int
Get or set the current map zoom level.
Maximum zoom level varies with the choice of TileSource.
Some TileSources have very limited zoom levels available such as "MapquestAerial"
There are no methods to add markers/overlays/polylines/polygons etc.
And here's my demo code:
Code:
'Activity module
Sub Process_Globals
'These global variables will be declared once when the application starts.
'These variables can be accessed from all modules.
Dim Latitude As Double
Dim Longitude As Double
Dim TileSource As String
Dim ZoomLevel As Int
End Sub
Sub Globals
'These global variables will be redeclared each time the activity is created.
'These variables can only be accessed from this module.
Dim MapView1 As MapView
Dim TileSourceSpinner As Spinner
End Sub
Sub Activity_Create(FirstTime As Boolean)
If FirstTime Then
Latitude=52.75610
Longitude=0.39748
TileSource="Mapnik"
ZoomLevel=12
End If
MapView1.Initialize("")
TileSourceSpinner.Initialize("TileSourceSelect")
Activity.AddView(MapView1, 0, 48dip, 100%x, 100%y-48dip)
Activity.AddView(TileSourceSpinner, 0, 0, 100%x, 48dip)
MapView1.MultiTouchEnabled=True
MapView1.ZoomEnabled=True
MapView1.Zoom=ZoomLevel
MapView1.setCenter(Latitude, Longitude)
TileSourceSpinner.AddAll(MapView1.GetTileSources)
TileSourceSpinner.Prompt="Select a TileSource"
TileSourceSpinner.SelectedIndex=TileSourceSpinner.IndexOf(TileSource)
TileSourceSelect_ItemClick(TileSourceSpinner.SelectedIndex, TileSourceSpinner.SelectedItem)
Log("Activity_Create Latitude="&Latitude&", Longitude="&Longitude&", TileSource="&TileSource&", ZoomLevel="&ZoomLevel)
End Sub
Sub Activity_Resume
End Sub
Sub Activity_Pause (UserClosed As Boolean)
If UserClosed=False Then
Dim MapCenter() As Double
MapCenter=MapView1.Center
Latitude=MapCenter(0)
Longitude=MapCenter(1)
ZoomLevel=MapView1.Zoom
Log("Activity_Pause Latitude="&Latitude&", Longitude="&Longitude&", TileSource="&TileSource&", ZoomLevel="&ZoomLevel)
End If
End Sub
Sub TileSourceSelect_ItemClick (Position As Int, Value As Object)
' Valid values (tested so far) are "CycleMap", "Mapnik", "MapquestAerial", "MapquestOSM", "OSMPublicTransport"
' "Mapnik" is the default TileSource
' Some TileSources have very limited zoom levels available such as "MapquestAerial"
' Others seem unreliable and don't always display a tile
' It may be that an API key or license is required to use some TileSources
' Look at the unfiltered Log to see related error messages
TileSource=Value
MapView1.TileSource=TileSource
End Sub
Displays a default Mapnik map centered on sunny Norfolk, UK.
A spinner allows you to select TileSource and a few process globals enable the map zoom, center and TileSource to be restored on orientation change.
Demo code and library files are attached.
Note that as well as the two normal library files OSMDroid.jar and OSMDroid.xml, there are two additional files osmdroid-android-3.0.6.jar and slf4j-android-1.6.1-RC1.jar which need to be copied to your B4A Additional libraries folder.
I'm looking forward to learning the OSMDroid API and the first thing i'll do is add support for basic markers.
Documentation for the API seems rather brief so i have no idea how long it will take to go from this prototype to a more fully featured working library.
Oh and one last thing - OSMDroid has support for offline mode.
You can download the tiles for an area, transfer them to your device's SD card and browse the map without an internet connection.
See
MobileAtlasCreator for more information.
Martin.