如何从react中的邮政编码中获取位置(纬度和经度)



我需要知道从react中的邮政编码中获取纬度和经度的最佳方法是什么?

我尝试使用谷歌API的地理代码(使用npm包react-geocode(,但我不能仅根据邮政编码进行搜索。

所以我需要你对如何做到这一点的意见。源代码示例也将不胜感激。

提前感谢您的评论。当做

也许可以尝试这样的方法,并根据您的需求进行调整,因为只给出了几个上下文:

var lat = '';
var lng = '';
// depends on whether you really only have the zip code
var address = {zipcode} or {city and state};
geocoder.geocode({ 'address': address}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
lat = results[0].geometry.location.lat();
lng = results[0].geometry.location.lng();
});
} else {
console.log("Geocode was not successful for the following reason: " + status);
}
});
console.log('Latitude: ' + lat + ' Longitude: ' + lng);
// using react-geocode package I was able to successfully capture lat lng of Pincode.  
GeoCode.setApiKey("YOUR API KEY");
GeoCode.setLanguage("en");
GeoCode.setRegion("in");

const getGeoCode = (e) => {
let zipCode = e.target.value;
const latitude = ''

GeoCode.fromAddress(zipCode).then(({ results }) => {
const { lat, lng } = results[0].geometry.location;
createTicketFormik.values.lat = lat;
createTicketFormik.values.lng = lng;
});
};
<TextField
fullWidth
type="number"
size="small"
name="postalCode"
InputLabelProps={{ style: { fontSize: 14 } }}
label="Postal Code"             
onInput={getGeoCode}
/>

最新更新