/* global setTimeout */ /* eslint-disable react/jsx-indent-props */ /* eslint-disable import/no-absolute-path */ import React, { Component } from 'react'; import PropTypes from 'prop-types'; import { Map, TileLayer, Marker, CircleMarker, Circle } from 'react-leaflet'; import Leaflet from 'leaflet'; import { translate } from 'react-i18next'; import { withTracker } from 'meteor/react-meteor-data'; import update from 'immutability-helper'; import geolocation from '/imports/startup/client/geolocation'; import { positionIcon } from '/imports/ui/components/Maps/Icons'; import 'leaflet-graphicscale/dist/Leaflet.GraphicScale.min.css'; import 'leaflet-graphicscale/dist/Leaflet.GraphicScale.min.js'; import 'leaflet-sleep/Leaflet.Sleep.js'; import Control from 'react-leaflet-control'; import { Button, ButtonToolbar } from 'react-bootstrap'; import './SelectionMap.scss'; class SelectionMap extends Component { constructor(props) { super(props); this.state = { center: props.center, marker: props.center, zoom: 11, distance: props.distance, draggable: true }; this.getMap = this.getMap.bind(this); this.toggleDraggable = this.toggleDraggable.bind(this); this.updatePosition = this.updatePosition.bind(this); this.fit = this.fit.bind(this); this.addScale = this.addScale.bind(this); this.doSubs = this.doSubs.bind(this); } componentDidMount() { if (this.isValidState()) { this.addScale(); } } componentWillReceiveProps(nextProps) { const nextCenter = nextProps.center[0] ? nextProps.center : this.state.center; const nextMarker = nextProps.center[0] ? nextProps.center : this.state.marker; this.setState({ center: nextCenter, marker: nextMarker, distance: nextProps.distance || this.state.distance }); this.fit(); } componentDidUpdate() { this.fit(); } getMap() { return this.selectionMap.leafletElement; } toggleDraggable() { this.setState({ draggable: !this.state.draggable }); } updatePosition() { const { lat, lng } = this.marker.leafletElement.getLatLng(); // console.log(`New marker lat ${lat} and lng ${lng}`); const currentDistance = this.state.distance; this.setState(update(this.state, { $merge: { marker: [lat, lng] } })); this.props.onSelection({ lat, lng, currentDistance }); this.fit(); } fit() { // console.log("fit!"); if (this.selectionMap && this.distanceCircle) { this.getMap().fitBounds(this.distanceCircle.leafletElement.getBounds(), [70, 70]); } } addScale() { // https://www.npmjs.com/package/leaflet-graphicscale const map = this.getMap(); const options = { fill: 'fill', showSubunits: true }; // var graphicScale = Leaflet.control.graphicScale([options]).addTo(map); } doSubs() { // event param not used this.props.history.push( '/login', { subscription: { center: this.state.center, distance: this.state.distance } } ); } isValidState() { return this.state.center && this.state.center[0] && this.state.distance; } render() { // console.log('render map called'); return (