is there a way to set a longClickListsner
on a marker on google maps v2? I want to have the user long press on the marker and have a dialog show with options to delete or view information. Can this be done?
I have another proposition.
First i make the marker draggable:
mapa.addMarker(new MarkerOptions() ...
.setDraggable(true);
After you can make a listener setOnMarkerDragListener like this:
mapa.setOnMarkerDragListener(new OnMarkerDragListener() {
@Override
public void onMarkerDragStart(Marker marker) {
// TODO Auto-generated method stub
//Here your code
}
@Override
public void onMarkerDragEnd(Marker marker) {
// TODO Auto-generated method stub
}
@Override
public void onMarkerDrag(Marker marker) {
// TODO Auto-generated method stub
}
});
And then you can override that you want (normally onMarkerDragStart to simulate a long click)
Hope it helps
Marker class doesn't have LongClickListener. Though this approach is far from perfect here is an idea about what you can do.
Set a long click listener for your GoogleMap object. On long click, check if the clicked position is close enough to any of your markers. To decide this closeness threshold, you may use map zoom level.
Here is the not so good sample code. I haven't tried it but it may suit your needs.
map.setOnMapLongClickListener(new OnMapLongClickListener() {
@Override
public void onMapLongClick(LatLng latLng) {
for(Marker marker : yourMarkerList) {
if(Math.abs(marker.getPosition().latitude - latLng.latitude) < 0.05 && Math.abs(marker.getPosition().longitude - latLng.longitude) < 0.05) {
Toast.makeText(MapActivity.this, "got clicked", Toast.LENGTH_SHORT).show(); //do some stuff
break;
}
}
}
});
As ARP suggested, you can use the OnMarkerDragListener to simulate the long click. In my case, I am creating new markers when the onMapLongClick() occurred, so I wanted to do something similar (but not equal) that the
@Override
public void onMarkerDragStart(Marker marker) {
marker.remove();
MarkerOptions options = createBasedOnMarker(marker);
this.googleMap.addMarker(options);
}
this way, you will remove the marker that was previously within the map and create another in the same position, with the same content (you have to have a Map or Pair to keep the info from the markers you create).
Hope my workaround helps.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With