mapbox-gl-native icon indicating copy to clipboard operation
mapbox-gl-native copied to clipboard

Add Draggable Markers

Open tobrun opened this issue 11 years ago • 28 comments

Google Maps allows its Markers to be draggable and allows end developers to register to events

tobrun avatar Sep 29 '15 10:09 tobrun

A workaround would at least be to provide a MapView.OnMoveListener with a onMove callback. The callback would typically be called if the view detecs a MotionEvent.ACTION_MOVE event and the event is not dispatched as a scroll event. This happens e.g. after a long-press followed by a drag.

We are currently working on an Android Map Editor using mapbox-gl-native as the framework. However, I see no possibilities for detecting when the user tries to move a marker. With the above workaround we could detect a long-press followed by a move event and then move the marker by recreating it in the new position.

pshustad avatar Nov 06 '15 14:11 pshustad

@pshustad It is possible to add your own onTouch listener to MapView from an activity since it inherits that from View.

We used this as a work around to detect long presses until we had a chance to add a long press listener.

ljbade avatar Nov 09 '15 23:11 ljbade

@ljbade Thanks for the advice. I was actually in the middle of preparing a pull request for a full-blown implementation of the OnMarkerDragListener API, but might try this workaround instead.

Of course, if there are future plans for a Mapbox "drag marker" API, I would be happy to continue working on it.

pshustad avatar Nov 10 '15 07:11 pshustad

@pshustad feel free to keep working on a PR if you want. I just wanted to let you know about the work around.

ljbade avatar Nov 10 '15 10:11 ljbade

Now that #3276 has landed this feature can be implemented using View Markers instead of GL Markers

tobrun avatar May 20 '16 11:05 tobrun

Hi @tobrun , could you please elaborate how you would realize draggable markers with marker views? (I was looking for a possibility to add a touch listener to a marker view but could not find any.)

p-fischer avatar Jun 09 '16 13:06 p-fischer

I'm also interested how to drag Markers same way as it is possible in Google Maps. @tobrun please give us some suggestions or code snippet how you thought to implement it using View Markers. What are the development plans in this area?

filipc avatar Jun 23 '16 14:06 filipc

@p-fischer @filipc at this point I haven't started to think about the actual implementation or haven't discussed this internally. This feature just barely didn't make the cut for the 4.1.0 feature list and was considered a nice to have. @frederoni is working on the iOS equivalent in https://github.com/mapbox/mapbox-gl-native/pull/5373.

Ideally I would love to support the multi window drag/drop functionality as shown at Google I/O this year. This will require us to use the Android SDK drag and drop and not create something custom from scratch. At this point, when a drag starts we probably need to call removeMarker and re-add it again when we drop the marker.

tobrun avatar Jun 23 '16 22:06 tobrun

I hope this will become sooner, My legacy mapbox android have draggable marker, I dont want to change/remove it from this current mapbox sdk

DavidMont avatar Jul 15 '16 05:07 DavidMont

@tobrun @ljbade Is there any documentation or experiment PR on how to implement Draggable Marker using ACTION_MOVE touch event? I used to extend ItemizedIconOverlay and override onDrawItem to achieve this in 0.7.4. Any help will be much appreciated! Thanks :)

BharathMG avatar Jul 21 '16 07:07 BharathMG

This issue is blocked by #5639, that PR will move the touch logic in core instead of the Android binding. This will introduce the requirement of implementing draggable markers in a different way as initially indicated in https://github.com/mapbox/mapbox-gl-native/issues/2450#issuecomment-228201350.

Current thinking:

  • OnTouch down event:
    • check if we hit any annotation using core
  • OnTouch drag events:
    • detach view from MarkerViewManager update cycles and vierhierarchy
    • update view with x,y coordinates coming from motion event.
  • OnTouch end drag event
    • attach view back and update LatLng of Marker object

tobrun avatar Jul 26 '16 09:07 tobrun

It is almost 1AM here and I am spending hours trying to find a way to drag a marker. My current application uses the JavaScript flavour and dragging works quite well. However, there is a demand for native Android app and it's being hard to get it implemented. Hope this get some priority in your team, thank you!

samucs avatar Aug 26 '16 07:08 samucs

This is on my todo list for improvements to MarkerViews. Currently working on zIndex, this will be the next item on the list.

tobrun avatar Aug 26 '16 16:08 tobrun

Hi, any update on this issue? Regards.

samucs avatar Sep 21 '16 06:09 samucs

+1

marvinernst avatar Oct 20 '16 07:10 marvinernst

One kinda hacky way that I've found of doing this is to use reflection and set an onTouch listener for the MapView. I think this might interfere with the onClick for Markers, but I haven't tested. Furthermore, this behavior does not wait for a long press to start dragging (this is the behavior I wanted, but it may not be appropriate for all cases)

mapView.setOnTouchListener(new View.OnTouchListener() {
            //This onTouch code is a copy of the MapView#onSingleTapConfirmed code, except I'm dragging instead of clicking, and it's being called for every touch event rather than just a tap
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if (event != null) {
                    PointF tapPoint = new PointF(event.getX(), event.getY());
                    float toleranceSides = 4 * screenDensity;
                    float toleranceTopBottom = 10 * screenDensity;
                    float averageIconWidth = 42;
                    float averageIconHeight = 42;
                    RectF tapRect = new RectF((tapPoint.x - averageIconWidth / 2 - toleranceSides) / screenDensity,
                            (tapPoint.y - averageIconHeight / 2 - toleranceTopBottom) / screenDensity,
                            (tapPoint.x + averageIconWidth / 2 + toleranceSides) / screenDensity,
                            (tapPoint.y + averageIconHeight / 2 + toleranceTopBottom) / screenDensity);
                    try {
                        Method method = mapView.getClass().getDeclaredMethod("getMarkersInRect", RectF.class); //Using reflection to access a Mapbox Package Private method
                        method.setAccessible(true);
                        List<Marker> nearbyMarkers = (List<Marker>) method.invoke(mapView, tapRect);
                        long newSelectedMarkerId = -1;
                        List<Marker> selectedMarkers = map.getSelectedMarkers();
                        if (nearbyMarkers != null && nearbyMarkers.size() > 0) {
                            Collections.sort(nearbyMarkers);
                            for (Marker nearbyMarker : nearbyMarkers) {
                                boolean found = false;
                                for (Marker selectedMarker : selectedMarkers) {
                                    if (selectedMarker.equals(nearbyMarker)) {
                                        found = true;
                                    }
                                }
                                if (!found) {
                                    newSelectedMarkerId = nearbyMarker.getId();
                                    break;
                                }
                            }
                        }

                        if (newSelectedMarkerId >= 0) {
                            List<Annotation> annotations = map.getAnnotations();
                            int count = annotations.size();
                            for (int i = 0; i < count; i++) {
                                Annotation annotation = annotations.get(i);
                                if (annotation instanceof MarkerView) {
                                    if (annotation.getId() == newSelectedMarkerId) {
                                        if (selectedMarkers.isEmpty() || !selectedMarkers.contains(annotation)) {
                                            //DRAG! instead of clicking
                                            annotation.setPosition(map.getProjection().fromScreenLocation(tapPoint));
                                            return true;
                                        }
                                        break;
                                    }
                                }
                            }
                        }
                    } catch (NoSuchMethodException | InvocationTargetException | IllegalAccessException e) {
                        e.printStackTrace();
                    }
                }
                return false;
            }
        });

vanshg avatar Nov 02 '16 00:11 vanshg

@tobrun Any plans to have draggable markers in 5.0?

vanshg avatar Mar 09 '17 19:03 vanshg

Hi, Any update on for the draggable marker feature? Thanks :)

codebee avatar Mar 19 '18 12:03 codebee

Android : any update or trick we use drag marker or point or polyline on map??? i am stuck for four three days on drag marker or point on polyline??? kindly help me to solve out this problem or code??

Meerz avatar Apr 20 '18 04:04 Meerz

@Meerz Refer to the code I posted a few comments above. I can confirm it is still working on Mapbox 6.0.1

@tobrun Is this feature in any future roadmap?

vanshg avatar Apr 20 '18 06:04 vanshg