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

First working connection with drawings db & back

parent 2be2026e
No related branches found
No related tags found
1 merge request!21Development
...@@ -26,17 +26,21 @@ class UserMap extends Component { ...@@ -26,17 +26,21 @@ class UserMap extends Component {
this.addToGeojsonLayer = this.addToGeojsonLayer.bind(this); this.addToGeojsonLayer = this.addToGeojsonLayer.bind(this);
this.setCurrentPosition = this.setCurrentPosition.bind(this); this.setCurrentPosition = this.setCurrentPosition.bind(this);
//this.setCurrentGeojson = this.setCurrentGeojson.bind(this);
this.watchPositionId = null; this.watchPositionId = null;
} }
componentDidMount() { componentDidMount() {
this.getCurrentPosition(position => { this.getCurrentPosition(position => {
//this.setCurrentPosition(position); this.setCurrentPosition(position);
}); });
//sendGeoJSON() this.fetchGeoJSON();
} }
// Sends the players marker to the backend // Sends the players marker to the backend
sendGeoJSON() { sendGeoJSON() {
console.log(
"Lähetettävät jutut: " + JSON.stringify(this.state.geoJSONLayer)
);
fetch("http://localhost:5000/mapmarkers/insertLocation", { fetch("http://localhost:5000/mapmarkers/insertLocation", {
method: "PUT", method: "PUT",
headers: { headers: {
...@@ -44,10 +48,38 @@ class UserMap extends Component { ...@@ -44,10 +48,38 @@ class UserMap extends Component {
Accept: "application/json", Accept: "application/json",
"Content-Type": "application/json" "Content-Type": "application/json"
}, },
body: this.state.geojsonLayer body: JSON.stringify(this.state.geoJSONLayer)
});
}
// Mikä vitum readablestream :(
fetchGeoJSON() {
fetch("http://localhost:5000/mapmarkers/getall", {
method: "GET",
headers: {
Authorization: "Bearer " + sessionStorage.getItem("token"),
Accept: "application/json",
"Content-Type": "application/json"
}
}) })
.then(res => res.json()) .then(res => res.json())
.then(result => console.log(result)); .then(data => {
let newFeatures = [];
data.map(item => {
newFeatures.push(item.features[0][0]);
return newFeatures;
});
this.setState({
geoJSONLayer: {
type: "FeatureCollection",
features: newFeatures
}
});
console.log(
"Geojsonlayer state fetchin jälkeen: " +
JSON.stringify(this.state.geoJSONLayer)
);
});
} }
componentWillUnmount() { componentWillUnmount() {
...@@ -56,15 +88,11 @@ class UserMap extends Component { ...@@ -56,15 +88,11 @@ class UserMap extends Component {
} }
} }
setCurrentPosition(data) { setCurrentPosition(position) {
this.setState({ this.setState({
...this.state, ownLat: position.coords.latitude,
geojsonLayer: data ownLng: position.coords.longitude
}); });
console.log(
"User mapin statessa oleva geojson: " +
JSON.stringify(this.state.geoJSONLayer)
);
} }
getCurrentPosition(callback) { getCurrentPosition(callback) {
...@@ -101,15 +129,16 @@ class UserMap extends Component { ...@@ -101,15 +129,16 @@ class UserMap extends Component {
} }
} }
// Function to be passed to DrawTools so it can add geojson data to this components state
addToGeojsonLayer(layerToAdd) { addToGeojsonLayer(layerToAdd) {
let oldFeatures = [...this.state.geoJSONFeatures]; let oldFeatures = [...this.state.geoJSONFeatures];
console.log(oldFeatures);
oldFeatures.push(layerToAdd); oldFeatures.push(layerToAdd);
const newFeatures = oldFeatures; const newFeatures = oldFeatures;
this.setState({ geoJSONlayer: { features: newFeatures } }); this.setState({ geoJSONLayer: { features: newFeatures } });
console.log( console.log(
"Geojsonlayer state: " + JSON.stringify(this.state.geoJSONlayer) "Geojsonlayer state: " + JSON.stringify(this.state.geoJSONLayer)
); );
this.sendGeoJSON();
} }
render() { render() {
...@@ -131,11 +160,7 @@ class UserMap extends Component { ...@@ -131,11 +160,7 @@ class UserMap extends Component {
position={this.props.position} position={this.props.position}
addToGeojsonLayer={this.addToGeojsonLayer} addToGeojsonLayer={this.addToGeojsonLayer}
/> />
<Marker position={this.props.position}>
<Popup>
Se on perjantai, my dudes <br />
</Popup>
</Marker>
{this.state.ownLat !== null && ( {this.state.ownLat !== null && (
<Marker position={[this.state.ownLat, this.state.ownLng]}> <Marker position={[this.state.ownLat, this.state.ownLng]}>
<Popup> <Popup>
...@@ -144,9 +169,12 @@ class UserMap extends Component { ...@@ -144,9 +169,12 @@ class UserMap extends Component {
</Popup> </Popup>
</Marker> </Marker>
)} )}
<GeoJSON data={this.state.geoJSONlayer} /> <GeoJSON
key={JSON.stringify(this.state.geoJSONLayer)}
data={this.state.geoJSONLayer}
/>
</Map> </Map>
); // this.state.geojsonLayer );
} }
} }
......
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