问题描述
我有一个使用react-google-map插件渲染的地图。流程是拖动标记,并根据标记位置获取纬度,经度和地址。我只得到经纬度。我如何获得地址?
这是我的下面的代码。
import React from "react"
import { compose,withProps,lifecycle } from "recompose"
import { withScriptjs,withGoogleMap,GoogleMap,Marker } from "react-google-maps"
import "./config";
const MyMapComponent = compose(
withProps({
googleMapURL: "https://maps.googleapis.com/maps/api/js?key=AIzaSyDzzi_VBcf2Oef6LTViLU767UPNHlnIze4&libraries=geometry,drawing,places",loadingElement: <div style={{ height: `100%` }} />,containerElement: <div style={{ height: `400px` }} />,mapElement: <div style={{ height: `100%` }} />,}),lifecycle({
componentwillMount() {
const refs = {}
this.setState({
position: null,onMarkerMounted: ref => {
refs.marker = ref;
},onPositionChanged: () => {
const position = refs.marker.getPosition();
console.log(position.toString());
}
})
},withScriptjs,withGoogleMap
)((props) =>
<GoogleMap defaultZoom={8} defaultCenter={{ lat: -34.397,lng: 150.644 }}>
{props.isMarkerShown && <Marker position={{ lat: -34.397,lng: 150.644 }} draggable={true} ref={props.onMarkerMounted} onPositionChanged={props.onPositionChanged} />}
</GoogleMap>
)
class custommap extends React.PureComponent {
state = {
isMarkerShown: false,}
render() {
return (
<div>
<MyMapComponent isMarkerShown={true} />
</div>
)
}
}
export default custommap;
感谢任何帮助,
解决方法
我刚刚弄清楚了。我使用react-geocode插件。
Geocode.fromLatLng(position.lat(),position.lng()).then(
response => {
const address = response.results[0].formatted_address;
console.log(address);
},error => {
console.error(error);
}
);