colouring-montreal/app/src/frontend/map.js

86 lines
2.8 KiB
JavaScript
Raw Normal View History

import React, { Component } from 'react';
import { Map, TileLayer, ZoomControl, AttributionControl } from 'react-leaflet-universal';
import '../../node_modules/leaflet/dist/leaflet.css'
import './map.css'
const OS_API_KEY = 'NVUxtY5r8eA6eIfwrPTAGKrAAsoeI9E9';
/**
* Map area
*/
class ColouringMap extends Component {
2018-09-10 07:40:25 -04:00
constructor(props) {
super(props);
this.state = {
lat: 51.5245255,
lng: -0.1338422,
2018-09-10 18:34:56 -04:00
zoom: 16
2018-09-10 07:40:25 -04:00
};
this.handleClick = this.handleClick.bind(this);
}
handleClick(e) {
var lat = e.latlng.lat
var lng = e.latlng.lng
fetch(
2018-09-10 18:34:56 -04:00
'/buildings.json?lat='+lat+'&lng='+lng
2018-09-10 07:40:25 -04:00
).then(
(res) => res.json()
).then(function(data){
2018-09-10 18:34:56 -04:00
if (data.geometry_id && data.id){
this.props.history.push(`/building/${data.id}.html`);
this.props.selectBuilding(data);
2018-09-10 07:40:25 -04:00
} else {
2018-09-11 18:30:17 -04:00
// this.props.selectBuilding(undefined); // TODO follow through back to maps
2018-09-10 07:40:25 -04:00
}
2018-09-10 18:34:56 -04:00
}.bind(this)).catch(
(err) => console.error(err)
)
}
render() {
2018-09-11 18:30:17 -04:00
const data_layer = (
this.props.match && this.props.match.params && this.props.match.params[1]
)? this.props.match.params[1].replace("/", "")
: 'date_year';
2018-09-10 17:14:09 -04:00
const position = [this.state.lat, this.state.lng];
const key = OS_API_KEY
const tilematrixSet = 'EPSG:3857'
2018-09-10 17:14:09 -04:00
const layer = 'Light 3857' // alternatively 'Night 3857'
const url = `https://api2.ordnancesurvey.co.uk/mapping_api/v1/service/zxy/${tilematrixSet}/${layer}/{z}/{x}/{y}.png?key=${key}`
const attribution = 'Building attribute data is © Colouring London contributors. Maps contain OS data © Crown copyright: OS Maps baselayers and building outlines.'
2018-09-10 17:14:09 -04:00
const colour = `/tiles/${data_layer}/{z}/{x}/{y}.png`;
2018-09-10 18:34:56 -04:00
const geometry_id = (this.props.building) ? this.props.building.geometry_id : undefined;
const highlight = `/tiles/highlight/{z}/{x}/{y}.png?highlight=${geometry_id}`
const highlightLayer = this.props.building ? (
<TileLayer key={data_layer} url={highlight} />
2018-09-10 07:40:25 -04:00
) : null;
2018-09-10 17:14:09 -04:00
return (
<Map
center={position}
zoom={this.state.zoom}
2018-09-10 05:44:32 -04:00
minZoom={10}
maxZoom={18}
doubleClickZoom={false}
zoomControl={false}
2018-09-10 07:40:25 -04:00
attributionControl={false}
onClick={this.handleClick}
>
2018-09-10 05:44:32 -04:00
<TileLayer url={url} attribution={attribution} />
2018-09-10 17:14:09 -04:00
<TileLayer url={colour} />
2018-09-10 07:40:25 -04:00
{ highlightLayer }
<ZoomControl position="topright" />
<AttributionControl prefix="" />
</Map>
);
}
};
export default ColouringMap;