R
R
Rodion Yurchenko2016-08-18 09:07:54
JavaScript
Rodion Yurchenko, 2016-08-18 09:07:54

How to track a click on a newly created polygon in a leaflet?

Good afternoon!
There is a leaflet on which you can draw polygons
, you need to catch the click event on the newly created polygon
, now on the site there is this code:

$(document).ready(function () {
    cityId = $('#save').data('city');
    mainLayer = new Array();
    function showMapEditor(container, lat, lon) {
        map = new L.Map(container, {center: new L.LatLng(lat, lon), zoom: 13, zoomAnimation: false});
        map.scrollWheelZoom.disable();
        //Подключаем тайлы
        var osm = new L.TileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png');
        var yndx = new L.Yandex();
        var googleLayer = new L.Google('ROADMAP');
        //Добавляем слой osm
        map.addLayer(osm);
        //Добавляем control для переключения карт
        map.addControl(new L.Control.Layers({'OSM': osm, "Yandex": yndx, "Google": googleLayer}));
        //Добавляем cintrol для рисования карт
        drawnItems = new L.FeatureGroup();
        map.addLayer(drawnItems);
        var options = {
            position: 'topright',
            draw: {
                polygon: {
                    allowIntersection: false, // Restricts shapes to simple polygons
                    drawError: {
                        color: '#e1e100', // Color the shape will turn when intersects
                        message: '<Strong>Error! <Strong> Can not describe the polygon!' // Message that will show when intersect
                    },
                    shapeOptions: {
                        color: '#1b3834',
                        weight: 1,
                        opacity: 0.9,
                        stroke: true,
                        fill: true,
                        clickable: true
                    }
                }
            },
            edit: {
                featureGroup: drawnItems, //REQUIRED!!
                remove: true,
                polyline: {
                    shapeOptions: {
                        color: '#1b3834',
                        weight: 1,
                        opacity: 0.9
                    }
                },
                polygon: {
                    allowIntersection: false, // Restricts shapes to simple polygons
                    drawError: {
                        color: '#e1e100', // Color the shape will turn when intersects
                        message: '<Strong>Error! <Strong> Can not describe the polygon!' // Message that will show when intersect
                    },
                    shapeOptions: {
                        color: '#1b3834',
                        weight: 1,
                        opacity: 0.9,
                        stroke: true,
                        fill: true,
                        clickable: true
                    }
                }
            }
        };
        var drawControl = new L.Control.Draw(options);
        map.addControl(drawControl);
        map.on('draw:created', function (e) {
            var type = e.layerType,
                    layer = e.layer;
            drawnItems.addLayer(layer);
        });
    }

    var cityLat = $('#cityLat').val();
    var cityLon = $('#cityLon').val();
    showMapEditor('editMap', cityLat, cityLon);

});


I just can’t figure out where the object of the new polygon is and how to attach to it?

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question