I’m setting up a map to find the coords of a person and then put that location on the map. But for some reason, the coords aren’t being shown on the map. I console.log to make sure the state variables(where the coords are being stored) were emitting the right coords and they are. I don’t know why the map won’t change to them though.
My code:
JavaScript
x
80
80
1
import { StatusBar } from "expo-status-bar";
2
import React from "react";
3
import { StyleSheet, Text, View } from "react-native";
4
import { MapContainer, TileLayer, Marker, Popup } from "react-leaflet";
5
import Constants from "expo-constants";
6
import * as Location from "expo-location";
7
import * as Permissions from "expo-permissions";
8
import { render } from "react-dom";
9
10
import "leaflet/dist/leaflet.css";
11
12
export default class App extends React.Component {
13
constructor() {
14
super();
15
this.state = {
16
ready: false,
17
where: { lat: '', lng: '' },
18
error: null,
19
};
20
}
21
22
componentDidMount() {
23
let geoOptions = {
24
enableHighAccuracy: true,
25
timeOut: 20000,
26
maximumAge: 60 * 60 * 24,
27
};
28
this.setState({ ready: false, error: null });
29
navigator.geolocation.getCurrentPosition(
30
this.geoSuccess,
31
this.geoFailure,
32
geoOptions
33
);
34
}
35
geoSuccess = (position) => {
36
console.log(position.coords.latitude);
37
console.log(position.coords.longitude);
38
console.log(this.state.where?.lng);
39
console.log(this.state.where?.lat);
40
41
42
this.setState({
43
ready: true,
44
where: { lat: position.coords.latitude, lng: position.coords.longitude
45
},
46
47
});
48
console.log(this.state.where?.lng);
49
console.log(this.state.where?.lat);
50
};
51
geoFailure = (err) => {
52
this.setState({ error: err.message });
53
console.log(this.state.error);
54
};
55
56
57
58
render() {
59
const position = [this.state.where?.lat, this.state.where?.lng];
60
return (
61
<>
62
{(this.state.where != null || this.state.where != undefined) &&
63
<MapContainer
64
style={{ height: "100%", width: "100%" }}
65
center={position}
66
zoom="30"
67
scrollWheelZoom={true}
68
69
>
70
<TileLayer
71
attribution='© <a href="http://osm.org/copyright">OpenStreetMap</a> contributors'
72
url="https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png"
73
/>
74
</MapContainer>
75
}
76
</>
77
);
78
}
79
}
80
Advertisement
Answer
From the official docs
Except for its children, MapContainer props are immutable: changing them after they have been set a first time will have no effect on the Map instance or its container.
Use a child component that will change your map view upon position change
JavaScript
1
7
1
function ChangeMapView({ coords }) {
2
const map = useMap();
3
map.setView(coords, map.getZoom());
4
5
return null;
6
}
7
Use it like this:
JavaScript
1
13
13
1
<MapContainer
2
style={{ height: "100vh", width: "100%" }}
3
center={position}
4
zoom="30"
5
scrollWheelZoom={true}
6
>
7
<TileLayer
8
attribution='© <a href="http://osm.org/copyright">OpenStreetMap</a> contributors'
9
url="https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png"
10
/>
11
<ChangeMapView coords={position} />
12
</MapContainer>
13