[英]Google Reverse Geocode API - not returning consistent data
I am developing an app in React Native JS and I am using Googles reverse geocode API to find the city, state, county, country, & zip code ( must include county, state, and country ) of a user. 我正在使用React Native JS开发一个应用,并且正在使用Google的反向地理编码API查找用户的城市,州,县,国家和邮政编码( 必须包括县,州和国家 )。 My problem is that I can not figure out a consistent way to gather the data because Google returns a JSON file with different order each time depending on the location.
我的问题是我无法找出一种一致的方式来收集数据,因为Google每次都会根据位置返回一个具有不同顺序的JSON文件。
My code for now is: 我现在的代码是:
fetch(`https://maps.googleapis.com/maps/api/geocode/json?${qs}`).then((res) => res.json()).then((json) => {
//success!
var city=false,state=false,county=false,country=false,zip=false;
for (var i = 0; i < json.results.length; i++) {
if ((!city || !state) && json.results[i].types[0] === "locality") {
city = json.results[i].address_components[0].short_name,
state = json.results[i].address_components[2].short_name;
} else if (!county && json.results[i].types[0] === "administrative_area_level_2") {
county = json.results[i].address_components[0].short_name;
} else if (!state && json.results[i].types[0] === "administrative_area_level_1") {
state = json.results[i].address_components[0].short_name;
} else if (!county && json.results[i].types[0] === "county") {
county = json.results[i].address_components[0].short_name;
} else if (!country && json.results[i].types[0] === "country") {
country = json.results[i].address_components[0].short_name;
}
}
${qs} being the latlng and key at the end of the URL. $ {qs}是URL末尾的latlng和键。
I am trying to go through the results and was picking out different types, but this still does not work. 我试图查看结果,并选择了不同的类型,但这仍然行不通。 I also want to get this to work in all of North America..
我也想让它在整个北美运作。
All feedback is greatly appreciated!!! 所有反馈非常感谢!!! Thank you!
谢谢!
Well I couldn't figure it out in the end. 好吧,我最终无法弄清楚。 I ended up switching to https://locationiq.org/ API and it does exactly what I was trying to.
我最终切换到https://locationiq.org/ API,它完全符合我的尝试。
You can do something like this: 您可以执行以下操作:
_geoCodeCallback = (results, status, event) => {
const google = window.google; // eslint-disable-line
if (status === google.maps.GeocoderStatus.OK) {
if (results[0]) {
const add = results[0].formatted_address;
const value = add.split(",");
const count = value.length;
const city = value[count - 3];
// here we can dispatch action to search by city name and autofill the autocomplete
} else {
console.log("address not found");
}
} else {
console.log(status);
}
}
_geocodeAddress = (geocoder, latlng) => {
geocoder.geocode({ location: latlng }, this._geoCodeCallback);
}
if (navigator.geolocation) {
this._displayLocation = (latitude, longitude) => {
const geocoder = new google.maps.Geocoder();
const latlng = new google.maps.LatLng(latitude, longitude);
this._geocodeAddress(geocoder, latlng);
};
For entire implementation of fetching city and other details using google reverse geo coding in react refer to 有关使用Google反向地理编码在React中获取城市和其他详细信息的完整实现,请参见
https://medium.com/@eesh.t/auto-detect-location-react-component-using-google-geocoding-and-reverse-geocoding-in-autocomplete-66a269c59315 https://medium.com/@eesh.t/auto-detect-location-react-component-using-google-geocoding-and-reverse-geocoding-in-autocomplete-66a269c59315
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.