Skip to content

Instantly share code, notes, and snippets.

@mlevans
Created March 26, 2013 21:44
Show Gist options
  • Save mlevans/5249590 to your computer and use it in GitHub Desktop.
Save mlevans/5249590 to your computer and use it in GitHub Desktop.
Linking to a new map with the same center (for Bill)
<!DOCTYPE html>
<html>
<head>
<title>Linking to a new map with the same center</title>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="http://cdn.leafletjs.com/leaflet-0.5/leaflet.css" />
<!--[if lte IE 8]>
<link rel="stylesheet" href="http://cdn.leafletjs.com/leaflet-0.5/leaflet.ie.css" />
<![endif]-->
<style type="text/css">
body {
font-family: "Helvetica Neue", Helvetica, Arial, sans-serif;
font-size: 14px
padding: 0px;
margin: 20px;
}
</style>
<script src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
<script src="http://cdn.leafletjs.com/leaflet-0.5/leaflet.js"></script>
<script src="https://raw.github.com/mlevans/leaflet-hash/master/leaflet-hash.js"></script>
<script>
$(function(){
var map = L.map('map').setView([37.7647, -122.4359], 12);
L.tileLayer('http://{s}.tile.stamen.com/toner-lite/{z}/{x}/{y}.png', {
maxZoom: 18
}).addTo(map);
var hash = new L.Hash(map);
var base_url = 'http://mlevans.github.com/leaflet-hash/';
$('#new_map').click(function() {
var zoom = map.getZoom(),
lat = map.getCenter().lat,
lon = map.getCenter().lng;
var new_url = base_url + 'map.html#' + zoom + '/' + lat + '/' + lon;
$(this).attr("href", new_url);
});
});
</script>
</head>
<body>
<a id="new_map" href="#">Click here for a new map at the same location</a>
<div id="map" style="width: 600px; height: 400px"></div>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment