我正在尝试在地图中显示多个标记。
我在父组件中有这段代码:
const [coords, setCoords] = useState ([{}])
setCoords(
[
{lat: -5.2119899, lng: 119.4461816},
{lat: -5.209704, lng: 119.44075},
]
)
<ReactGoogleMaps coordinates={coords} />
子组件中的代码片段:
const WrappedMap = withScriptjs(withGoogleMap((props) =>
<GoogleMap
defaultZoom={13}
defaultCenter={{ lat: Number(props.coordinates.lat), lng: Number(props.coordinates.lng) }}
>
{props.isMarkerShown && <Marker position={{ lat: Number(props.coordinates.lat), lng: Number(props.coordinates.lng)}} />}
</GoogleMap>
));
function ReactGoogleMaps(props) {
return (
<div style={{width: "100%", height: "100%"}}>
<WrappedMap
coordinates={{lat: props.coordinates[0].lat, lng: props.coordinates[0].lng}}
/>
</div>
)
};
上面的代码运行良好。问题是当坐标属性的索引从 0 更改为 1 时,它会返回此错误:TypeError: Cannot read property 'lat' of undefined。
我已经坚持了几天。如果有人可以帮助我,我将非常感激。提前非常感谢你。
蓝山帝景
达令说
相关分类