import React from 'react' import {throttle} from 'lodash'; import PropTypes from 'prop-types' import { loadJSON } from '../../libs/urlopen' import FeatureLayerPopup from './FeatureLayerPopup'; import 'ol/ol.css' import {apply} from 'ol-mapbox-style'; import {Map, View, Proj, Overlay} from 'ol'; import {toLonLat} from 'ol/proj'; import {toStringHDMS} from 'ol/coordinate'; function renderCoords (coords) { if (!coords || coords.length < 2) { return null; } else { return {coords.map((coord) => String(coord).padStart(7, "\u00A0")).join(', ')} } } export default class OpenLayersMap extends React.Component { static propTypes = { onDataChange: PropTypes.func, mapStyle: PropTypes.object.isRequired, accessToken: PropTypes.string, style: PropTypes.object, onLayerSelect: PropTypes.func.isRequired, debugToolbox: PropTypes.bool.isRequired, replaceAccessTokens: PropTypes.func.isRequired, onChange: PropTypes.func.isRequired, } static defaultProps = { onMapLoaded: () => {}, onDataChange: () => {}, onLayerSelect: () => {}, } constructor(props) { super(props); this.state = { zoom: 0, rotation: 0, cursor: [], center: [], }; this.updateStyle = throttle(this._updateStyle.bind(this), 200); } _updateStyle(newMapStyle) { if(!this.map) return; // See this.map.getLayers().clear(); apply(this.map, newMapStyle); } componentDidUpdate(prevProps) { if (this.props.mapStyle !== prevProps.mapStyle) { this.updateStyle( this.props.replaceAccessTokens(this.props.mapStyle) ); } } componentDidMount() { this.overlay = new Overlay({ element: this.popupContainer, autoPan: true, autoPanAnimation: { duration: 250 } }); const map = new Map({ target: this.container, overlays: [this.overlay], view: new View({ zoom: 1, center: [180, -90], }) }); map.on('pointermove', (evt) => { var coords = toLonLat(evt.coordinate); this.setState({ cursor: [ coords[0].toFixed(2), coords[1].toFixed(2) ] }) }) 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({ center: [ center[0].toFixed(2), center[1].toFixed(2), ], rotation: map.getView().getRotation().toFixed(2), zoom: map.getView().getZoom().toFixed(2) }); }); this.map = map; this.updateStyle( this.props.replaceAccessTokens(this.props.mapStyle) ); } closeOverlay = (e) => { e.target.blur(); this.overlay.setPosition(undefined); } render() { return
this.popupContainer = x} style={{background: "black"}} className="maputnik-popup" >
Zoom: {this.state.zoom}
{this.props.debugToolbox &&
{renderCoords(this.state.cursor)}
{renderCoords(this.state.center)}
{this.state.rotation}
}
this.container = x} style={{ ...this.props.style, }}>
} }