move marker on google maps api 2 move marker on google maps api 2 android android

move marker on google maps api 2


Make variable

 Marker now;

in this part add Marker and remove marker, of course put in the rest of your marker attributes:

 @Overridepublic void onLocationChanged(Location location) {    if(now != null){                now.remove();            }    TextView tvLocation = (TextView) findViewById(R.id.tv_location);    // Getting latitude of the current location    double latitude = location.getLatitude();    // Getting longitude of the current location    double longitude = location.getLongitude();    // Creating a LatLng object for the current location    LatLng latLng = new LatLng(latitude, longitude);    now = googleMap.addMarker(new MarkerOptions().position(latLng)));    // Showing the current location in Google Map    googleMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));    // Zoom in the Google Map    googleMap.animateCamera(CameraUpdateFactory.zoomTo(15));}


Optimized way to move marker :

marker mMarker;

After adding marker to map. while change the location you just need to set position rather than remove marker and add again.

mMarker.setPosition(new LatLon(latLng));

this will reduce the code from remove and add marker to direct set position and also reduce complexity. :)

Enjoy.


You are just moving camera on location change where as you should add marker as well then it will draw marker on current location. and before adding marker clear all the previous markers by calling googlmap.clear();