I want to add a button to my leaflet that zooms to the position of the user. I can't figure out the right way. This is what I tried:
L.easyButton('<img src="myimage.png">', function(btn, map){
map.locate({setView: true, maxZoom: 18});
}).addTo(map);
var Test = L.popup().setContent("Your Location");
this does not work…
Best Answer
Your code for adding an Easy Button and have the map locate the user looks to be working. In the sense that it correctly requests the user his/her permission to use the geolocation API.
It also correctly sets the view to that position once the location completes.
So I assume what you may ask for is how to get the popup set on that position once the location completes, as the code you provide seems not working for that very part?
In that case, you would simply have to bind a callback on map
locationfound
event, as explained in the maplocate
method.Demo: http://jsfiddle.net/ve2huzxw/67/