B4J Question Googlemap - Identify which marker is clicked

Roger Daley

Well-Known Member
Licensed User
Longtime User
Hi All

New question.

I have a map with two markers.
In googlemap_MarkerClick (SelectedMarker As Marker) event, how do I identify which of the 2 markers has been clicked? I need to do this as each marker is associated with an Image and I need to take action on this marker later in the program.

B4X:
Sub gmap_MarkerClick (SelectedMarker As Marker)
    'Set MarkerFlag to 1 or 2 depending on SelectedMarker
End Sub

Sub gmap_Click (Point As LatLng)
  'If MarkerFlag = 1 then Marker1 = Point
  'If MarkerFlag = 2 then Marker2 = point
End Sub


Regards Roger
 

Roycefer

Well-Known Member
Licensed User
Longtime User
Use the Map technique, again. When you create a Marker, put it in a Map as the key along with some meta-data as the value. Then, when you encounter that Marker object again, you can retrieve the meta-data from the Map by using that Marker as the key.
 
Upvote 0

Roycefer

Well-Known Member
Licensed User
Longtime User
Yes, exactly. I think the Tag pattern is one of the neat innovations of B4X. I wish it was implemented in more classes.
 
Upvote 0

Roger Daley

Well-Known Member
Licensed User
Longtime User
Hi All,

To round off the thread; below is the final code that I used.

Regards
Roger

B4X:
Sub gmap_MarkerClick (SelectedMarker As Marker)
    MarkerFlag = markerMap.Get(SelectedMarker)
'    Log (MarkerFlag)
    MapZoom = gmap.CameraPosition.Zoom
    MapCenter = gmap.CameraPosition.Target
    MapLat = MapCenter.Latitude
    MapLng = MapCenter.Longitude
  
    If MarkerFlag =1 Then
    'Remove BTS marker before replacing marker icon
        BTSMarker.Remove
        MarkerLocation.Initialize(BTSLat,BTSLng)
        BTSMarker = gmap.AddMarker2(10 , 10, "BASE", File.GetUri(File.DirAssets, "bts2.png"))
        BTSMarker.Position = MarkerLocation         'set BTSMarker new location
        markerMap.Put(BTSMarker, 1)
  
        LANDMarker.Remove
        MarkerLocation.Initialize(LANDLat,LANDLng)
        LANDMarker = gmap.AddMarker2(LANDLat , LANDLng, "LANDMARK", File.GetUri(File.DirAssets, "landmark2.png"))
        LANDMarker.Position = MarkerLocation         'set LANDMarker new location
        markerMap.Put(LANDMarker, 2)                 'place them both in a Map so you can retrieve Marker1's location (MarkerLocation)   
    Else
        LANDMarker.Remove
        MarkerLocation.Initialize(LANDLat,LANDLng)
        LANDMarker = gmap.AddMarker2(LANDLat , LANDLng, "LANDMARK", File.GetUri(File.DirAssets, "landmark3.png"))
        LANDMarker.Position = MarkerLocation         'set LANDMarker new location
        markerMap.Put(LANDMarker, 2)                 'place them both in a Map so you can retrieve Marker1's location (MarkerLocation)   

        BTSMarker.Remove
        MarkerLocation.Initialize(BTSLat,BTSLng)
        BTSMarker = gmap.AddMarker2(10 , 10, "BASE", File.GetUri(File.DirAssets, "bts1.png"))
        BTSMarker.Position = MarkerLocation         'set BTSMarker new location
        markerMap.Put(BTSMarker, 1)
    End If
'Force re-draw of map to remove old marker icon
    RefreshScreen
End Sub
 
Upvote 0
Top