Skip to content

Commit

Permalink
url uses circle around view center as bounds
Browse files Browse the repository at this point in the history
  • Loading branch information
Gjum committed May 30, 2017
1 parent 300cf84 commit f9bfa12
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 8 deletions.
9 changes: 7 additions & 2 deletions js/Main.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,12 @@ export class Main extends Component {
this.state = _.merge(this.state, props.options);
}

this.mapView = hashToView(location.hash, this.props, this.state);
this.viewDefaults = {
radius: props.borderCircleRadius || props.borderSquareRadius,
basemap: this.state.basemap,
};

this.mapView = hashToView(location.hash, this.viewDefaults);
this.state.basemap = this.mapView.basemap;

this.pluginApi = new PluginApi(this);
Expand Down Expand Up @@ -188,7 +193,7 @@ export class Main extends Component {
zoom={minZoom}
maxZoom={5}
minZoom={minZoom}
onmoveend={e => history.replaceState({}, document.title, viewToHash(e.target, this.state))}
onmoveend={e => history.replaceState({}, document.title, viewToHash(e.target, this.state, this.viewDefaults))}
onmousemove={e => this.coordsDisplay && this.coordsDisplay.setCursor(e.latlng)}
>

Expand Down
27 changes: 21 additions & 6 deletions js/Url.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,26 @@
import {intCoords, radiusToBounds, xz} from './Util';

export function viewToHash(leaf, appState) {
export function viewToHash(leaf, appState, defaults) {
var hash = '';

if (appState.basemap != defaults.basemap)
hash += '#t=' + appState.basemap;

let boundsObj = leaf.getBounds();
let [x, z] = intCoords(boundsObj.getCenter());
let [e, n] = intCoords(boundsObj.getNorthEast());
let [w, s] = intCoords(boundsObj.getSouthWest());
return '#b=' + [w, n, e, s].join(',')
+ '#t=' + appState.basemap;
let radius = parseInt(Math.min(Math.abs(e - w), Math.abs(s - n)) / 2);
if (radius < defaults.radius)
hash += '#c=' + x + ',' + z + ',r' + radius;

return hash || '#';
}

export function hashToView(hash, appProps, appState) {
export function hashToView(hash, defaults) {
var view = {
bounds: radiusToBounds(appProps.borderCircleRadius || appProps.borderSquareRadius),
basemap: appState.basemap,
bounds: radiusToBounds(defaults.radius),
basemap: defaults.basemap,
};
if (!hash) return view;

Expand All @@ -34,6 +43,12 @@ export function hashToView(hash, appProps, appState) {
let [w, n, e, s] = vals.split(',', 4).map(parseFloat);
view.bounds = [xz(w, n), xz(e, s)];
}
else if (key == 'c') {
let [x, z, r] = vals.split(/,r?/, 3).map(parseFloat);
if (!r) view.marker = true;
r = r || 10;
view.bounds = [xz(x - r, z - r), xz(x + r, z + r)];
}
else if (key == 't') view.basemap = vals;
});

Expand Down

0 comments on commit f9bfa12

Please sign in to comment.