Skip to content
Snippets Groups Projects
Commit c3184e22 authored by M3034's avatar M3034
Browse files

Merge branch 'get-own-location' into 'development'

Get own location

See merge request !3
parents 45408cf6 9a4ec122
No related branches found
No related tags found
1 merge request!3Get own location
...@@ -8,22 +8,89 @@ import DrawTools from './DrawTools.js' ...@@ -8,22 +8,89 @@ import DrawTools from './DrawTools.js'
class UserMap extends Component { class UserMap extends Component {
render() { constructor(props){
return ( super(props);
<Map this.state = {
className="map" ownLat: null,
center={this.props.position} ownLng: null,
zoom={this.props.zoom} }
>
<TileLayer this.DeviceLocationUpdater = null;
attribution='Maanmittauslaitoksen kartta' this.updateDeviceLocation = this.updateDeviceLocation.bind(this);
url=" https://tiles.kartat.kapsi.fi/peruskartta/{z}/{x}/{y}.png" }
/>'
<ZoomControl position='topright' /> componentDidMount(){
<DrawTools position={this.props.position} /> this.startDeviceLocationUpdater();
</Map> }
)
} 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() {
return (
<Map className='map' center={this.props.position} zoom={this.props.zoom}>
<TileLayer
attribution='Maanmittauslaitoksen kartta'
url=' https://tiles.kartat.kapsi.fi/taustakartta/{z}/{x}/{y}.jpg'
/>
<ZoomControl position='topright' />
<DrawTools position={this.props.position} />
<Marker position={this.props.position}>
<Popup>
Se on perjantai, my dudes <br />
</Popup>
</Marker>
{this.state.ownLat !== null && <Marker position={[this.state.ownLat, this.state.ownLng]}>
<Popup>
User's real position.<br />
</Popup>
</Marker>}
</Map>
);
}
} }
export default UserMap; export default UserMap;
\ No newline at end of file
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