diff --git a/src/components/UserMap.js b/src/components/UserMap.js index 31092dc717df87531e2e51152a41691ce28870cc..1a283939de70e157ce48cf0d977292ed5bb27fcd 100644 --- a/src/components/UserMap.js +++ b/src/components/UserMap.js @@ -2,15 +2,33 @@ import React, { Component } from 'react'; import { Map, TileLayer, Marker, Popup } from 'react-leaflet'; class UserMap extends Component { + constructor(props){ + super(props); + this.state = { + ownLat: null, + ownLng: null, + } + } + + componentDidMount(){ + this.getCurrentPosition((position) => { + if(position != null){ + this.setState({ + ownLat: position.lat, + ownLng: position.lng, + }); + } + }); + } - getCurrentPosition(){ + getCurrentPosition(callback){ if(!navigator.geolocation){ console.log("Can't get geolocation :/"); - return null; + callback(null); } else{ - navigator.geolocation.getCurrentPosition((position)=>{ - return ({ + navigator.geolocation.getCurrentPosition((position)=> { + callback({ lat: position.coords.latitude, lng: position.coords.longitude }); @@ -30,6 +48,7 @@ class UserMap extends Component { Se on perjantai, my dudes <br /> </Popup> </Marker> + {this.state.ownLat !== null && <Marker position={[this.state.ownLat, this.state.ownLng]}></Marker>} </Map> ); }