2

我是 ReactJS 的新手。我通过引用此链接使用了地图

它工作正常。我在搜索栏中检测到我的位置图标。单击时,我能够检测位置并控制基于纬度和经度的格式化地址。我怎么想在搜索位置输入框中显示地址。我如何做到这一点?

应用程序.js

import React from 'react';
import MapWithASearchBox from '../components/main_layout/location';
class App extends React.Component {
state = {       
    latitude : '',
    longitude: '',          
};  
 eventHandler() {
    if(navigator.geolocation) {
        navigator.geolocation.getCurrentPosition((position) => {                
            this.setState({
                latitude:position.coords.latitude,
                longitude:position.coords.longitude,});
                this.myfunc(position.coords.latitude, 
                position.coords.longitude)  
            },                  
)        
}
}
  myfunc = (lat, long) => { 

 var geocoder = new window.google.maps.Geocoder();
 console.log(geocoder)

 var latlng = {lat: parseFloat(lat), lng: parseFloat(long)};
geocoder.geocode({'location': latlng}, function(results, status) {
if (status === 'OK') {
  if (results[0]) { 

    const addr = results[0].formatted_address;
    console.log(addr)
  } else {
    window.alert('No results found');
  }
} else {
  window.alert('Geocoder failed due to: ' + status);
}
});  
}
render() {          
    return (
        <div>               
            <MapWithASearchBox />               
        </div>
    );
}
}
export default App;
4

0 回答 0