https://trac.openstreetmap.org/ticket/5056
zoom: this.getZoom(),
layers: this.getLayersCode()
}
zoom: this.getZoom(),
layers: this.getLayersCode()
}
+ },
+
+ setState: function(state, options) {
+ if (state.center) this.setView(state.center, state.zoom, options);
+ this.updateLayers(state.layers);
},
parseHash: function(hash) {
},
parseHash: function(hash) {
var i = hash.indexOf('#');
if (i < 0) {
var i = hash.indexOf('#');
if (i < 0) {
- hash = hash.substr(i + 1);
-
- if (hash === '') {
- return false;
- }
+ hash = querystring.parse(hash.substr(i + 1));
- hash = querystring.parse(hash);
-
- if (hash.map === undefined) {
- return false;
- }
+ var map = (hash.map || '').split('/'),
+ zoom = parseInt(map[0], 10),
+ lat = parseFloat(map[1]),
+ lon = parseFloat(map[2]);
- var args = hash.map.split("/");
- if (args.length !== 3) {
- return false;
+ if (!isNaN(zoom) && !isNaN(lat) && !isNaN(lon)) {
+ args.center = new L.LatLng(lat, lon);
+ args.zoom = zoom;
- var zoom = parseInt(args[0], 10),
- lat = parseFloat(args[1]),
- lon = parseFloat(args[2]);
-
- if (isNaN(zoom) || isNaN(lat) || isNaN(lon)) {
- return false;
- }
-
- args = {
- center: new L.LatLng(lat, lon),
- zoom: zoom
- };
-
if (hash.layers) {
args.layers = hash.layers;
}
if (hash.layers) {
args.layers = hash.layers;
}
currentPath = path;
currentRoute = routes.recognize(currentPath);
currentRoute.run('popstate', currentPath);
currentPath = path;
currentRoute = routes.recognize(currentPath);
currentRoute.run('popstate', currentPath);
- var state = e.originalEvent.state;
- if (state.center) {
- map.setView(state.center, state.zoom, {animate: false});
- map.updateLayers(state.layers);
- }
+ map.setState(e.originalEvent.state, {animate: false});
});
router.route = function (url) {
var path = url.replace(/#.*/, ''),
route = routes.recognize(path);
if (!route) return false;
});
router.route = function (url) {
var path = url.replace(/#.*/, ''),
route = routes.recognize(path);
if (!route) return false;
- window.history.pushState(OSM.parseHash(url) || {}, document.title, url);
+ window.history.pushState(OSM.parseHash(url), document.title, url);
currentRoute.run('unload');
currentPath = path;
currentRoute = route;
currentRoute.run('unload');
currentPath = path;
currentRoute = route;
if (hash === currentHash) return;
currentHash = hash;
var state = OSM.parseHash(hash);
if (hash === currentHash) return;
currentHash = hash;
var state = OSM.parseHash(hash);
- if (!state) return;
- map.setView(state.center, state.zoom);
- map.updateLayers(state.layers);
router.stateChange(state, hash);
};
router.stateChange(state, hash);
};