Browse Source

Permalink: read state in init; set intial location only when no other location known

master
parent
commit
4506d53822
  1. 15
      src/index.js
  2. 5
      src/location.js

15
src/index.js

@ -31,6 +31,11 @@ window.onload = function() {
map = L.map('map')
call_hooks('init')
if (location.hash && location.hash.length > 1) {
readState(location.hash.substr(1))
}
call_hooks_callback('init_callback', onload2)
}
@ -105,10 +110,6 @@ function onload2 () {
updateState()
})
if (location.hash && location.hash.length > 1) {
readState(location.hash.substr(1))
}
hash(function (loc) {
readState(loc.substr(1))
})
@ -166,7 +167,11 @@ function readState (url) {
if ('map' in newState) {
var parts = newState.map.split('/')
map.flyTo({ lat: parts[1], lng: parts[2] }, parts[0])
if (typeof map.getZoom() === 'undefined') {
map.setView({ lat: parts[1], lng: parts[2] }, parts[0])
} else {
map.flyTo({ lat: parts[1], lng: parts[2] }, parts[0])
}
}
state = newState

5
src/location.js

@ -7,6 +7,11 @@ register_hook('init_callback', function (callback) {
}
ipLocation('', function (err, ipLoc) {
// initial map location already set
if (typeof map.getZoom() !== 'undefined') {
return callback()
}
if (typeof ipLoc === 'object' && 'latitude' in ipLoc) {
map.setView([ ipLoc.latitude, ipLoc.longitude ], 14)
} else {

Loading…
Cancel
Save