简体   繁体   English

如何使用 react-google-places-autocomplete 从地址获取纬度和经度

[英]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.

相关问题 从 react-google-places-autocomplete 获取所选项目 - Get selected item from react-google-places-autocomplete React-google-places-autocomplete street_address 组件不工作? - React-google-places-autocomplete street_address component not working? 如何将 react-google-places-autocomplete 与 react-hook-from 集成? - How to integrate react-google-places-autocomplete with react-hook-from? OnSelect 不适用于 react-Google-places-autocomplete - OnSelect dosnt work with react-Google-places-autocomplete 如何让一个地方的纬度和经度反应谷歌地图? - How to get the latitude and longitude of a place react google maps? 'google-map-react' 中的标记如何根据道具的纬度和经度获取其位置? - How does a marker in 'google-map-react' get its location based on latitude and longitude from props? 如何获取带有经纬度的地址? - How do I get the address with the latitude and longitude? 如何从反应中的邮政(邮政编码)获取 position(纬度和经度)? - How to get position (latitude and longitude) from a postal (zip) code in react? 有什么方法可以从React JS Google Map中的经度和纬度位置获取位置名称 - is there any way to get the location name from the longitude and latitude positions in react js google map 谷歌在反应中放置自动完成,不要在输入的地方显示完整地址 - Google places autocomplete in react, don't display full address in places input
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM