Javascript 谷歌反向地理编码API-不返回一致数据

Javascript 谷歌反向地理编码API-不返回一致数据,javascript,reactjs,google-maps,Javascript,Reactjs,Google Maps,我正在React Native JS中开发一个应用程序,我正在使用谷歌反向地理编码API查找用户的城市、州、县、国家和邮政编码(必须包括县、州和国家)。我的问题是,我无法找到一种一致的方法来收集数据,因为谷歌每次返回的JSON文件的顺序取决于位置 我现在的代码是: fetch(`https://maps.googleapis.com/maps/api/geocode/json?${qs}`).then((res) => res.json()).then((json) => {

我正在React Native JS中开发一个应用程序,我正在使用谷歌反向地理编码API查找用户的城市、州、县、国家和邮政编码(必须包括县、州和国家)。我的问题是,我无法找到一种一致的方法来收集数据,因为谷歌每次返回的JSON文件的顺序取决于位置

我现在的代码是:

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;
     }

  }
fetch(`https://maps.googleapis.com/maps/api/geocode/json?${qs}`)。然后((res)=>res.json())。然后((json)=>{
//成功!
var city=false,state=false,country=false,country=false,zip=false;
for(var i=0;i

${qs}作为URL末尾的latlng和key。
我正在尝试查看结果并选择不同的类型,但这仍然不起作用。我还想让它在整个北美都起作用。

非常感谢所有反馈!!!谢谢!

我最终没能弄清楚。我最终换了到API,它正是我想做的。

好吧,最后我想不出来。我最终切换到API,它正是我想做的。

你可以这样做:

 _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);
    };
有关使用react中的google反向地理编码获取城市和其他详细信息的完整实现,请参阅


您可以这样做:

 _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);
    };
有关使用react中的google反向地理编码获取城市和其他详细信息的完整实现,请参阅


您能否发布您收到的JSON对象的1/2示例是的,两个示例,均未返回county、zip或city。第二个示例从第467行开始。您能否发布您收到的JSON对象的1/2示例是的,两个示例,均未返回county、zip或city。第二个示例从第467行开始