我正试着用react地图通过坐标来获得地址。我知道如何在功能组件中这样做。但是我真的需要使用类组件来代替。我要去找马。但不知道该怎么做。找不到解释得很清楚的文件。
这是我的代码:
import React, { Component } from 'react'
import { YMaps, Map, ZoomControl, FullscreenControl, SearchControl, GeolocationControl, Placemark } from "react-yandex-maps";
export default class YMap extends Component {
constructor(props) {
super(props)
this.state = {
coords: [],
mapState: {
center: [41.2825125, 69.1392826],
zoom: 9
},
}
}
componentDidUpdate(prevProps, prevState) {
if (prevProps.oldCoords !== this.props.oldCoords) {
this.setState({ coords: this.props.oldCoords })
}
}
onMapClick = (e) => {
const coords = e.get("coords");
this.setState({ coords: coords })
};
render() {
return (
<div>
<YMaps query={{ apikey: "" }}>
<Map
modules={["Placemark", "geocode", "geoObject.addon.balloon"]}
onClick={this.onMapClick}
state={this.state.mapState}
width='100%'
height='500px'
>
{this.state.coords ? <Placemark geometry={this.state.coords} /> : null}
<ZoomControl />
<FullscreenControl />
<SearchControl />
<GeolocationControl />
</Map>
</YMaps>
</div>
)
}
}
发布于 2022-11-30 06:20:51
如果要查找单击位置的坐标,可以从Map组件获取坐标。它返回点击位置的坐标,如下所示
<Map onClick={(e)=>props.set(e._sourceEvent.originalEvent.coords)}/>
更新状态后,可以使用坐标放置placemark。
https://stackoverflow.com/questions/64626103
复制相似问题