[英]How to get Latitude and Longitude from address using react-google-places-autocomplete
I am using this package react-google-places-autocomplete and need latitude and longitude from address.我正在使用这个 package react-google-places-autocomplete并且需要地址中的纬度和经度。 By their documentation, there is a function but it keep says
根据他们的文档,有一个 function 但它一直说
"This API project is not authorized to use this API. For more information on authentication and Google Maps JavaScript API services"
I enabled Geocoding and GeoLocating APIs but still getting same error.我启用了地理编码和地理定位 API,但仍然出现相同的错误。 Here is my code:
这是我的代码:
import GooglePlacesAutocomplete, { geocodeByAddress, getLatLng } from 'react-google-places-autocomplete';
const handleAddress = (e)=>{
setSelectedAddress(e);
geocodeByAddress('Montevideo, Uruguay')
.then(results => getLatLng(results[0]))
.then(({ lat, lng }) =>
console.log('Successfully got latitude and longitude', { lat, lng })
);
<GooglePlacesAutocomplete
apiKey={config.url.Google_key}
selectProps={{
placeholder: 'Address *',
name:"address",
inputValue:inputField['address'],
onInputChange : (e)=>{setInputField({...inputField, ['address']: e})},
onChange:(place) => {handleAddress(place.label); setErrorAddress(false);console.log(place)}
}}
/>
I am using the static address to test for now.我现在使用 static 地址进行测试。 Please let me know what i am doing wrong.
请让我知道我做错了什么。
onPlaceSelected={place =>
{
console.log('Lat', place.geometry.location.lat())
console.log('Lng', place.geometry.location.lng())
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.