Android Question Google maps marker

Almora

Active Member
Licensed User
Longtime User
How can i do just a marker.
I'm using FusedLocationProvider


B4X:
        Public m1 As Marker
        m1=gmap.AddMarker2(Location1.Latitude,Location1.Longitude,"Lon: " &Location1.Longitude, 200 )
        m1.Snippet="Lat: " & Location1.Latitude
        m1.InfoWindowShown=True
       
        Dim cp As CameraPosition
        cp.Initialize(Location1.Latitude, Location1.Longitude, 14)
        gmap.AnimateCamera(cp)
 

Attachments

  • Screenshot_2017-05-18-19-16-01-1.jpg
    Screenshot_2017-05-18-19-16-01-1.jpg
    363.4 KB · Views: 377

DonManfred

Expert
Licensed User
Longtime User
Why are you adding more than one Marker then?

1. Get the map
2. Add a Marker and remember it´s object (global var for ex)
B4X:
Dim mymarker As Marker ' Global var
    mymarker = gmap.AddMarker(-8.841878,-75.750383, "") ' get the markerobject

inside your new location event you update the marker with
B4X:
    mymarker.Position = newposition
 
Upvote 0

Almora

Active Member
Licensed User
Longtime User
B4X:
  Dim LaLo As LatLng
LaLo.Initialize(location1.Latitude,location1.Longitude)
m1.Position = LaLo

I used this code. But again the same. Many markers appear.
 

Attachments

  • SmartSelectImage_2017-05-19-13-29-25-1.png
    SmartSelectImage_2017-05-19-13-29-25-1.png
    364.6 KB · Views: 277
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
You are STILL adding multiple markers.... Add the MARKER the FIRST TIME. After that just use the markerobject.
B4X:
Sub FusedLocationProvider1_LocationChanged(Location1 As Location)
    Log("FusedLocationProvider1_LocationChanged")
    LastLocation=Location1

    If gmap.IsInitialized = False Then
        ToastMessageShow("Error initializing map.", True)
    Else  
                          
        m1=gmap.AddMarker2(Location1.Latitude,Location1.Longitude,"Lon: " &Location1.Longitude, 200 )

Add the marker here once....
B4X:
Sub MapFragment1_Ready
   
    gmap = MapFragment1.GetMap
    m1=gmap.AddMarker2(15,15,"Lon: " &15, 200 )

End Sub

Then in you location changed you just need to update the known marker.
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
I´ve revisited your project.
A few remarks:
- You don´t need Runtimepermissions if you set targetsdk to 19 like you did.
- As i wrote above; you need to create one marker and reuse the markerobject to change it´s position.

B4X:
Sub MapFragment1_Ready
    Log($"MapFragment1_Ready()"$)
    gmap = MapFragment1.GetMap
    m1=gmap.AddMarker2(15,15,"DummyLocation", 200 )
    Log($"Marker set at dummy-position..."$)
End Sub

and then - in your locationchanged even - you set the new Location to the marker

B4X:
Sub FusedLocationProvider1_LocationChanged(Location1 As Location)
    Log($"FusedLocationProvider1_LocationChanged(GMap initialized = ${gmap.IsInitialized})"$)
    LastLocation=Location1
    If gmap.IsInitialized = False Then
        ToastMessageShow("Error initializing map.", True)
    Else
        Log($"Changing marker-position to Lat: ${Location1.Latitude}, Lon: ${Location1.Longitude}"$)
        m1.Title = "Location"
        m1.Snippet=$"Lat: ${Location1.Latitude} Lon: ${Location1.Longitude}"$
        m1.InfoWindowShown=True
       
        Dim LaLo As LatLng
        LaLo.Initialize(Location1.Latitude,Location1.Longitude)
        m1.Position = LaLo  'new Position
       
        Dim cp As CameraPosition
        cp.Initialize(Location1.Latitude, Location1.Longitude, 14)
        gmap.AnimateCamera(cp)
       
        gmap.MyLocationEnabled=True
    End If
End Sub
 
Last edited:
Upvote 0
Top