diff --git a/imports/ui/pages/FiresMap/FiresMap.js b/imports/ui/pages/FiresMap/FiresMap.js index d0e2634..fc14118 100644 --- a/imports/ui/pages/FiresMap/FiresMap.js +++ b/imports/ui/pages/FiresMap/FiresMap.js @@ -27,34 +27,26 @@ import UserSubsToFiresCollection from '/imports/api/Subscriptions/Subscriptions' import './FiresMap.scss'; const MAXZOOM = 6; -const DEF_LAT = 35.159028; -const DEF_LNG = -46.738057; -const DEFAULT_VIEWPORT = { - center: [DEF_LAT, DEF_LNG], // a point in the sea - zoom: 8 -}; const zoom = new ReactiveVar(8); -const lat = new ReactiveVar(DEF_LAT); -const lng = new ReactiveVar(DEF_LNG); -const height = new ReactiveVar(400); -const width = new ReactiveVar(400); +const lat = new ReactiveVar(); +const lng = new ReactiveVar(); +const height = new ReactiveVar(); +const width = new ReactiveVar(); class FiresMap extends React.Component { constructor(props) { super(props); this.state = { - viewport: DEFAULT_VIEWPORT, - modified: false, + viewport: this.props.viewport, useMarkers: false, showSubsUnion: true }; - this.unionGroup = new L.LayerGroup(); const self = this; // viewportchange // https://stackoverflow.com/questions/23123138/perform-debounce-in-react-js this.debounceView = _.debounce((viewport) => { self.handleViewportChange(viewport); - }, 2000); + }, 1500); this.onViewportChanged = this.onViewportChanged.bind(this); } @@ -77,35 +69,15 @@ class FiresMap extends React.Component { return this.fireMap.leafletElement; } - handleViewportChange(viewport) { - console.log(`Viewport changed: ${JSON.stringify(viewport)}`); - zoom.set(viewport.zoom); - lat.set(viewport.center[0]); - lng.set(viewport.center[1]); - this.setState({ viewport, modified: true }); - /* this.state.viewport = viewport; - * this.state.modified = true; */ - if (this.props.subsready && this.fireMap) { - this.showSubsUnion(this.state.showSubsUnion); - } - } - - centerOnUserLocation(viewport) { - this.handleViewportChange(viewport); - } - - useMarkers(use) { - /* this.setState({ userMarkers: use });*/ - this.state.useMarkers = use; - // this.forceUpdate(); + setShowSubsUnion(show) { + this.setState({ showSubsUnion: show }); + this.showSubsUnion(show); } showSubsUnion(show) { - // this.setState({ showSubsUnion: show }); - this.state.showSubsUnion = show; const map = this.getMap(); // http://leafletjs.com/reference-1.2.0.html#layergroup - const self = this; + const unionGroup = new L.LayerGroup(); if (this.union) { map.removeLayer(this.union); @@ -118,9 +90,9 @@ class FiresMap extends React.Component { }; UserSubsToFiresCollection.find().forEach((subs) => { const circle = LGeo.circle([subs.lat, subs.lon], subs.distance * 1000, copts); - circle.addTo(self.unionGroup); + circle.addTo(unionGroup); }); - this.union = unify(self.unionGroup.getLayers()); + this.union = unify(unionGroup.getLayers()); this.union.setStyle({ color: '#145A32', fillColor: 'green', @@ -130,6 +102,29 @@ class FiresMap extends React.Component { } } + handleViewportChange(viewport) { + console.log(`Viewport changed: ${JSON.stringify(viewport)}`); + zoom.set(viewport.zoom); + lat.set(viewport.center[0]); + lng.set(viewport.center[1]); + this.setState({ viewport }); + /* this.state.viewport = viewport; + * this.state.modified = true; */ + if (this.props.subsready && this.fireMap) { + this.showSubsUnion(this.state.showSubsUnion); + } + } + + centerOnUserLocation(viewport) { + this.handleViewportChange(viewport); + } + + useMarkers(use) { + this.setState({ useMarkers: use }); + // this.state.useMarkers = use; + // this.forceUpdate(); + } + addScale() { // https://www.npmjs.com/package/leaflet-graphicscale const map = this.getMap(); @@ -141,13 +136,11 @@ class FiresMap extends React.Component { } render() { - this.state.viewport = !this.state.modified && this.props.viewport && Array.isArray(this.props.viewport.center)? this.props.viewport: this.state.viewport; - if (this.props.subsready && this.fireMap) { // Show union of users this.showSubsUnion(this.state.showSubsUnion); } - + console.log('Rendering map'); return ( /* Large number of markers: https://stackoverflow.com/questions/43015854/large-dataset-of-markers-or-dots-in-leaflet/43019740#43019740 */ @@ -171,7 +164,7 @@ class FiresMap extends React.Component {