[英]React leaflet map center not changing
I'm setting up a map to find the coords of a person and then put that location on the map.我正在设置一个 map 来查找一个人的坐标,然后将该位置放在 map 上。 But for some reason, the coords aren't being shown on the map.但由于某种原因,map 上没有显示坐标。 I console.log to make sure the state variables(where the coords are being stored) were emitting the right coords and they are.我 console.log 以确保 state 变量(存储坐标的位置)发出正确的坐标并且它们是正确的。 I don't know why the map won't change to them though.我不知道为什么 map 不会改变它们。
My code:我的代码:
import { StatusBar } from "expo-status-bar";
import React from "react";
import { StyleSheet, Text, View } from "react-native";
import { MapContainer, TileLayer, Marker, Popup } from "react-leaflet";
import Constants from "expo-constants";
import * as Location from "expo-location";
import * as Permissions from "expo-permissions";
import { render } from "react-dom";
import "leaflet/dist/leaflet.css";
export default class App extends React.Component {
constructor() {
super();
this.state = {
ready: false,
where: { lat: '', lng: '' },
error: null,
};
}
componentDidMount() {
let geoOptions = {
enableHighAccuracy: true,
timeOut: 20000,
maximumAge: 60 * 60 * 24,
};
this.setState({ ready: false, error: null });
navigator.geolocation.getCurrentPosition(
this.geoSuccess,
this.geoFailure,
geoOptions
);
}
geoSuccess = (position) => {
console.log(position.coords.latitude);
console.log(position.coords.longitude);
console.log(this.state.where?.lng);
console.log(this.state.where?.lat);
this.setState({
ready: true,
where: { lat: position.coords.latitude, lng: position.coords.longitude
},
});
console.log(this.state.where?.lng);
console.log(this.state.where?.lat);
};
geoFailure = (err) => {
this.setState({ error: err.message });
console.log(this.state.error);
};
render() {
const position = [this.state.where?.lat, this.state.where?.lng];
return (
<>
{(this.state.where != null || this.state.where != undefined) &&
<MapContainer
style={{ height: "100%", width: "100%" }}
center={position}
zoom="30"
scrollWheelZoom={true}
>
<TileLayer
attribution='© <a href="http://osm.org/copyright">OpenStreetMap</a> contributors'
url="https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png"
/>
</MapContainer>
}
</>
);
}
}
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.除了它的孩子, MapContainer 道具是不可变的:在它们第一次设置后更改它们不会对 Map 实例或其容器产生影响。
Use a child component that will change your map view upon position change使用将在 position 更改时更改您的 map 视图的子组件
function ChangeMapView({ coords }) {
const map = useMap();
map.setView(coords, map.getZoom());
return null;
}
Use it like this:像这样使用它:
<MapContainer
style={{ height: "100vh", width: "100%" }}
center={position}
zoom="30"
scrollWheelZoom={true}
>
<TileLayer
attribution='© <a href="http://osm.org/copyright">OpenStreetMap</a> contributors'
url="https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png"
/>
<ChangeMapView coords={position} />
</MapContainer>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.