我有一个react 应用程序,我有一个谷歌 map 组件.
这一条:
<GoogleMap
center={center}
zoom={3}
onLoad={(map) => setMap(map)}
mapContainerStyle={{ width: '100%', height: '100%' }}
>
{data.toDisplayOnMap.length > 0 && (
<>
{data.toDisplayOnMap.map((element, i) => {
if ('events' in element.item) {
return <Marker key={i} position={element.item.locationCoords} onClick={() => navigate(`/location?id=${element.id}`)} icon={`http://labs.google.com/ridefinder/images/mm_20_red.png`} />
} else if ('waypoints' in element.item) {
if (element.item.traveltype === 'location') {
return <DirectionsRenderer options={{ preserveViewport: true, polylineOptions: { strokeColor: 'blue', strokeWeight: 2 }, markerOptions: { icon: `http://labs.google.com/ridefinder/images/mm_20_yellow.png` } }} directions={element.route} />
} else {
let wps = element.item.waypoints;;
let coords = [];
wps.forEach((wp) => {
wp.coords.forEach((coord) => {
coords.push(coord)
})
})
return (<>
<Marker position={coords[0]} icon={`http://labs.google.com/ridefinder/images/mm_20_yellow.png`} />
<Marker position={coords[coords.length - 1]} icon={`http://labs.google.com/ridefinder/images/mm_20_yellow.png`} />
<Polyline path={coords} options={{ geodesic: true, strokeColor: '#000', strokeOpacity: 1, strokeWeight: 2, editable: false, draggable: false, clickable: true }} />
</>)
}
}
})}
</>
)}
</GoogleMap>
现在的情况是,我有一个物体列表,如果人们选中一个复选框,它就会显示在 map 上,但我的中心在非洲,放大了3,这样我就可以最大限度地了解 map .如果我放大以查看该标记或该折线,然后取消选中该复选框,它会重新加载 map 并将我移回非洲.我真的不知道如何将它与useMemo或类似的东西一起使用,因为我 map 上显示的内容来自页面的状态.我试过使用Memo,但可能我用错了,因为它什么都不做.在我的控制台里,我也有这个
253 useJsApiLoader.tsx:62 Performance warning! LoadScript has been reloaded unintentionally!
我是这样宣布我的 map 的:
const [map, setMap] = useState(/** @type google.maps.Map */(null))
个
const { isLoaded } = useJsApiLoader({
googleMapsApiKey: ct.MAPS_API_KEY,
libraries: p
})
请帮我解决这个问题.