Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

how to programmatically create a marker ? #159

Closed
delister opened this issue Jul 11, 2013 · 7 comments
Closed

how to programmatically create a marker ? #159

delister opened this issue Jul 11, 2013 · 7 comments

Comments

@delister
Copy link

Tell me is it possible to programmatically create a marker ?

P.S. Translated by google translate.

@markgibbons25
Copy link

Yes it's easy. But you don't need Leaflet.draw to do it, just Leaflet. Check the API at http://leafletjs.com/

@delister
Copy link
Author

If I do this:
L.marker([51.5, -0.09]).addTo(map);
then the marker can not be edited via the control panel leaflet.draw.

@delister delister reopened this Jul 12, 2013
@jacobtoye
Copy link
Member

Hi @delister, you need to make sure the marker is added to the L.FeatureGroup that you pass into the draw control. E.g.

// Normal setup of draw control
var drawnItems = new L.FeatureGroup();
map.addLayer(drawnItems);

// Set the title to show on the polygon button
L.drawLocal.draw.toolbar.buttons.polygon = 'Draw a sexy polygon!';

var drawControl = new L.Control.Draw({
    draw: {
        position: 'topleft',
        polygon: {
            allowIntersection: false,
            showArea: true,
            drawError: {
                color: '#b00b00',
                timeout: 1000
            },
            shapeOptions: {
                color: '#bada55'
            }
        },
        circle: {
            shapeOptions: {
                color: '#662d91'
            }
        },
        marker: false
    },
    edit: {
        featureGroup: drawnItems,
        remove: false
    }
});
map.addControl(drawControl);

// Add marker programatically:
L.marker([51.5, -0.09]).addTo(drawnItems);

Note the last line adds the marker to the feature group rather than the map.

@ruuttt
Copy link

ruuttt commented May 6, 2014

Thanks

@stepankuzmin
Copy link

I'm getting this error on edit

Uncaught TypeError: Cannot read property 'enable' of undefined 

P.S. I'm trying to add GeoJSON layer.

@joongtang
Copy link

try this code~

L.geoJson(data, {
    style: function (feature) {
        return {color: feature.properties.color};
    },
    onEachFeature: function (feature, layer) {
        layer.bindPopup(feature.properties.description).addTo(drawnItems);
    }
});

@ZanwingMak
Copy link

thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants