mercredi 25 février 2015

Google Maps api v2 android: How to Remove a single marker?



In my app i need to have 2 types of markers: the first ones need to stay on a location and the second ones need to move, right now I dont have the static markers yet but my app can show a marker moving if the location of the phone changes but for that i call mMap.clear(), I dont want to clear all markers when the location changes so I need to remove only that marker, I read in another question that I need to use Marker.remove(); to remove individual markers but im not sure where to implement that in the code.


Here is the method for a new location:



public void onLocationChanged(Location location) {
mMap.clear();
GetLatLong();
handleNewLocation(location);
mCurrentLocation = location;


}


and here is the handleNewLocation method:



private void handleNewLocation(Location location) {
if (mLastLocation != null) {

LatLng latLng = new LatLng(list.get(0), list.get(1));
//mMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));


//añadir un nuevo marker con la posición de la variable latLng
MarkerOptions camion = new MarkerOptions()
.position(latLng)
.title("Camión")
.snippet("ruta " + ruta)
.icon(BitmapDescriptorFactory.fromResource(R.drawable.bus));


Marker marker = mMap.addMarker(camion);
if (marker == null) {
mMap.addMarker(camion);
} else {
camion.position(latLng);

}

}
}


Thank you.




Aucun commentaire:

Enregistrer un commentaire