react-google-maps 如何获得标记位置?
react-google-maps how does one get marker position?
我阅读了文档,它方便地概述了可用的道具和方法。请看here.
我的问题是,鉴于此处的示例组件:
import {withScriptjs, withGoogleMap, GoogleMap, Marker} from "react-google-maps";
class MyParentComponentWrapper extends Component {
...
...// inside react component class
mapComponent() {
const MyMapComponent = withScriptjs(withGoogleMap((props) =>{
return (
<GoogleMap
defaultZoom={18}
defaultCenter={{ lat: props.lat, lng: props.lng }}
>
{ props.isMarkerShown && <Marker onPositionChanged={()=>{
// This event will trigger the
// call to update the state where lat and lng will go.
}} draggable position={{ lat: props.lat, lng: props.lng }} /> }
</GoogleMap>
)
}))
return (
<MyMapComponent
lat={this.state.form.location.latitude}
lng={this.state.form.location.longitude}
googleMapURL={`https://maps.googleapis.com/maps/api/js?key=${env.google.apiKey}&v=3.exp&libraries=geometry,drawing,places`}
loadingElement={<div style={{ height: `100%` }} />}
containerElement={<div style={{ height: `400px` }} />}
mapElement={<div style={{ height: `100%` }} />}
isMarkerShown/>
)
}
...
看到 onPositionChanged
事件了吗?我想更新包含 lat 和 lng 的 MyParentComponentWrapper
的状态,但我不知道如何调用 getPosition()
来获取 lat lng 值。没有提供示例,文档看起来也不清楚……有没有办法调用我不知道的 Marker
组件中的方法?如果您知道如何执行此操作,能否提供一个有关如何执行此操作的示例?
onPositionChanged
事件不提供触发事件,但您可以通过 ref
属性存储对 Marker
组件的引用:
<GoogleMap defaultZoom={8} defaultCenter={{ lat: -34.397, lng: 150.644 }}>
<Marker position={{ lat: -34.397, lng: 150.644 }} draggable={true} ref={props.onMarkerMounted} onPositionChanged={props.onPositionChanged} />
</GoogleMap>
然后获取标记在 onPositionChanged
事件中的位置,如下所示:
lifecycle({
componentWillMount() {
const refs = {}
this.setState({
onMarkerMounted: ref => {
refs.marker = ref;
},
onPositionChanged: () => {
const position = refs.marker.getPosition();
console.log(position.toString());
}
})
},
})
我阅读了文档,它方便地概述了可用的道具和方法。请看here.
我的问题是,鉴于此处的示例组件:
import {withScriptjs, withGoogleMap, GoogleMap, Marker} from "react-google-maps";
class MyParentComponentWrapper extends Component {
...
...// inside react component class
mapComponent() {
const MyMapComponent = withScriptjs(withGoogleMap((props) =>{
return (
<GoogleMap
defaultZoom={18}
defaultCenter={{ lat: props.lat, lng: props.lng }}
>
{ props.isMarkerShown && <Marker onPositionChanged={()=>{
// This event will trigger the
// call to update the state where lat and lng will go.
}} draggable position={{ lat: props.lat, lng: props.lng }} /> }
</GoogleMap>
)
}))
return (
<MyMapComponent
lat={this.state.form.location.latitude}
lng={this.state.form.location.longitude}
googleMapURL={`https://maps.googleapis.com/maps/api/js?key=${env.google.apiKey}&v=3.exp&libraries=geometry,drawing,places`}
loadingElement={<div style={{ height: `100%` }} />}
containerElement={<div style={{ height: `400px` }} />}
mapElement={<div style={{ height: `100%` }} />}
isMarkerShown/>
)
}
...
看到 onPositionChanged
事件了吗?我想更新包含 lat 和 lng 的 MyParentComponentWrapper
的状态,但我不知道如何调用 getPosition()
来获取 lat lng 值。没有提供示例,文档看起来也不清楚……有没有办法调用我不知道的 Marker
组件中的方法?如果您知道如何执行此操作,能否提供一个有关如何执行此操作的示例?
onPositionChanged
事件不提供触发事件,但您可以通过 ref
属性存储对 Marker
组件的引用:
<GoogleMap defaultZoom={8} defaultCenter={{ lat: -34.397, lng: 150.644 }}>
<Marker position={{ lat: -34.397, lng: 150.644 }} draggable={true} ref={props.onMarkerMounted} onPositionChanged={props.onPositionChanged} />
</GoogleMap>
然后获取标记在 onPositionChanged
事件中的位置,如下所示:
lifecycle({
componentWillMount() {
const refs = {}
this.setState({
onMarkerMounted: ref => {
refs.marker = ref;
},
onPositionChanged: () => {
const position = refs.marker.getPosition();
console.log(position.toString());
}
})
},
})