Leaflet-hash lets you to add dynamic URL hashes to web pages with Leaflet maps. You can easily link users to specific map views.
You can view a demo of leaflet-hash at mlevans.github.io/leaflet-hash/map.html.
-
Prepare a basic leaflet map. You can find instructions on Leaflet's quick-start guide.
-
Include leaflet-hash.js.
-
Once you have initialized the map (an instance of L.Map), add the following code:
// Assuming your map instance is in a variable called map var hash = new L.Hash(map);
L.Hash
can accept a second parameter options
which can have two keys, baseURI
and query
.
baseURI
allows leaflet-hash
to be used along side any hash-based router, such as angular-router
and react-router
.
For example, if your map lives at http://mydomain.com/#/map
, you can configure leaflet-hash
to respect that format as so:
L.Hash(map, {
baseURI: '#/map'
});
query
can be used to allow leaflet-hash
to preserve query strings that may be present, for example, if you would like to use the URI to manage/preserve map layers.
If you would like to have URIs like #/0/1/1?layers=OSM,Mapbox
, you can enable it as so
L.Hash(map, {
query: true
});
MIT License. See LICENSE for details.