Google API Address Components

前端 未结 3 1611
借酒劲吻你
借酒劲吻你 2020-12-12 07:02

I am attempting to obtain the address components using the Google Maps API however am unable to properly parse results. My code is as follows:

    // Ajax C         


        
3条回答
  •  独厮守ぢ
    2020-12-12 07:11

    I'm assuming what you are doing is a reverse geocode.

    The object returned looks something like this (from the example):

    {
      "address_components": [
        {
          "long_name": "Grand St/Bedford Av",
          "short_name": "Grand St/Bedford Av",
          "types": [
            "bus_station",
            "transit_station",
            "establishment"
          ]
        },
        {
          "long_name": "Williamsburg",
          "short_name": "Williamsburg",
          "types": [
            "neighborhood",
            "political"
          ]
        },
        {
          "long_name": "Brooklyn",
          "short_name": "Brooklyn",
          "types": [
            "sublocality_level_1",
            "sublocality",
            "political"
          ]
        },
    ...
    

    There is no direct way to access a result's street, house number etc. because each part of the address can be several types at the same time. You would have to iterate over the address_components and filter according to what you need.

    Here's another Stackoverflow thread covering this topic: get city from geocoder results?

提交回复
热议问题