Saran
Saran

Reputation: 443

Move marker without clearing GoogleMap

I added a TileOverlay and some Markers into a map and I want to move a specific Marker on a map without using googleMap.clear() because TileOverlay will get cleared too. Is it possible to do it?

public void onPositionChanged(LatLng newPosition) {
    myMarker.position(newPosition);
    // myMarker should be updated on a map
}

Upvotes: 1

Views: 1109

Answers (2)

Efecan Altay
Efecan Altay

Reputation: 1

Don't Clear Just add new Marker to map

    public void AddMarkerAndSetCameraPosition(LatLng latlng)
    {
        //GMap.Clear();
        CurrentMarker = GMap.AddMarker(new MarkerOptions()
                                                .SetPosition(latlng));
        GMap.AnimateCamera(CameraUpdateFactory.NewLatLngZoom(latlng, 15f));
        GMap.CameraPosition.Zoom = 15f;
        CurrentMarker.ShowInfoWindow();

    }

Upvotes: 0

Athiwat Chunlakhan
Athiwat Chunlakhan

Reputation: 7809

From the code that you have posted. I can see that you only need 1 marker at a time (or you are adding more? doesn't matter). You will have to store the reference to the marker some where your method can access. Then you could just create a dummy marker(or check if the marker doesn't exists, then create one and save the reference to it) and move it using dummy.setPosition(LatLng latlng).

Here is an example code:

private Marker marker;
public void onPositionChanged(LatLng newPosition) {
    if (marker == null) {
        marker = map.addMarker(new MarkerOptions().position(newPostion));
    } else {
        marker.setPostion(newPosition);
    }
}

Reference https://developers.google.com/maps/documentation/android/reference/com/google/android/gms/maps/model/Marker

Upvotes: 1

Related Questions