Skip to content

Instantly share code, notes, and snippets.

@ramiroaznar
Last active July 5, 2018 20:14
Show Gist options
  • Save ramiroaznar/36a8a2075dcaee97824ebbbb6d5bdb70 to your computer and use it in GitHub Desktop.
Save ramiroaznar/36a8a2075dcaee97824ebbbb6d5bdb70 to your computer and use it in GitHub Desktop.
Leaflet's flyTo method

Click on a location and the flyTo method will zoom and pan to that particular place.

<!DOCTYPE html>
<html>
<head>
<title>Leaflet's flyTo method</title>
<meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
<meta http-equiv="content-type" content="text/html; charset=UTF-8"/>
<link rel="shortcut icon" href="http://cartodb.com/assets/favicon.ico" />
<style>
html, body, #map {
height: 100%;
padding: 0;
margin: 0;
}
</style>
<link rel="stylesheet" href="https://unpkg.com/[email protected]/dist/leaflet.css" />
<script src="https://unpkg.com/[email protected]/dist/leaflet.js"></script>
</head>
<body>
<div id="map"></div>
<script>
function main() {
var map = new L.Map('map', {
zoomControl: false,
center: [40, 0],
zoom: 3
});
L.tileLayer('http://{s}.tile.opentopomap.org/{z}/{x}/{y}.png', {
attribution: 'OSM'
}).addTo(map);
map.on('click',
function(e){
var coord = e.latlng.toString().split(',');
var lat = coord[0].split('(');
var lng = coord[1].split(')');
console.log("You clicked the map at latitude: " + lat[1] + " and longitude:" + lng[0]);
map.flyTo([lat[1], lng[0]], 10, {
animate: true,
duration: 2 // in seconds
});
});
}
window.onload = main;
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment