Android Question How to change a google map marker icon in the marker click event?

JohnC

Expert
Licensed User
Longtime User
I want to change a marker icon when the user clicks on that marker in a google maps fragment.

So, I'm assuming I would do this in the mapfrag_MarkerClicked event (as shown below).

And when looking at this post, it suggests that there should be a method to do this using the Marker object:


B4X:
//Change the marker icon
          marker.setIcon('https://www.google.com/mapfiles/marker_green.png');

But, in B4A 9.0, there doesn't seem to be such a method.

B4X:
Sub MapFrag_MarkerClick (SelectedMarker As Marker) As Boolean
    SelectedMarker.SetIcon  '<--------- no such method
End Sub

Any ideas how I can change the icon when it's clicked on?

P.S. I am using Erel's Google Maps 2.5 Lib (https://www.b4x.com/android/forum/threads/google-maps.63930/#content)
 
Last edited:

TILogistic

Expert
Licensed User
Longtime User
B4X:
Sub MapFrag_MarkerClick (SelectedMarker As Marker) As Boolean
    Dim BitmapPosition As B4XBitmap = xui.LoadBitmapResize(File.DirAssets, "my.png", 26dip, 26dip, True)
    SetMarkerIcon(SelectedMarker, BitmapPosition)
End Sub
B4X:
Public Sub SetMarkerIcon(mMarker As Marker, mIcon As B4XBitmap)
    Dim jo As JavaObject = mMarker
    Dim bitmapdescriptor As JavaObject
    bitmapdescriptor.InitializeStatic("com.google.android.gms.maps.model.BitmapDescriptorFactory")
    jo.RunMethod("setIcon", Array(bitmapdescriptor.RunMethod("fromBitmap", Array(mIcon))))
End Sub
 
Upvote 0

TILogistic

Expert
Licensed User
Longtime User
To identify the marker that the user clicked:

MarkerExtras => GoogleMapsExtras library

Sample:
B4X:
Sub MapFrag_MarkerClick (SelectedMarker As Marker) As Boolean
    Dim MarkerId As String = MarkerExtras1.GetId(SelectedMarker)
    If MarkerId = "m1" Then
        Dim BitmapPosition As B4XBitmap = xui.LoadBitmapResize(File.DirAssets, "my.png", 26dip, 26dip, True)
        SetMarkerIcon(SelectedMarker, BitmapPosition)
    End If
End Sub
 
Upvote 0

JohnC

Expert
Licensed User
Longtime User
Thank you!

It worked great :)
 
Upvote 0
Top