mirror of
https://github.com/a-nyx/maputnik-with-pmtiles.git
synced 2024-12-27 08:25:24 +01:00
Feature to update current map view in OSM
This commit is contained in:
parent
6c240d53e4
commit
79fa2b3508
4 changed files with 87 additions and 6 deletions
|
@ -212,6 +212,13 @@ export default class App extends React.Component {
|
|||
vectorLayers: {},
|
||||
mapState: "map",
|
||||
spec: latest,
|
||||
mapView: {
|
||||
zoom: 0,
|
||||
center: {
|
||||
lng: 0,
|
||||
lat: 0,
|
||||
},
|
||||
},
|
||||
isOpen: {
|
||||
settings: false,
|
||||
sources: false,
|
||||
|
@ -531,11 +538,22 @@ export default class App extends React.Component {
|
|||
return metadata['maputnik:renderer'] || 'mbgljs';
|
||||
}
|
||||
|
||||
onMapChange = (mapView) => {
|
||||
this.setState({
|
||||
mapView,
|
||||
});
|
||||
}
|
||||
|
||||
mapRenderer() {
|
||||
const metadata = this.state.mapStyle.metadata || {};
|
||||
|
||||
const mapProps = {
|
||||
mapStyle: style.replaceAccessTokens(this.state.mapStyle, {allowFallback: true}),
|
||||
mapStyle: this.state.mapStyle,
|
||||
replaceAccessTokens: (mapStyle) => {
|
||||
return style.replaceAccessTokens(mapStyle, {
|
||||
allowFallback: true
|
||||
});
|
||||
},
|
||||
onDataChange: (e) => {
|
||||
this.layerWatcher.analyzeMap(e.map)
|
||||
this.fetchSources();
|
||||
|
@ -550,11 +568,13 @@ export default class App extends React.Component {
|
|||
if(renderer === 'ol') {
|
||||
mapElement = <OpenLayersMap
|
||||
{...mapProps}
|
||||
onChange={this.onMapChange}
|
||||
debugToolbox={this.state.openlayersDebugOptions.debugToolbox}
|
||||
onLayerSelect={this.onLayerSelect}
|
||||
/>
|
||||
} else {
|
||||
mapElement = <MapboxGlMap {...mapProps}
|
||||
onChange={this.onMapChange}
|
||||
options={this.state.mapboxGlDebugOptions}
|
||||
inspectModeEnabled={this.state.mapState === "inspect"}
|
||||
highlightedLayer={this.state.mapStyle.layers[this.state.selectedLayerIndex]}
|
||||
|
@ -676,6 +696,7 @@ export default class App extends React.Component {
|
|||
onChangeOpenlayersDebug={this.onChangeOpenlayersDebug}
|
||||
isOpen={this.state.isOpen.debug}
|
||||
onOpenToggle={this.toggleModal.bind(this, 'debug')}
|
||||
mapView={this.state.mapView}
|
||||
/>
|
||||
<ShortcutsModal
|
||||
ref={(el) => this.shortcutEl = el}
|
||||
|
|
|
@ -66,6 +66,7 @@ export default class MapboxGlMap extends React.Component {
|
|||
onMapLoaded: () => {},
|
||||
onDataChange: () => {},
|
||||
onLayerSelect: () => {},
|
||||
onChange: () => {},
|
||||
mapboxAccessToken: tokens.mapbox,
|
||||
options: {},
|
||||
}
|
||||
|
@ -128,12 +129,21 @@ export default class MapboxGlMap extends React.Component {
|
|||
|
||||
const map = new MapboxGl.Map(mapOpts);
|
||||
|
||||
const center = map.getCenter();
|
||||
const zoom = map.getZoom();
|
||||
|
||||
this.setState({
|
||||
center,
|
||||
zoom,
|
||||
});
|
||||
this.props.onChange({center, zoom});
|
||||
|
||||
map.showTileBoundaries = mapOpts.showTileBoundaries;
|
||||
map.showCollisionBoxes = mapOpts.showCollisionBoxes;
|
||||
map.showOverdrawInspector = mapOpts.showOverdrawInspector;
|
||||
|
||||
const zoom = new ZoomControl;
|
||||
map.addControl(zoom, 'top-right');
|
||||
const zoomControl = new ZoomControl;
|
||||
map.addControl(zoomControl, 'top-right');
|
||||
|
||||
const nav = new MapboxGl.NavigationControl({visualizePitch:true});
|
||||
map.addControl(nav, 'top-right');
|
||||
|
@ -189,7 +199,21 @@ export default class MapboxGlMap extends React.Component {
|
|||
this.setState({
|
||||
zoom: map.getZoom()
|
||||
});
|
||||
})
|
||||
});
|
||||
|
||||
map.on("dragend", e => {
|
||||
this.props.onChange({
|
||||
center: map.getCenter(),
|
||||
zoom: this.state.zoom,
|
||||
})
|
||||
});
|
||||
|
||||
map.on("zoomend", e => {
|
||||
this.props.onChange({
|
||||
center: this.state.center,
|
||||
zoom: map.getZoom(),
|
||||
})
|
||||
});
|
||||
}
|
||||
|
||||
render() {
|
||||
|
|
|
@ -61,7 +61,9 @@ export default class OpenLayersMap extends React.Component {
|
|||
|
||||
componentDidUpdate(prevProps) {
|
||||
if (this.props.mapStyle !== prevProps.mapStyle) {
|
||||
this.updateStyle(this.props.mapStyle);
|
||||
this.updateStyle(
|
||||
this.props.replaceAccessTokens(this.props.mapStyle)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -93,6 +95,22 @@ export default class OpenLayersMap extends React.Component {
|
|||
})
|
||||
})
|
||||
|
||||
const onMoveEnd = () => {
|
||||
const zoom = map.getView().getZoom();
|
||||
const center = toLonLat(map.getView().getCenter());
|
||||
|
||||
this.props.onChange({
|
||||
zoom,
|
||||
center: {
|
||||
lng: center[0],
|
||||
lat: center[1],
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
onMoveEnd();
|
||||
map.on('moveend', onMoveEnd);
|
||||
|
||||
map.on('postrender', (evt) => {
|
||||
const center = toLonLat(map.getView().getCenter());
|
||||
this.setState({
|
||||
|
@ -108,7 +126,9 @@ export default class OpenLayersMap extends React.Component {
|
|||
|
||||
|
||||
this.map = map;
|
||||
this.updateStyle(this.props.mapStyle);
|
||||
this.updateStyle(
|
||||
this.props.replaceAccessTokens(this.props.mapStyle)
|
||||
);
|
||||
}
|
||||
|
||||
closeOverlay = (e) => {
|
||||
|
|
|
@ -13,9 +13,12 @@ class DebugModal extends React.Component {
|
|||
onOpenToggle: PropTypes.func.isRequired,
|
||||
mapboxGlDebugOptions: PropTypes.object,
|
||||
openlayersDebugOptions: PropTypes.object,
|
||||
mapView: PropTypes.object,
|
||||
}
|
||||
|
||||
render() {
|
||||
const {mapView} = this.props;
|
||||
|
||||
return <Modal
|
||||
data-wd-key="debug-modal"
|
||||
isOpen={this.props.isOpen}
|
||||
|
@ -23,6 +26,7 @@ class DebugModal extends React.Component {
|
|||
title={'Debug'}
|
||||
>
|
||||
<div className="maputnik-modal-section maputnik-modal-shortcuts">
|
||||
<h4>Options</h4>
|
||||
{this.props.renderer === 'mbgljs' &&
|
||||
<ul>
|
||||
{Object.entries(this.props.mapboxGlDebugOptions).map(([key, val]) => {
|
||||
|
@ -46,6 +50,18 @@ class DebugModal extends React.Component {
|
|||
</ul>
|
||||
}
|
||||
</div>
|
||||
<div className="maputnik-modal-section">
|
||||
<h4>Links</h4>
|
||||
<p>
|
||||
<a
|
||||
target="_blank"
|
||||
rel="noopener" or rel="noreferrer"
|
||||
href={`https://www.openstreetmap.org/#map=${Math.round(mapView.zoom)+1}/${mapView.center.lat}/${mapView.center.lng}`}
|
||||
>
|
||||
Open in OSM
|
||||
</a> — Opens the current view on openstreetmap.org
|
||||
</p>
|
||||
</div>
|
||||
</Modal>
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue