How to keep the map view with the URL hash

To maintain the map view in the URL hash, you can utilize the hash:true option in the map constructor. This feature is particularly valuable for sharing links that lead to a specific map view or loading the map with a predetermined view other than the default.

By implementing this functionality, the map’s position, including zoom level, center latitude, center longitude, bearing, and pitch (#zoom/latitude/longitude/bearing/pitch), will be synchronized with the hash fragment in the URL of the page. For instance, you can have a URL like: http://path/to/my/page.html#15.21/46.234453/6.055004/23.2/57.

This allows users to share and retrieve a particular map view by simply copying and pasting the URL. Whether it’s for sharing, bookmarking, or customizing default map views, leveraging the URL hash feature is an excellent way to enhance the user experience and improve map navigation.



Related examples

An extension of MapLibre GL JS