How to change the position of a Marker on a Android Map v2
Asked Answered
W

4

34

I need to do the following: I have a Marker on the map and I need to change the position of it. So I tried the following:

MarkerOptions a = new MarkerOptions()
            .position(new LatLng(50,6)));
        map.addMarker(a);
        a.position(new LatLng(50,5));

where map is a GoogleMap. I think I have to refresh the map or somthing equal?

Weakfish answered 9/4, 2013 at 14:56 Comment(0)
W
91

Found the solution, Need to do it like this:

MarkerOptions a = new MarkerOptions()
    .position(new LatLng(50,6)));
Marker m = map.addMarker(a);
m.setPosition(new LatLng(50,5));
Weakfish answered 9/4, 2013 at 15:13 Comment(4)
@dvrm I checked it and it changes the position of the marker on the map. What do you mean by update?Grier
@dvrm if no need to add marker ,then use mMap.clear(); methodTemperament
@mischka You are right! It is just adding new Marker, not updating old one.Poussin
Thank you, it works, but you should call m.remove() before if you need "refresh" your marker not create one.Obligee
L
26

There's one example of moving marker in google map v2 demo app .. In file adt-bundle-linux/sdk/extras/google/google_play_services/samples/maps/src/com/exa‌​mple/mapdemo/MarkerDemoActivity.java (4.2.2 examples)

Here the code for moving a marker:

public void animateMarker(final Marker marker, final LatLng toPosition, final boolean hideMarker) {
    final Handler handler = new Handler();
    final long start = SystemClock.uptimeMillis();
    Projection proj = mGoogleMapObject.getProjection();
    Point startPoint = proj.toScreenLocation(marker.getPosition());
    final LatLng startLatLng = proj.fromScreenLocation(startPoint);
    final long duration = 500;

    final Interpolator interpolator = new LinearInterpolator();

    handler.post(new Runnable() {
        @Override
        public void run() {
            long elapsed = SystemClock.uptimeMillis() - start;
            float t = interpolator.getInterpolation((float) elapsed
                    / duration);
            double lng = t * toPosition.longitude + (1 - t)
                    * startLatLng.longitude;
            double lat = t * toPosition.latitude + (1 - t)
                    * startLatLng.latitude;
            marker.setPosition(new LatLng(lat, lng));

            if (t < 1.0) {
                // Post again 16ms later.
                handler.postDelayed(this, 16);
            } else {
                if (hideMarker) {
                    marker.setVisible(false);
                } else {
                    marker.setVisible(true);
                }
            }
        }
    });
}

This code will animate the marker from one geopoint to another.

Liquid answered 9/4, 2013 at 15:22 Comment(2)
what is mGoogleMapObject?Bodi
@HammadNasir, mGoogleMapObject is GoogleMap object, that initialize in your class. private GoogleMap mGoogleMap; Projection proj = mGoogleMap.getProjection();Republic
F
20

Most of the answers on StackOverflow suggest using remove() to remove the marker and then create a new one using addMarker . Instead of doing that, simply save the marker once in a Marker type variable and then update its location.

MarkerOptions a = new MarkerOptions().position(new LatLng(50,6))); 
myMarker = mGoogleMap.addMarker(a);

And then instead of removing and re-creating a marker, simply use

myMarker.setPosition(newLatLng);  

Hope this helps !!

Fahland answered 8/12, 2016 at 10:50 Comment(2)
Thank you! Your comment was usefull for me!Knoll
it doesn't work, doing nothing for me. @Weakfish answer's helped.Obligee
P
5

Define "marker" outside the function. for the first time, it will be null and "if" condition will be executed. for the second time "else" will be executed.

        Marker marker = null;    
        protected void onPostExecute(Coordinates coordinates) {
        LatLng latLong = new LatLng("lat", "long");
        if (marker == null) {
            MarkerOptions options = new MarkerOptions().position(latLong)
                    .title("Marker Title");
            marker = mMap.addMarker(options);
        }
        else {
            marker.setPosition(latLong);
        }
     mMap.moveCamera(CameraUpdateFactory.newLatLng(latLong));
     mMap.animateCamera(CameraUpdateFactory.zoomTo(16f));
    }
Prague answered 5/7, 2018 at 17:8 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.