2016-11-09 21:13:43 +00:00
|
|
|
import React from 'react';
|
|
|
|
import '../css/Map.scss';
|
2016-11-11 16:00:31 +00:00
|
|
|
//import 'leaflet.css';
|
|
|
|
import 'leaflet/dist/leaflet.css';
|
|
|
|
import 'leaflet-basemaps/L.Control.Basemaps.css';
|
|
|
|
import Leaflet from 'leaflet';
|
|
|
|
import 'leaflet-basemaps/L.Control.Basemaps';
|
2016-11-09 21:13:43 +00:00
|
|
|
import $ from 'jquery';
|
|
|
|
import ErrorMessage from './ErrorMessage';
|
|
|
|
|
|
|
|
class Map extends React.Component {
|
|
|
|
static defaultProps = {
|
2016-11-11 16:00:31 +00:00
|
|
|
bounds: [[-90, -180], [90, 180]],
|
2016-11-09 21:13:43 +00:00
|
|
|
maxzoom: 18,
|
|
|
|
minzoom: 0,
|
|
|
|
scheme: 'tms',
|
|
|
|
showBackground: false,
|
2016-11-11 16:00:31 +00:00
|
|
|
opacity: 100,
|
2016-11-09 21:13:43 +00:00
|
|
|
url: "",
|
|
|
|
error: ""
|
|
|
|
}
|
|
|
|
|
|
|
|
static propTypes() {
|
|
|
|
return {
|
|
|
|
bounds: React.PropTypes.array,
|
|
|
|
maxzoom: React.PropTypes.integer,
|
|
|
|
minzoom: React.PropTypes.integer,
|
|
|
|
scheme: React.PropTypes.string, // either 'tms' or 'xyz'
|
|
|
|
showBackground: React.PropTypes.boolean,
|
|
|
|
showControls: React.PropTypes.boolean,
|
|
|
|
tileJSON: React.PropTypes.string,
|
|
|
|
url: React.PropTypes.string
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
constructor(props) {
|
|
|
|
super(props);
|
|
|
|
|
|
|
|
this.state = {
|
|
|
|
bounds: this.props.bounds,
|
|
|
|
maxzoom: this.props.maxzoom,
|
2016-11-11 16:00:31 +00:00
|
|
|
minzoom: this.props.minzoom
|
2016-11-09 21:13:43 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
componentDidMount() {
|
|
|
|
const { showBackground, tileJSON } = this.props;
|
|
|
|
const { bounds, maxzoom, minzoom, scheme, url } = this.state;
|
|
|
|
|
|
|
|
if (tileJSON != null) {
|
|
|
|
this.tileJsonRequest = $.getJSON(tileJSON)
|
|
|
|
.done(info => {
|
|
|
|
const bounds = [info.bounds.slice(0, 2).reverse(), info.bounds.slice(2, 4).reverse()];
|
|
|
|
|
|
|
|
this.setState({
|
|
|
|
bounds,
|
|
|
|
maxzoom: info.maxzoom,
|
|
|
|
minzoom: info.minzoom,
|
|
|
|
scheme: info.scheme || 'xyz',
|
|
|
|
url: info.tiles[0]
|
|
|
|
});
|
|
|
|
})
|
|
|
|
.fail((_, __, err) => this.setState({error: err.message}));
|
|
|
|
}
|
|
|
|
|
|
|
|
const layers = [];
|
|
|
|
|
|
|
|
if (url != null) {
|
|
|
|
this.imageryLayer = Leaflet.tileLayer(url, {
|
|
|
|
minZoom: minzoom,
|
|
|
|
maxZoom: maxzoom,
|
|
|
|
tms: scheme === 'tms'
|
|
|
|
});
|
|
|
|
|
|
|
|
layers.push(this.imageryLayer);
|
|
|
|
}
|
|
|
|
|
|
|
|
this.leaflet = Leaflet.map(this.container, {
|
|
|
|
scrollWheelZoom: true,
|
|
|
|
layers
|
|
|
|
});
|
|
|
|
|
2016-11-11 16:00:31 +00:00
|
|
|
if (showBackground) {
|
|
|
|
const basemaps = [
|
2016-11-11 17:55:56 +00:00
|
|
|
L.tileLayer('//{s}.google.com/vt/lyrs=s,h&x={x}&y={y}&z={z}', {
|
|
|
|
attribution: 'Map data: © Google Maps',
|
|
|
|
subdomains: ['mt0','mt1','mt2','mt3'],
|
|
|
|
maxZoom: 22,
|
|
|
|
minZoom: 0,
|
|
|
|
label: 'Google Maps Hybrid'
|
|
|
|
}),
|
2016-11-11 16:00:31 +00:00
|
|
|
L.tileLayer('//server.arcgisonline.com/ArcGIS/rest/services/World_Imagery/MapServer/tile/{z}/{y}/{x}', {
|
|
|
|
attribution: 'Tiles © Esri — Source: Esri, i-cubed, USDA, USGS, AEX, GeoEye, Getmapping, Aerogrid, IGN, IGP, UPR-EGP, and the GIS User Community',
|
|
|
|
maxZoom: 22,
|
|
|
|
minZoom: 0,
|
|
|
|
label: 'ESRI Satellite' // optional label used for tooltip
|
|
|
|
}),
|
|
|
|
L.tileLayer('//{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {
|
|
|
|
attribution: '© <a href="http://www.openstreetmap.org/copyright">OpenStreetMap</a>',
|
|
|
|
maxZoom: 22,
|
|
|
|
minZoom: 0,
|
|
|
|
label: 'OSM Mapnik' // optional label used for tooltip
|
|
|
|
})
|
|
|
|
];
|
|
|
|
|
|
|
|
this.leaflet.addControl(Leaflet.control.basemaps({
|
|
|
|
basemaps: basemaps,
|
|
|
|
tileX: 0, // tile X coordinate
|
|
|
|
tileY: 0, // tile Y coordinate
|
|
|
|
tileZ: 1 // tile zoom level
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
|
2016-11-09 21:13:43 +00:00
|
|
|
this.leaflet.fitBounds(bounds);
|
|
|
|
|
|
|
|
Leaflet.control.scale({
|
|
|
|
maxWidth: 250,
|
|
|
|
}).addTo(this.leaflet);
|
|
|
|
this.leaflet.attributionControl.setPrefix("");
|
|
|
|
}
|
|
|
|
|
|
|
|
componentDidUpdate() {
|
2016-11-11 16:00:31 +00:00
|
|
|
const { bounds, maxzoom, minzoom, scheme, url } = this.state;
|
2016-11-09 21:13:43 +00:00
|
|
|
|
|
|
|
if (!this.imageryLayer) {
|
|
|
|
this.imageryLayer = Leaflet.tileLayer(url, {
|
|
|
|
minZoom: minzoom,
|
|
|
|
maxZoom: maxzoom,
|
|
|
|
tms: scheme === 'tms'
|
|
|
|
}).addTo(this.leaflet);
|
|
|
|
|
|
|
|
this.leaflet.fitBounds(bounds);
|
|
|
|
}
|
|
|
|
|
2016-11-11 16:00:31 +00:00
|
|
|
this.imageryLayer.setOpacity(this.props.opacity / 100);
|
2016-11-09 21:13:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
componentWillUnmount() {
|
|
|
|
this.leaflet.remove();
|
|
|
|
if (this.tileJsonRequest) this.tileJsonRequest.abort();
|
|
|
|
}
|
|
|
|
|
|
|
|
render() {
|
|
|
|
const { opacity, error } = this.state;
|
|
|
|
|
|
|
|
return (
|
|
|
|
<div style={{height: "100%"}}>
|
2016-11-14 15:58:00 +00:00
|
|
|
<ErrorMessage bind={[this, 'error']} />
|
2016-11-09 21:13:43 +00:00
|
|
|
<div
|
|
|
|
style={{height: "100%"}}
|
|
|
|
ref={(domNode) => (this.container = domNode)}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export default Map;
|