0

我正在使用 google geocode api,它创建了一个我不知道如何使用的数组。我需要从这个数组中提取特定元素。特别是数组中的最后一个纬度和经度。

{
    "results": [
        {
            "address_components": [
                {
                    "long_name": "1600",
                    "short_name": "1600",
                    "types": [
                        "street_number"
                    ]
                },
                {
                    "long_name": "President's Park",
                    "short_name": "President's Park",
                    "types": [
                        "establishment"
                    ]
                },
                {
                    "long_name": "Pennsylvania Avenue Northwest",
                    "short_name": "Pennsylvania Ave NW",
                    "types": [
                        "route"
                    ]
                },
                {
                    "long_name": "Washington",
                    "short_name": "Washington",
                    "types": [
                        "locality",
                        "political"
                    ]
                },
                {
                    "long_name": "District of Columbia",
                    "short_name": "DC",
                    "types": [
                        "administrative_area_level_1",
                        "political"
                    ]
                },
                {
                    "long_name": "United States",
                    "short_name": "US",
                    "types": [
                        "country",
                        "political"
                    ]
                },
                {
                    "long_name": "20500",
                    "short_name": "20500",
                    "types": [
                        "postal_code"
                    ]
                }
            ],
            "formatted_address": "1600 Pennsylvania Avenue Northwest, President's Park, Washington, DC 20500, USA",
            "geometry": {
                "location": {
                    "lat": 38.8978378,
                    "lng": -77.0365123
                },
                "location_type": "ROOFTOP",
                "viewport": {
                    "northeast": {
                        "lat": 38.89918678029149,
                        "lng": -77.03516331970849
                    },
                    "southwest": {
                        "lat": 38.89648881970849,
                        "lng": -77.03786128029151
                    }
                }
            },
            "types": [
                "street_address"
            ]
        }
    ],
    "status": "OK"
}


Array
(
    [latitude] => -77.0365123
    [longitude] => 38.8978378
    [location_type] => ROOFTOP
)
4

1 回答 1

1

编辑

由于您有 JSON 格式的结果,并且结构现在更清晰,格式更清晰,因此您可以通过以下方式获得该结果中的 lat 或 lng 值

$object=json_decode($json);
$lat=$object->results[0]->geometry->location->lat; // same style for lng
echo $lat;

在这里看小提琴

于 2013-10-10T05:30:08.170 回答