center map on new marker

1,777 views
Skip to first unread message

Ben

unread,
Mar 11, 2013, 2:09:24 PM3/11/13
to leafl...@googlegroups.com
I just can't figure out what I'm doing wrong. I am creating a new marker when a button is click. I want the map to zoom and pan to the new marker. The code I have will zoom in, and pan, but it will not center on the marker, instead it will be centered just need the marker.
"check" is my button, and newLocation is the lat and long.

        var map = L.map('map').setView([20, 20], 2);

        $('#check').click(function() {
            var marker = L.marker(newLocalton).addTo(map);
            map.panTo(newLocalton);
            map.setZoom(14);
        });

Ricardo Freitas

unread,
Mar 11, 2013, 2:25:47 PM3/11/13
to leaflet-js
Instead of panTo, try setView. You will get the pan effect if the coordinate is close enough. Had a similar problem before.


--
 
---
You received this message because you are subscribed to the Google Groups "Leaflet" group.
To unsubscribe from this group and stop receiving emails from it, send an email to leaflet-js+...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Ben

unread,
Mar 11, 2013, 4:02:26 PM3/11/13
to leafl...@googlegroups.com
it's really odd, I just noticed that my code works perfectly well if I set the original map state to zoom level 9. It seems that asking it to zoom and pan from zoom level 2 to 14 is messing things up.
Ben
Reply all
Reply to author
Forward
0 new messages