Google api地理编码仅获得一些值

问题描述 投票:-2回答:1

我正在使用google geocoding api。一切正常。但是,当我搜索1个位置时,会收到很多信息。例如,我只需要administrative_area_level_1

我使用的网址:

https://maps.googleapis.com/maps/api/geocode/json?latlng=12.1224221,-68.8824233&sensor=false&key=mykey:)

我收到此:

{
    "results": [
        {
            "address_components": [
                {
                    "long_name": "Kaya Toni Kunchi",
                    "short_name": "Kaya Toni Kunchi",
                    "types": [
                        "route"
                    ]
                },
                {
                    "long_name": "Willemstad",
                    "short_name": "Willemstad",
                    "types": [
                        "locality",
                        "political"
                    ]
                },
                {
                    "long_name": "Curaçao",
                    "short_name": "Curaçao",
                    "types": [
                        "administrative_area_level_1",
                        "political"
                    ]
                },
                {
                    "long_name": "Curaçao",
                    "short_name": "CW",
                    "types": [
                        "country",
                        "political"
                    ]
                }
            ],
            "formatted_address": "Kaya Toni Kunchi, Willemstad, Curaçao",
            "geometry": {
                "bounds": {
                    "northeast": {
                        "lat": 12.1237013,
                        "lng": -68.88351919999999
                    },
                    "southwest": {
                        "lat": 12.1234992,
                        "lng": -68.8842526
                    }
                },
                "location": {
                    "lat": 12.123662,
                    "lng": -68.88392089999999
                },
                "location_type": "GEOMETRIC_CENTER",
                "viewport": {
                    "northeast": {
                        "lat": 12.1249492302915,
                        "lng": -68.88253691970849
                    },
                    "southwest": {
                        "lat": 12.1222512697085,
                        "lng": -68.88523488029149
                    }
                }
            },
            "place_id": "ChIJNcHwFUB9g44RuzTE4FqQ3UE",
            "types": [
                "route"
            ]
        },
        {
            "address_components": [
                {
                    "long_name": "Santa Rosa",
                    "short_name": "Santa Rosa",
                    "types": [
                        "political",
                        "sublocality",
                        "sublocality_level_1"
                    ]
                },
                {
                    "long_name": "Willemstad",
                    "short_name": "Willemstad",
                    "types": [
                        "locality",
                        "political"
                    ]
                },
                {
                    "long_name": "Santa Maria",
                    "short_name": "Santa Maria",
                    "types": [
                        "administrative_area_level_2",
                        "political"
                    ]
                },
                {
                    "long_name": "Curaçao",
                    "short_name": "Curaçao",
                    "types": [
                        "administrative_area_level_1",
                        "political"
                    ]
                },
                {
                    "long_name": "Curaçao",
                    "short_name": "CW",
                    "types": [
                        "country",
                        "political"
                    ]
                }
            ],
            "formatted_address": "Santa Rosa, Willemstad, Curaçao",
            "geometry": {
                "bounds": {
                    "northeast": {
                        "lat": 12.1251383,
                        "lng": -68.8636779
                    },
                    "southwest": {
                        "lat": 12.1082708,
                        "lng": -68.8835479
                    }
                },
                "location": {
                    "lat": 12.1136041,
                    "lng": -68.87348109999999
                },
                "location_type": "APPROXIMATE",
                "viewport": {
                    "northeast": {
                        "lat": 12.1251383,
                        "lng": -68.8636779
                    },
                    "southwest": {
                        "lat": 12.1082708,
                        "lng": -68.8835479
                    }
                }
            },
            "place_id": "ChIJLbPjNBJ9g44R9mOepLEBeF4",
            "types": [
                "political",
                "sublocality",
                "sublocality_level_1"
            ]
        },
        {
            "address_components": [
                {
                    "long_name": "Willemstad",
                    "short_name": "Willemstad",
                    "types": [
                        "locality",
                        "political"
                    ]
                },
                {
                    "long_name": "Santa Maria",
                    "short_name": "Santa Maria",
                    "types": [
                        "administrative_area_level_2",
                        "political"
                    ]
                },
                {
                    "long_name": "Curaçao",
                    "short_name": "Curaçao",
                    "types": [
                        "administrative_area_level_1",
                        "political"
                    ]
                },
                {
                    "long_name": "Curaçao",
                    "short_name": "CW",
                    "types": [
                        "country",
                        "political"
                    ]
                }
            ],
            "formatted_address": "Willemstad, Curaçao",
            "geometry": {
                "bounds": {
                    "northeast": {
                        "lat": 12.1857187,
                        "lng": -68.8053561
                    },
                    "southwest": {
                        "lat": 12.0711749,
                        "lng": -68.9916729
                    }
                },
                "location": {
                    "lat": 12.1224221,
                    "lng": -68.8824233
                },
                "location_type": "APPROXIMATE",
                "viewport": {
                    "northeast": {
                        "lat": 12.1857187,
                        "lng": -68.8053561
                    },
                    "southwest": {
                        "lat": 12.0711749,
                        "lng": -68.9916729
                    }
                }
            },
            "place_id": "ChIJVy-mDF-ChI4R701VMbSSSR8",
            "types": [
                "locality",
                "political"
            ]
        },
        {
            "address_components": [
                {
                    "long_name": "Curaçao",
                    "short_name": "Curaçao",
                    "types": [
                        "administrative_area_level_1",
                        "political"
                    ]
                },
                {
                    "long_name": "Curaçao",
                    "short_name": "CW",
                    "types": [
                        "country",
                        "political"
                    ]
                }
            ],
            "formatted_address": "Curaçao",
            "geometry": {
                "bounds": {
                    "northeast": {
                        "lat": 12.4151186,
                        "lng": -68.6109924
                    },
                    "southwest": {
                        "lat": 11.9627538,
                        "lng": -69.18834029999999
                    }
                },
                "location": {
                    "lat": 12.0989103,
                    "lng": -68.8585774
                },
                "location_type": "APPROXIMATE",
                "viewport": {
                    "northeast": {
                        "lat": 12.4151186,
                        "lng": -68.6109924
                    },
                    "southwest": {
                        "lat": 11.9627538,
                        "lng": -69.18834029999999
                    }
                }
            },
            "place_id": "ChIJtWah-X1ig44RMbWx9gCSTJg",
            "types": [
                "administrative_area_level_1",
                "political"
            ]
        },
        {
            "address_components": [
                {
                    "long_name": "Curaçao",
                    "short_name": "CW",
                    "types": [
                        "country",
                        "political"
                    ]
                }
            ],
            "formatted_address": "Curaçao",
            "geometry": {
                "bounds": {
                    "northeast": {
                        "lat": 12.4941999,
                        "lng": -68.5670001
                    },
                    "southwest": {
                        "lat": 11.9224,
                        "lng": -69.29899999999999
                    }
                },
                "location": {
                    "lat": 12.16957,
                    "lng": -68.99002
                },
                "location_type": "APPROXIMATE",
                "viewport": {
                    "northeast": {
                        "lat": 12.4941999,
                        "lng": -68.5670001
                    },
                    "southwest": {
                        "lat": 11.9224,
                        "lng": -69.29899999999999
                    }
                }
            },
            "place_id": "ChIJx_3HYr6dhI4RI0fG2yiSf_w",
            "types": [
                "country",
                "political"
            ]
        }
    ],
    "status": "OK"
}

现在我必须这样做:

json_decode($response->getBody())->results[0]->address_components[4]->long_name;

我得到了administrative_area_level_1

有更好的地方吗?

php google-maps geolocation google-geocoding-api
1个回答
1
投票
地理编码API中的

Reverse geocoding也允许指定您想要获取的结果类型。为了仅获取类型为Administrative_area_level_1的结果,请在您的请求中添加result_type参数

https://maps.googleapis.com/maps/api/geocode/json?latlng=12.1224221%2C-68.8824233&result_type=administrative_area_level_1&key=YOUR_API_KEY

响应将仅包含管理区域级别1的项目,因此您可以将管理区域的名称检索为:

json_decode($response->getBody())->results[0]->formatted_address

json_decode($response->getBody())->results[0]->address_components[0]->long_name

我希望这会有所帮助!

© www.soinside.com 2019 - 2024. All rights reserved.