From d39ec1588c59d140c0485e79de7a3a8e69c99289 Mon Sep 17 00:00:00 2001 From: muerwre Date: Mon, 27 Aug 2018 17:28:10 +0700 Subject: [PATCH] center on user location --- src/components/UserLocation.jsx | 69 +++++++++++++++++++++++++++++++++ src/containers/App.jsx | 19 +++++++++ src/styles/colors.less | 4 ++ src/styles/map.less | 52 ++++++++++++++++++++++++- src/styles/panel.less | 4 +- src/utils/geolocation.js | 5 +++ 6 files changed, 149 insertions(+), 4 deletions(-) create mode 100644 src/components/UserLocation.jsx create mode 100644 src/utils/geolocation.js diff --git a/src/components/UserLocation.jsx b/src/components/UserLocation.jsx new file mode 100644 index 0000000..e8f89fc --- /dev/null +++ b/src/components/UserLocation.jsx @@ -0,0 +1,69 @@ +import React from 'react'; +import L, { marker } from 'leaflet'; +import { DomMarker } from '$utils/DomMarker'; + +export class UserLocation extends React.Component { + constructor(props) { + super(props); + + const element = document.createElement('div'); + + this.icon = new DomMarker({ + element, + className: 'location-marker', + }); + + this.mark = null; + this.map = props.editor.map.map; + this.location = []; + // this.mark.addTo(props.editor.map.map); + } + + componentDidMount() { + this.getUserLocation(this.updateLocationMark); + } + + getUserLocation = callback => { + if (!window.navigator || !window.navigator.geolocation) return; + + window.navigator.geolocation.getCurrentPosition(position => { + if (!position || !position.coords || !position.coords.latitude || !position.coords.longitude) return; + + const { latitude, longitude } = position.coords; + + callback(latitude, longitude); + }); + }; + + centerMapOnLocation = () => { + if (this.location && this.location.length === 2) { + this.panMapTo(this.location[0], this.location[1]); + } else { + console.log('hoho'); + this.getUserLocation(this.panMapTo); + } + + this.getUserLocation(this.updateLocationMark); + }; + + panMapTo = (latitude, longitude) => { + if (!latitude || !longitude) return; + + this.map.panTo([latitude, longitude]); + }; + + updateLocationMark = (latitude, longitude) => { + if (!latitude || !longitude) return; + + if (this.mark) this.map.removeLayer(this.mark); + + this.location = [latitude, longitude]; + this.mark = marker(this.location, { icon: this.icon }).addTo(this.map); + }; + + render() { + return ( +
+ ) + } +} diff --git a/src/containers/App.jsx b/src/containers/App.jsx index dd6dfcc..3e74bb4 100644 --- a/src/containers/App.jsx +++ b/src/containers/App.jsx @@ -4,6 +4,8 @@ import { Editor } from '$modules/Editor'; import { EditorPanel } from '$components/panels/EditorPanel'; import { Fills } from '$components/Fills'; import { DEFAULT_LOGO } from '$constants/logos'; +import { getUserLocation } from '$utils/geolocation'; +import { UserLocation } from '$components/UserLocation'; export class App extends React.Component { state = { @@ -36,6 +38,20 @@ export class App extends React.Component { setLogo = logo => { this.setState({ logo }); }; + // + // locateByGeo = () => { + // getUserLocation(this.setMapCenterByGeo); + // }; + // + // setMapCenterByGeo = position => { + // if (!position || !position.coords || !position.coords.latitude || !position.coords.longitude) return; + // + // const { latitude, longitude } = position.coords; + // + // console.log('panning to', { latitude, longitude }); + // + // this.editor.map.map.panTo([latitude, longitude]); + // }; editor = new Editor({ container: 'map', @@ -59,6 +75,9 @@ export class App extends React.Component { return (
+ + + { + if (!navigator || !navigator.geolocation) return; + + navigator.geolocation.getCurrentPosition(callback); +};