WitaloBenicio
WitaloBenicio

Reputation: 3692

Update position of a marker without adding a new one

How to update a position of my marker in Google Maps v2 in android without adding a new one? Every time my location changes i have to update the position of the user in a Google Map, but it always creating a new point e the map, how to avoid this behaviour?

Here is how im updating

public void onLocationChanged(Location location) {
    this.localizacao = location;
    loc = location;
    if (mAdapter.getCount() == 0){
        getAdvsFromServer();
    }

    LatLng me = new LatLng(location.getLatitude(), location.getLongitude());

    if (eu != null){
        Log.d("marker_eu", "removed");
        eu.remove();
    }
    eu = map.addMarker(new MarkerOptions().position(me).title("Eu"));
    Bitmap bm = BitmapFactory.decodeResource(getResources(), R.drawable.ic_maps_indicator_current_position);
    eu.setIcon(BitmapDescriptorFactory.fromBitmap(bm));
    map.moveCamera(CameraUpdateFactory.newLatLng(me));
    //map.moveCamera(CameraUpdateFactory.newLatLngZoom(me, 13));
}

UPDATE: This way it always creating a new point too:

        public void onLocationChanged(Location location) {
            this.localizacao = location;
            loc = location;
            if (mAdapter.getCount() == 0){
                getAdvsFromServer();
            }

            if (ac != null){
                ac.setLayoutLocalizacaoVisible(View.GONE, View.VISIBLE);
            }

            LatLng me = new LatLng(location.getLatitude(), location.getLongitude());

//          if (eu != null){
//              Log.d("marker_eu", "removed");
//              eu.remove();
//          }

            if (eu == null){
                eu = map.addMarker(new MarkerOptions().position(me).title("Eu"));
                Bitmap bm = BitmapFactory.decodeResource(getResources(), R.drawable.ic_maps_indicator_current_position);
                eu.setIcon(BitmapDescriptorFactory.fromBitmap(bm));
                map.moveCamera(CameraUpdateFactory.newLatLngZoom(me, 13));
            }else {
                eu.setPosition(me);
                map.moveCamera(CameraUpdateFactory.newLatLng(me));
            }

Upvotes: 2

Views: 3781

Answers (2)

user3517102
user3517102

Reputation: 68

If your activity is creating a new Marker while running this method, than eu is somehow cleared or null going into the if statement. eu.setPosition(me);would not create another Marker but simply change the location of the marker without altering any other attributes.

If you are certain that the new marker is not created somewhere else in your code then check the state of eu before going into your if statement and find out why it is null at that point by backtracking. Check the position of the Marker to verify the location after the execution. Use the following and watch the logcat.

System.out.println(eu);

Upvotes: 0

Paresh Mayani
Paresh Mayani

Reputation: 128448

You should not add new marker every time location change, instead you can directly set the new position into your existing marker.

For example:

marker.setPosition(new LatLng(lat, lng));

Upvotes: 1

Related Questions