猿问

从这个 google 地方 API 地址结果中提取地址片段的最佳方法是什么?

这可能是一个简单的 JSON 相关问题。我成功地从 Google Places API 接收到一个 JSON 对象,它看起来与 Google Places API 文档中的这个示例完全一样。我需要将每个地址组件(街道号码、城市、州、邮编等)分配给我的代码中的单独变量。例如,我如何最好地参考 street_number ?


这是我现在的做法,但它不起作用,因为例如,如果我的地址没有“楼层”,那么我将不得不引用address_components[0]而不是address_components[1]. 这是我现在无法使用的方式:


const MyStreetNumber =  details.address_components[1].long_name

console.log(MyStreetNumber) //logs 48

这是“详细信息”的定义(我从 Google Places API 收到的响应):


  {

   "html_attributions" : [],

   "result" : {

      "address_components" : [

         {

            "long_name" : "5",

            "short_name" : "5",

            "types" : [ "floor" ]

         },

         {

            "long_name" : "48",

            "short_name" : "48",

            "types" : [ "street_number" ]

         },

         {

            "long_name" : "Pirrama Road",

            "short_name" : "Pirrama Rd",

            "types" : [ "route" ]

         },

         {

            "long_name" : "Pyrmont",

            "short_name" : "Pyrmont",

            "types" : [ "locality", "political" ]

         },

         {

            "long_name" : "Council of the City of Sydney",

            "short_name" : "Sydney",

            "types" : [ "administrative_area_level_2", "political" ]

         },

         {

            "long_name" : "New South Wales",

            "short_name" : "NSW",

            "types" : [ "administrative_area_level_1", "political" ]

         },

         {

            "long_name" : "Australia",

            "short_name" : "AU",

            "types" : [ "country", "political" ]

         },

         {

            "long_name" : "2009",

            "short_name" : "2009",

            "types" : [ "postal_code" ]

         }

      ],

   

慕少森
浏览 127回答 3
3回答

拉莫斯之舞

有点晚了,但如果它对任何人都有帮助,可以这样做:如果您只需要第一个结果function handleGooglePlacesPress = results => {  const map = {    street_number: 'streetNumber',    route: 'streetName',    sublocality_level_1: 'city',    locality: 'city1',    administrative_area_level_1: 'state',    postal_code: 'postalCode',    country: 'country'  };  const brokenDownAddress = {};  results[0].address_components.forEach(component => {    brokenDownAddress[map[component.types[0]]] = component.long_name;  }  return brokenDownAddress;}

慕田峪4524236

您可以使用 for 循环并检查types值,这是一个简单的示例:var add_comps = json['result']['address_components'];for (var i = 0; i < add_comps.length; i++) {&nbsp; if (add_comps[i]['types'] == 'street_number') {&nbsp; &nbsp; console.log(i);&nbsp; }}

眼眸繁星

好的,所以我最终只是循环通过 details.address_components 来分配变量。我检查了两次城市,因为有时它被称为“locality”,有时它被称为“subLocality_level_1”,具体取决于我相信的国家:function handleGooglePlacesPress(data,details){&nbsp;var StreetNumber = null;&nbsp;var StreetName = null;&nbsp;var City = null;&nbsp;var State = null;&nbsp;var Country = null;&nbsp;var PostalCode = null;&nbsp; const addressComponents = details.address_components;&nbsp; addressComponents.forEach(addressComponent => {&nbsp;&nbsp; &nbsp; &nbsp; const addressType = addressComponent.types[0]&nbsp; &nbsp; &nbsp; if(addressType == 'street_number'){&nbsp; &nbsp; &nbsp; &nbsp; StreetNumber = addressComponent.long_name;&nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; if(addressType == 'route'){&nbsp; &nbsp; &nbsp; &nbsp; StreetName = addressComponent.long_name;&nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; if(addressType == 'sublocality_level_1'){&nbsp; &nbsp; &nbsp; &nbsp; City = addressComponent.long_name;&nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; if(addressType == 'locality'){&nbsp; &nbsp; &nbsp; &nbsp; City = addressComponent.long_name;&nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; if(addressType == 'administrative_area_level_1'){&nbsp; &nbsp; &nbsp; &nbsp; State = addressComponent.long_name&nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; if(addressType == 'postal_code'){&nbsp; &nbsp; &nbsp; &nbsp; PostalCode = addressComponent.long_name&nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; if(addressType == 'country'){&nbsp; &nbsp; &nbsp; &nbsp; Country = addressComponent.long_name&nbsp; &nbsp; &nbsp; }&nbsp; });console.log(StreetNumber,StreetName,City,State,PostalCode,Country) //logs each address piece
随时随地看视频慕课网APP

相关分类

JavaScript
我要回答