Skip to content
Snippets Groups Projects
Commit a7c23d50 authored by Taneli Riihimäki's avatar Taneli Riihimäki
Browse files

Update the markers position on map when the geolocation changes

parent dbb65b50
No related branches found
No related tags found
1 merge request!21Development
...@@ -3,45 +3,44 @@ import { ...@@ -3,45 +3,44 @@ import {
Map, Map,
TileLayer, TileLayer,
Marker, Marker,
Popup Popup,
LayersControl,
GeoJSON
} from 'react-leaflet' } from 'react-leaflet'
import DrawTools from './DrawTools' import DrawTools from './DrawTools'
class UserMap extends Component { class UserMap extends Component {
constructor(props){ constructor(props){
super(props); super(props);
this.state = { this.state = {
ownLat: null, ownLat: null,
ownLng: null, ownLng: null,
mapUrl: 'https://tiles.kartat.kapsi.fi/taustakartta/{z}/{x}/{y}.jpg', mapUrl: 'https://tiles.kartat.kapsi.fi/taustakartta/{z}/{x}/{y}.jpg',
marker: { geojsonLayer: {
type: "FeatureCollection", "type": "FeatureCollection",
latitude: "", "latitude": "",
longitude: "", "longitude": "",
timestamp: "", "timestamp": "",
features: [{ "features": [{
type: "Feature", "type": "Feature",
properties: {}, "properties": {},
geometry: { "geometry": {
type: "Point", "type": "Point",
coordinates: [ "coordinates": [ 25.7597186, 62.20416479 ]
62.2416479,
25.7597186
]
} }
}] }]
} }
} }
this.watchPositionId = null; this.watchPositionId = null;
} }
componentDidMount(){ componentDidMount(){
console.log(JSON.stringify(this.state.marker))
this.getCurrentPosition((position) => { this.getCurrentPosition((position) => {
this.setCurrentPosition(position); this.setCurrentPosition(position);
}); });
console.log(JSON.stringify(this.state.marker))
//sendGeoJSON() //sendGeoJSON()
} }
// Sends the players marker to the backend // Sends the players marker to the backend
...@@ -53,7 +52,7 @@ class UserMap extends Component { ...@@ -53,7 +52,7 @@ class UserMap extends Component {
Accept: 'application/json', Accept: 'application/json',
'Content-Type': 'application/json' 'Content-Type': 'application/json'
}, },
body: JSON.stringify(this.state.marker) body: JSON.stringify(this.state.geojsonLayer)
}) })
.then(res => res.json()) .then(res => res.json())
.then(result => console.log(result)) .then(result => console.log(result))
...@@ -65,26 +64,27 @@ class UserMap extends Component { ...@@ -65,26 +64,27 @@ class UserMap extends Component {
navigator.geolocation.clearWatch(this.watchPositionId); navigator.geolocation.clearWatch(this.watchPositionId);
} }
} }
// Sets the state for the changed position
setCurrentPosition(position){ setCurrentPosition(position){
this.setState({ this.setState({
ownLat: position.coords.latitude, ownLat: position.coords.latitude,
ownLng: position.coords.longitude, ownLng: position.coords.longitude,
marker: { geojsonLayer: {
features: [ "type": "FeatureCollection",
{ "latitude": "",
geometry: { "longitude": "",
type: "Point", "timestamp": "",
coordinates: [ "features": [{
position.coords.latitude, "type": "Feature",
position.coords.longitude "properties": {},
] "geometry": {
} "type": "Point",
} "coordinates": [ position.coords.latitude, position.coords.longitude ]
] }
}]
} }
}); });
console.log(this.state.marker) console.log(this.state.geojsonLayer.coordinates)
} }
getCurrentPosition(callback){ getCurrentPosition(callback){
...@@ -104,7 +104,7 @@ class UserMap extends Component { ...@@ -104,7 +104,7 @@ class UserMap extends Component {
this.watchPositionId = navigator.geolocation.watchPosition((position) =>{ this.watchPositionId = navigator.geolocation.watchPosition((position) =>{
//success //success
if(position != null){ if(position != null){
callback(position); callback(position);
} }
}, (error) =>{ }, (error) =>{
console.log(error); console.log(error);
...@@ -112,57 +112,22 @@ class UserMap extends Component { ...@@ -112,57 +112,22 @@ class UserMap extends Component {
} }
} }
positionToGeoJSON(position){
let geoJSON = {
type: "Feature",
properties: {},
geometry: {
type: "Point",
coordinates: [position.coords.longitude, position.coords.latitude]
}
}
return JSON.stringify(geoJSON);
}
mockGeoJSON(){
return (JSON.stringify({
type: "FeatureCollection",
latitude: "",
longitude: "",
timestamp: "",
features: [{
type: "Feature",
properties: {},
geometry: {
type: "Point",
coordinates: [
62.2416479,
25.7597186
]
}
}]
}))
}
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}>
<TileLayer <TileLayer
attribution='&copy; <a href="https://www.maanmittauslaitos.fi/">Maanmittauslaitos</a>' attribution='&copy; <a href="https://www.maanmittauslaitos.fi/">Maanmittauslaitos</a>'
url={this.props.mapUrl} url={this.props.mapUrl}
/> />
<DrawTools position={this.props.position} /> <DrawTools position={this.props.position} />
<Marker position={this.props.position}> <Marker position={this.props.position}>
<Popup maxWidth="600px"> <Popup maxWidth="600px">
<img src="wimma.png" alt="Wimma Lab" width="600px"/> <img src="wimma.png" alt="Wimma Lab" width="600px"/>
</Popup> </Popup>
</Marker> </Marker>
{this.state.ownLat !== null && <Marker position={[this.state.ownLat, this.state.ownLng]}> {this.state.ownLat !== null && <Marker position={[this.state.ownLat, this.state.ownLng]}>
<Popup> </Marker>}
User's real position.<br /> <GeoJSON key={JSON.stringify(this.state.geojsonLayer)} data={this.state.geojsonLayer} />
</Popup>
</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