AIM: Add a MarkersOverlay layer to the MapView.
With MarkersOverlay you can add
Marker objects to your MapView.
If you use the default Marker icon, then MarkersOverlay requires the resources in the drawable-nodpi folder that is included with the library download.
Copy the drawable-nodpi to your project's Objects/res folder and ensure that all files in that folder are set to read-only
MarkersOverlay generates two events:
Click and
LongClick.
A Marker object is initialized:
Code:
Dim Marker1 As Marker
Marker1.Initialize("A title", "A description", aLatitude, aLongitude, anIcon)
When a Marker is Clicked or LongClicked, the title, description and position of the Marker are pass as parameters to the event listener.
The position is passed as a
GeoPoint object.
MarkersOverlay leaves the developer to take action when a Marker is Clicked or LongClicked - it does nothing more than generate the events.
Here's the example code:
Code:
Sub Process_Globals
Dim MapCenter As GeoPoint
Dim TileSource As String
Dim ZoomLevel As Int
End Sub
Sub Globals
Dim MapView1 As MapView
Dim MinimapOverlay1 As MinimapOverlay
Dim ScaleBarOverlay1 As ScaleBarOverlay
Dim TileSourceSpinner As Spinner
' create the MarkersOverlay
Dim MarkersOverlay1 As MarkersOverlay
End Sub
Sub Activity_Create(FirstTime As Boolean)
' update the MenuItems
Activity.AddMenuItem("Fit map to markers", "MenuItemSelect")
' MapView initialized with no EventName as we'll not be listening for MapView events
MapView1.Initialize("")
Activity.AddView(MapView1, 0, 48dip, 100%x, 100%y-48dip)
MapView1.SetMultiTouchEnabled(True)
MapView1.SetZoomEnabled(True)
' initialize the MarkersOverlay and add it to the MapView
' an EventName is required as we will listen for the two events that MarkersOverlay generates
MarkersOverlay1.Initialize(MapView1, "MarkersOverlay1")
MapView1.AddOverlay(MarkersOverlay1)
' create and initialize 2 Markers
' for Marker1 i'll use a custom icon
Dim Icon As BitmapDrawable
Icon.Initialize(LoadBitmap(File.DirAssets, "my_icon.png"))
Dim Marker1 As Marker
Marker1.Initialize("Home sweet home", "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Morbi egestas suscipit convallis. Etiam pellentesque gravida est, quis luctus nunc commodo at. Nam et risus orci. Integer malesuada lorem dolor. Maecenas vestibulum cursus enim, tincidunt luctus libero placerat non. In vitae metus tellus, nec euismod nibh. Phasellus ut quam vitae justo sagittis auctor. Sed vel sapien dolor. Etiam ut sem id dolor iaculis ullamcorper. Aenean eget sem nibh, a tempor augue. Nulla interdum luctus molestie.", 52.75610, 0.39748, Icon)
' Marker2 will display the default OSMDroid icon
' the default icon is used if Null is passed as the Icon parameter
Dim Marker2 As Marker
Marker2.Initialize("Elsewhere", "Downham Market", 52.60801, 0.39047, Null)
' create a List and initialize it with the 2 Markers
Dim Markers As List
' ** example code updated **
' Markers.Initialize2(Array As Marker(Marker1, Marker2)) ' this is the previous code
Markers.Initialize2(Array As Object(Marker1, Marker2)) ' this is the new code
' add the List of Markers to the MarkersOverlay
MarkersOverlay1.AddMarkers(Markers)
If FirstTime Then
TileSource="Mapnik"
' fit the MapView to the MarkersOverlay
MapView1.FitMapToBoundingBox(MarkersOverlay1.GetBoundingBox)
Else
' restore saved zoom level and map center
MapView1.Zoom=ZoomLevel
MapView1.SetCenter3(MapCenter)
End If
ScaleBarOverlay1.Initialize(MapView1)
MapView1.AddOverlay(ScaleBarOverlay1)
' ensure that the MinimapOverlay is the LAST overlay added to the MapView
MinimapOverlay1.Initialize(MapView1)
MapView1.AddOverlay(MinimapOverlay1)
TileSourceSpinner.Initialize("TileSourceSelect")
Activity.AddView(TileSourceSpinner, 0, 0, 100%x, 48dip)
TileSourceSpinner.AddAll(MapView1.GetTileSources)
TileSourceSpinner.Prompt="Select a TileSource"
TileSourceSpinner.SelectedIndex=TileSourceSpinner.IndexOf(TileSource)
TileSourceSelect_ItemClick(TileSourceSpinner.SelectedIndex, TileSourceSpinner.SelectedItem)
End Sub
Sub Activity_Resume
End Sub
Sub Activity_Pause (UserClosed As Boolean)
MapCenter=MapView1.GetCenter
TileSource=MapView1.GetTileSource
ZoomLevel=MapView1.Zoom
End Sub
' two event listeners listen for the MarkersOverlay's two events
Sub MarkersOverlay1_Click(Title As String, Description As String, Point As GeoPoint)
Log("MarkersOverlay1_Click")
ToastMessageShow(Title&": "&Description, True)
End Sub
Sub MarkersOverlay1_LongClick(Title As String, Description As String, Point As GeoPoint)
Log("MarkersOverlay1_LongClick")
ToastMessageShow(Title&": Latitude="&Point.Latitude&", Longitude="&Point.Longitude, True)
' zoom the map in and center (with animation) on the LongClicked Marker
MapView1.Zoom=MapView1.GetMaxZoomLevel-1
MapView1.AnimateTo3(Point)
End Sub
Sub MenuItemSelect_Click
Dim MenuItem As String
MenuItem=Sender
Select MenuItem
Case "Fit map to markers"
' fit the MapView to the MarkersOverlay
MapView1.FitMapToBoundingBox(MarkersOverlay1.GetBoundingBox)
End Select
End Sub
Sub TileSourceSelect_ItemClick (Position As Int, Value As Object)
MapView1.SetTileSource(Value)
MinimapOverlay1.SetTileSource(Value)
End Sub
A MarkersOverlay is created, initialized and added to the MapView.
Two Markers are created and initialized.
The first Marker is initialized with a custom icon, the second Marker is initialized with
Null passed as it's icon so the second Marker will display the default OSMDroid icon.
MarkerOverlay has a single method to add Markers -
AddMarkers (Markers As List).
So you must pass this method a List of Marker objects.
If this is the first time that the activity has been created then the MapView
FitMapToBoundingBox method is called passing the bounding box returned by the MarkersOverlay
GetBoundingBox method.
The map center and zoom level are adjusted to fully display all Markers on the MarkersOverlay.
If this is not the first time that the activity has been created then the previously saved map center and zoom level are restored.
In Activity_Pause the map center and zoom level are saved.
A Click on a Marker will display a toast message with the Marker's Title and Description.
A LongClick on a Marker will display a toast message with the Marker's Title, Latitude and Longitude.
It will also zoom the map in to it's maximum zoom level minus one and center on the Marker's position using the MapView
AnimateTo methid.
The animation will likely not be noticeable unless the tiles for the zoomed in area have already been cached.
MarkersOverlay and MarkersFocusOverlay (which i shall cover next) are both functional but lacking in features i think.
You cannot change a Marker's position for example, and if you add Markers and have previously added Markers then the previous Markers are replaced not added to.
Another point to note is how difficult it is to Click or LongClick a small icon.
The default OSMDroid icon seems to small to me, the custom icon i have used is much easier to Click or LongClick.
I shall look at creating a more feature packed MarkersOverlay and MarkersFocusOverlay over the next week or so.
Anyway, on to the last example - the MarkersFocusOverlay.
Martin.