Skip to content
Snippets Groups Projects
Commit 1807a6ba authored by Joni Laukka's avatar Joni Laukka
Browse files

Own position is shown on the map when the map component did mount.

parent f3d68eb0
No related branches found
No related tags found
1 merge request!3Get own location
...@@ -2,15 +2,33 @@ import React, { Component } from 'react'; ...@@ -2,15 +2,33 @@ import React, { Component } from 'react';
import { Map, TileLayer, Marker, Popup } from 'react-leaflet'; import { Map, TileLayer, Marker, Popup } from 'react-leaflet';
class UserMap extends Component { 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){ if(!navigator.geolocation){
console.log("Can't get geolocation :/"); console.log("Can't get geolocation :/");
return null; callback(null);
} }
else{ else{
navigator.geolocation.getCurrentPosition((position)=>{ navigator.geolocation.getCurrentPosition((position)=> {
return ({ callback({
lat: position.coords.latitude, lat: position.coords.latitude,
lng: position.coords.longitude lng: position.coords.longitude
}); });
...@@ -30,6 +48,7 @@ class UserMap extends Component { ...@@ -30,6 +48,7 @@ class UserMap extends Component {
Se on perjantai, my dudes <br /> Se on perjantai, my dudes <br />
</Popup> </Popup>
</Marker> </Marker>
{this.state.ownLat !== null && <Marker position={[this.state.ownLat, this.state.ownLng]}></Marker>}
</Map> </Map>
); );
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment