1

In my django app I have a Worker model with a name and location (Point). Using django-leflet LeafletWidget I can create a Form where the user can set a location (marker to that worker). Is it possible to add an event to the widget, so, every time the user sets the marker, or change the marker position it gets the coordinates of that point and perform other actions (like an ajax request)?

class WorkerForm(forms.ModelForm):
    
    class Meta:
        model = WorkerModel
        exclude = ("id",)
        widgets = {
            'name':forms.TextInput(attrs={'class': 'form-control'}),
            'location': LeafletWidget(),
}

in the template i just call 'forms.location' and it renders the map with the controls to set a marker

This is what I get

Every time the user sets the marker I want to get the location of that marker. How do I do that?

Ernesto Ruiz
  • 736
  • 9
  • 31

1 Answers1

1

First you should add an additional JavaScript file to the form, where you can capture the event and do whatever you like with it. To do so, add a class media to the form and indicate the location of the JS inside:

class WorkerForm(forms.ModelForm):

    ...

    class Media:
        js = ('path/to/script.js',)

Create the script.js file on the path/to directory inside your static directory of your app. On that script.js, add the following code:

// Wait for the map to be initialized
$(window).on('map:init', function(e) {
    // Get the Leaflet map instance
    map = e.originalEvent.detail.map;
    // Capture the creation of a new feature
    map.on('draw:created', function (e) {
        const coordinates = e.layer._latlng;
        // Your stuff
    });
    map.on('draw:edited', function (e) {
        var layers = e.layers._layers
        var coordinates;
        Object.keys(layers).forEach(function(key) {
            coordinates = layers[key]._latlng;
        });
        // Your stuff
    });
});

Check the Leaflet Draw documentation to see all the events available.

Marc Compte
  • 4,579
  • 2
  • 16
  • 22
  • How can i get the location of the marker if a use the event map.on('draw:edited ', function (e){ const coordinates = e.layer._latlng; //this returns error }); – Ernesto Ruiz May 28 '21 at 03:33
  • e.layer._latlng does not exist for the 'draw:edited' event. How can I get the coordinates of the new location of the marker when editing? – Ernesto Ruiz May 28 '21 at 03:35
  • @ErnestoRuiz The "edited" event will return you the layer even if it only has 1 feature. You have to iterate through each feature (called layer in the Leaflet API) to get their corresponding coordinates. I have edited my answer to include this event. – Marc Compte May 28 '21 at 12:30
  • 1
    Thenk you so much for your help. A litle detail: I had to iterate e.layers._layers map.on('draw:edited', function (e) { var layers =e.layers._layers var coordinates; Object.keys(layers).forEach(function(key) { coordinates = layers[key]._latlng; console.log(coordinates) }); call_ajax(coordinates); }); – Ernesto Ruiz May 29 '21 at 18:50
  • I haven't used django-select and I don't know how to answer this other question. I'm afraid I can't provide my e-mail. In any case, your best bet is to publish your questions here. Here you'll get the fastest and best answer every time. If you don't get attention, try adding more info or additional details to the question, things you tried and errors you got. – Marc Compte May 29 '21 at 19:04