Looking for an exampe of adding a marker.

140 views
Skip to first unread message

Fasil Hayat

unread,
May 31, 2013, 5:09:29 PM5/31/13
to leafl...@googlegroups.com
Hi

I am very new to use Leaflet. I am looking for an example where you can add a marker on a map. Preferably an example where I can only add one marker / drag it around the map / add a marker somewhere else and the previous marker is removed.

Is there a good example I can build upon?


With regards,
Fasil

Bryan McBride

unread,
Jun 1, 2013, 12:57:55 AM6/1/13
to leafl...@googlegroups.com
Something like this...

// Create layer group
var group = L.featureGroup();
// Add layer group to map
map.addLayer(group);
// Add marker at center of map
group.addLayer(L.marker(map.getCenter(),{draggable: true}));

// Setup map 'click' listener to clear existing markers and add new marker at click location
map.on('click', function(e){
    group.clearLayers();
    group.addLayer(L.marker(e.latlng,{draggable: true}));
});

Fasil Hayat

unread,
Jun 7, 2013, 3:36:31 AM6/7/13
to leafl...@googlegroups.com
It works  :)
Anyway to start up the map without the marker set? and the upon clikck on the map it shows up and is draggable...

Pierre Roulette

unread,
Jun 7, 2013, 7:59:04 AM6/7/13
to leafl...@googlegroups.com
I don't completely understand what you're trying to say. You want to add the marker only when we click on the map ? And set it draggable ?

Ok, so if it's what you want, the solution is quite easy, given the previous answer :

You just create the group without add the marker : remove the line group.addLayer(L.marker(map.getCenter(),{draggable: true})); and this is it.

Fasil Hayat

unread,
Jun 7, 2013, 10:19:22 AM6/7/13
to leafl...@googlegroups.com
Yes, that was what, I was trying say.
Sorry, about my bad English. :)

Pierre Roulette

unread,
Jun 7, 2013, 10:28:00 AM6/7/13
to leafl...@googlegroups.com
Don't worry, I'm not fluent either ^^ So I hope your code works now ! Bye !

Fasil Hayat

unread,
Jun 7, 2013, 11:11:03 AM6/7/13
to leafl...@googlegroups.com
Is there any way you can show me how to use a custom marker?

I have a marker that I want to use instead of the build-in marker.

I appreciate your time.

Bryan McBride

unread,
Jun 7, 2013, 11:15:54 AM6/7/13
to leafl...@googlegroups.com

Pierre Roulette

unread,
Jun 7, 2013, 11:20:07 AM6/7/13
to leafl...@googlegroups.com
Nothing further !

Fasil Hayat

unread,
Jun 8, 2013, 5:12:32 AM6/8/13
to leafl...@googlegroups.com
Ahh thanks, that seems simple enough to implement.

If you have the time for clearing this question as well, I would be deeply thankful :)
https://groups.google.com/forum/?fromgroups#!topic/leaflet-js/i2xsP2mDRd0
Reply all
Reply to author
Forward
0 new messages