0

I want to show a context menu when I long click in a Marker in Android Google Maps V2, but:

  • Markers don't handle long clicking events.
  • The openContextMenu(View view) method needs a view and I don't know what to pass. I tried to call the method like this:

    openContextMenu(findViewById(R.id.map));
    

but the context menu doesn't open.

Help me please :P

Sergio
  • 19
  • 1
  • 6
  • 1
    What do you mean by "Marker"? – Alex Bonel Jun 15 '13 at 17:05
  • https://developers.google.com/maps/documentation/android/marker. check event handling section – Raghunandan Jun 15 '13 at 17:14
  • Unfortunately, I haven't any straightforward answer because of lack of experience of work with Google Maps on android, but I think that you should handle touch events on you map some way. Maybe [this](http://stackoverflow.com/questions/13722869/how-to-handle-ontouch-event-for-map-in-google-map-api-v2) link will help you – Alex Bonel Jun 15 '13 at 17:17
  • And one important info about markers as documentation describes. If the marker is draggable so it could be dragged after long click on it which conflicts with the behaviour you want to achieve, so you should call `setDraggable(false)` to handle long click on it by your way – Alex Bonel Jun 15 '13 at 17:23

4 Answers4

1

With respect to the long-click, I suspect that you are out of luck.

With respect to openContextMenu(), you need to call registerForContextMenu() on the View before you call openContextMenu().

Note that context menus have been out of style for a couple of years now, in favor of contextual action bars (a.k.a., action modes).

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
1

The problem here there is no way to have long click listener as of 3.1.36.

A hackish way would be to have transparent view over your map fragment with OnTouchListener attached, where you save touch down event time and return false.

If later you get your GoogleMap.OnMarkerClickListener.onMarkerClick called, you may decide if this was long or normal click.

For a proper way we would need to have OnMarkerLongClickListener in the API, for which you can file a feature request on gmaps-api-issues.

When you have one of these, you can just show a dialog with options, but as CommonsWare suggests, it's not the prettiest way. I suggest making marker selected by changing the icon and showing actions, but it's your choice.

MaciejGórski
  • 22,187
  • 7
  • 70
  • 94
1

Look at this page :

https://developers.google.com/maps/documentation/android/infowindows

You have to refer to the InfoWindow of the marker to do what you need, because there is a OnInfoWindowClickListener() you will handle the click event of the info, that is a different event from the click of the marker. Clever method to solve the lack of a OnMarkerLongClickListener().

I hope you can help

Mer
  • 19
  • 3
0

You could always just set define the marker as something when you add it eg:

@Override
    public void onMapLongClick(LatLng point) {

        map.clear();
        myMarker = map.addMarker(new MarkerOptions().position(point));
        Log.d("Devon", "Lat: " + point.latitude + " Lng: " + point.longitude);
    }

and then when you call the onMarkerClick method, if the marker clicked is equal to the defined marker, execute the code:

@Override
    public boolean onMarkerClick(Marker marker) {

        if (marker.equals(myMarker)) {
            Toast.makeText(this, "Add 'Directions to here' and 'Directions From here' buttons.", Toast.LENGTH_LONG).show();
        }
        return true;
    }