Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting a LongClickListener on a map Marker

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?

like image 868
tyczj Avatar asked Mar 13 '13 16:03

tyczj


3 Answers

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

like image 177
ARP Avatar answered Nov 17 '22 10:11

ARP


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;
                    }
                }

            }
        });
like image 20
akaya Avatar answered Nov 17 '22 12:11

akaya


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.

like image 7
DanihelSan Avatar answered Nov 17 '22 12:11

DanihelSan