Skip to content

Instantly share code, notes, and snippets.

@nrenner
Created April 27, 2017 14:44
Show Gist options
  • Save nrenner/558229a0c78fe28cd6a5694bec80a994 to your computer and use it in GitHub Desktop.
Save nrenner/558229a0c78fe28cd6a5694bec80a994 to your computer and use it in GitHub Desktop.
Extending leaflet-fullHash
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Extending leaflet-fullHash</title>
<link href="https://unpkg.com/leaflet@1.0.0/dist/leaflet.css" rel="stylesheet" type="text/css" />
<script src="https://unpkg.com/leaflet@1.0.0/dist/leaflet-src.js"></script>
<script src="http://kogor.github.io/leaflet-fullHash/leaflet-fullHash.js"></script>
<script src="MyHash.js"></script>
<style>
#map {
width: 600px;
height: 400px;
}
</style>
</head>
<body>
<div id='map'></div>
<script>
var myCenter = new L.LatLng(50.5, 30.51);
var map = new L.Map('map', { center: myCenter, zoom: 15 });
var osmCopy = '&copy; <a href="http://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors';
map.attributionControl.addAttribution(osmCopy);
var osm = L.tileLayer('http://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png');
var positron = L.tileLayer('https://cartodb-basemaps-{s}.global.ssl.fastly.net/light_all/{z}/{x}/{y}.png', {
attribution: '&copy; <a href="https://carto.com/attribution">CARTO</a>'
}).addTo(map);
var marker = new L.Marker(myCenter);
map.addLayer(marker);
marker.bindPopup("<p>Lorem ipsum dolor sit amet</p>");
L.control.layers({
'OpenStreetMap Standard': osm,
'Carto Positron': positron
}, {
'Marker': marker
}
).addTo(map);
var layerHashKeys = {
'osm': osm,
'positron': positron,
'marker': marker
};
L.myHash(map, layerHashKeys);
</script>
</body>
</html>
L.MyHash = function(map, options) {
L.Hash.call(this, map, options);
};
L.MyHash.prototype = L.Util.create(L.Hash.prototype);
L.MyHash.prototype.constructor = L.MyHash;
L.Util.extend(L.MyHash.prototype, {
parseHash: function(hash) {
console.log('parseHash: ' + hash);
var parsed = L.Hash.prototype.parseHash.call(this, hash);
console.log('parseHash: ' + JSON.stringify(parsed));
return parsed;
},
formatHash: function(map) {
var formatted = L.Hash.prototype.formatHash.call(this, map);
console.log('formatHash: ' + formatted);
return formatted;
},
update: function() {
L.Hash.prototype.update.call(this);
console.log('update');
}
});
L.myHash = function(map, options) {
return new L.MyHash(map, options);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment