Leaflet Draw how to trigger button events from outside the map - leaflet

I've got a leaflet draw instance where I need buttons outside the map to start / cancel draw actions. I have successfully triggered draw start with
map.Draw.Polyline(map, myDrawControlInstance.options.polyline).enable();
but the only action I could find anywhere in the API that is mentioned to cancel a draw action: map.Draw.Polyline(map, myDrawControlInstance.options.polyline).disable(); does not trigger the cancel action.
How can I mimic all buttons that appear in the Leaflet Draw interface when drawing, editing, deleting, etc.
Any help is greatly appreciated

Found a post somewhere from almost 10 years ago with the answer.
You have to keep the drawHandler that's returned by the new L.Draw. method and call the cancel() on it.
start a draw from outside:
const drawHandler = new L.Draw.Polyline(map, drawControl.options.polyline).enable();
cancel the draw from outside the map:
drawHandler.disable();

Related

Leaflet catch fitBounds event, but not moveend

Is it possible to catch only fitBounds event, but not moveend event?
There is no Leaflet built-in event dedicated to the fitBounds method. But since this method is called imperatively, you can easily execute whatever code you want alongside it.
map.fitBounds(myBounds);
// some code related to fitBounds
// e.g. you can trigger your own event:
map.fire("fitbounds-custom-event");
What may be implied by your question, is to detect the end of the potential fitBounds animation, as opposed to the end of user-initiated navigation (user pan, zoom...). Unfortunately there is currently nothing built in Leaflet either for this feature, although it has been discussed for a while (see Leaflet/Leaflet #2934).
One difficulty is that you can start a programmatic fitBounds with animation, and even though that animation is quite fast, user can navigate in the middle, ending the movement prematurely and in a different position. In that case, is it still considered the end of the fitBounds animation, or end of the user navigation?
If we ignore this difficulty, then a possible solution could be to attach a one time event listener when calling fitBounds:
map.once("moveend zoomend", myCallback);
map.fitBounds(myBounds);

Manually fire the Mapbox-GL-Draw drawing mode

I'd like to use Mapbox-GL-Draw in an app, but want to trigger polygon drawing from a button that exists outside the Mapbox map container, and not display the normal drawing control.
A hacky way to do this would be to simply move the button after it's created, but is there a simple, cleanish way to start the drawing process?
Oh, it looks like the answer is as simple as:
drawControl.changeMode('draw_polygon');

Drawings not visible after removeLayer method : leaflet

Introduction
As i am working on application which uses leaflet api. Live Here
Where first user enter '1' as input to load the image on map.
Then user can draw different shapes(fences) using bottom-left buttons.
The 'eraser' button is suppose to remove all layers from map regarding CurrentFeatureGroup.
Problem
When we click on 'eraser' button, all shapes will be removed from map having currentfeaturegroup.
But after removing when we draw some other shapes, these shapes are invisible, although i have checked the function working properly.
I don't have idea how these shapes are now invisible.
Script(which responsible to remove layers)
L.easyButton('<img src="/delete.png">', function () {
map.removeLayer(currentFeatureGroup);
$('.leaflet-container').css('cursor', '');
}).addTo(map);
Please consider removeLayer, not clearLayer.If someone have any idea
about this problem please do help.Any kind of help or reference will
be appreciated, thanks for your time
If you completely remove the featurelayer from the map by using map.removeLayer(currentFeatureLayer) where do you expect that any new features you draw after that will be added to? If you want to remove all current features from the featurelayer you really should be using currentFeatureLayer.clearLayers() which will keep the featurelayer so that you can continue to add features afterwards.

How to add overlays designed by user on button click in a map

I am interested in adding an option to my GIS Map application, the ability to draw Polygon, circle, polyroutes overlays for the user to search data within.The problem is that I've read and tested codes of how to draw an overlay, but they are always static.I want it to be dynamic, with dynamic center and points (or radius) set by the user on click.A mystery for me.(I'm a beginner in iPhone programming, this is my first app.)And I'm not using and don't want to use things like ArcGIS API for iPhone.I would appreciate any help.
To let the user "draw" an arbitrary polygon on the map, one approach is to use draggable annotations that represent the corners of the polygon. Provide an "Add Corner" button and some kind of Remove Corner button on each annotation.
See my answer to User creating a box on MKMapView for some more details. On that question though, the OP actually ended up using another solution described in the comments which would work well if the polygons are always rectangles.
For implementing a button in an annotation view (if you want a "Remove Corner" button on the annotations), see my answer to How to get click event from a button added over MKAnnotationView.
Once you a have a polygon or other overlay on the map, dragging it by direct touches may only be possible by adding a gesture recognizer to the map (with its own scrolling turned off) and using a custom MKOverlay and MKOverlayView that allow coordinate changes. Adding a gesture recognizer directly to an MKOverlayView doesn't seem to work and the built-in overlay classes don't allow changes to coordinates.
An alternative to moving by direct touches is putting some controls on the side (Up/Down/Left/Right/etc buttons) that modify the custom overlay.
The Apple sample app Breadcrumb gives an example of a custom overlay/view for a path. In WWDC 2010, the sample app LocationReminders gives an example of a custom overlay/view for a circle that can move and resize.
Finally, when you do a search for businesses, you could use the overlay's boundingMapRect (which is always a rectangle regardless of the overlay's shape) as the bounding box for the initial search and then check if the coordinate of each business found is in the overlay's actual shape using the answer to How to determine if an annotation is inside of MKPolygonView (iOS).

moving a map overlay

I am trying to create an app where the user can mark an area on a map. I want him to be able to move the area he marked.
I found that there is an api for adding overlays on a map, but I cannot get to move those overlays once I put them on the map. I tried to subclass the MKCircleView with my own view and implement the touchesBegan:withEvent: method but it never seems to get called.
Any ideas how this can be implemented and why it doesn't work this way?
Thanks.
Try looking through the apple developer code for sample called Regions.
It has circles which can be dragged and moved around.