diff --git a/modules/behavior/hash.js b/modules/behavior/hash.js index 728bd8f6b..c311e6b62 100644 --- a/modules/behavior/hash.js +++ b/modules/behavior/hash.js @@ -1,5 +1,6 @@ import * as d3 from 'd3'; import _ from 'lodash'; +import { geoSphericalDistance } from '../geo'; import { modeBrowse } from '../modes'; import { utilQsString, utilStringQs } from '../util'; @@ -12,13 +13,21 @@ export function behaviorHash(context) { var parser = function(map, s) { var q = utilStringQs(s); var args = (q.map || '').split('/').map(Number); + if (args.length < 3 || args.some(isNaN)) { return true; // replace bogus hash - } else if (s !== formatter(map).slice(1)) { - var mode = context.mode(); - if (mode && mode.id.match(/^draw/) !== null) { + + } else if (s !== formatter(map).slice(1)) { // hash has changed + var mode = context.mode(), + dist = geoSphericalDistance(map.center(), [args[2], args[1]]), + maxdist = 500; + + // Don't allow the hash location to change too much while drawing + // This can happen if the user accidently hit the back button. #3996 + if (mode && mode.id.match(/^draw/) !== null && dist > maxdist) { context.enter(modeBrowse(context)); } + map.centerZoom([args[2], Math.min(lat, Math.max(-lat, args[1]))], args[0]); } };