Skip to content
Snippets Groups Projects

Get own location

Merged H9031 requested to merge get-own-location into development
1 file
+ 67
1
Compare changes
  • Side-by-side
  • Inline
+ 67
1
@@ -2,6 +2,67 @@ import React, { Component } from 'react';
@@ -2,6 +2,67 @@ 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,
 
}
 
 
this.DeviceLocationUpdater = null;
 
this.updateDeviceLocation = this.updateDeviceLocation.bind(this);
 
}
 
 
componentDidMount(){
 
this.startDeviceLocationUpdater();
 
}
 
 
componentWillUnmount(){
 
if(this.DeviceLocationUpdater != null){
 
clearInterval(this.DeviceLocationUpdater);
 
}
 
}
 
 
startDeviceLocationUpdater(){
 
this.DeviceLocationUpdater = setInterval(this.updateDeviceLocation, 1000);
 
}
 
 
updateDeviceLocation(){
 
this.getCurrentPosition((position) => {
 
this.setCurrentPosition(position);
 
});
 
}
 
 
setCurrentPosition(position){
 
this.setState({
 
ownLat: position.coords.latitude,
 
ownLng: position.coords.longitude,
 
});
 
}
 
 
getCurrentPosition(callback){
 
if(!navigator.geolocation){
 
console.log("Can't get geolocation :/");
 
}
 
else{
 
// Position tracking options
 
const options = {
 
enableHighAccuracy: true,
 
timeout: 30000,
 
maximumAge: 0
 
}
 
 
navigator.geolocation.getCurrentPosition((position) =>{
 
//success
 
if(position != null){
 
callback(position);
 
}
 
}, (error) =>{
 
console.log(error);
 
}, options);
 
}
 
}
 
render() {
render() {
return (
return (
<Map className='map' center={this.props.position} zoom={this.props.zoom}>
<Map className='map' center={this.props.position} zoom={this.props.zoom}>
@@ -14,9 +75,14 @@ class UserMap extends Component {
@@ -14,9 +75,14 @@ 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]}>
 
<Popup>
 
User's real position.<br />
 
</Popup>
 
</Marker>}
</Map>
</Map>
);
);
}
}
}
}
export default UserMap;
export default UserMap;
 
\ No newline at end of file
Loading