如何使用 Google 地图 API 和 PHP 将邮政编码转换为 lat/long?

How to converting postcode to lat/long using Google Maps API with PHP?

我正在使用 PHP 将邮政编码 + 县的列表转换为纬度和经度。我试过像这样使用 google 映射 api 和 simplexml_load_file() 函数:

$url ="http://maps.googleapis.com/maps/api/geocode/xml?address=WS9+8NS,+West+Midlands&sensor=false";
$result = simplexml_load_file($url);
print"<pre>";
print_r($result);

现在这工作正常,因为我能够得到以下内容:

SimpleXMLElement Object
(
    [status] => OK
    [result] => SimpleXMLElement Object
        (
            [type] => postal_code
            [formatted_address] => Walsall, Walsall, West Midlands WS9 8NS, UK
            [address_component] => Array
                (
                    [0] => SimpleXMLElement Object
                        (
                            [long_name] => WS9 8NS
                            [short_name] => WS9 8NS
                            [type] => postal_code
                        )

                    [1] => SimpleXMLElement Object
                        (
                            [long_name] => Walsall
                            [short_name] => Walsall
                            [type] => Array
                                (
                                    [0] => locality
                                    [1] => political
                                )

                        )

                    [2] => SimpleXMLElement Object
                        (
                            [long_name] => Walsall
                            [short_name] => Walsall
                            [type] => postal_town
                        )

                    [3] => SimpleXMLElement Object
                        (
                            [long_name] => West Midlands
                            [short_name] => West Mids
                            [type] => Array
                                (
                                    [0] => administrative_area_level_2
                                    [1] => political
                                )

                        )

                    [4] => SimpleXMLElement Object
                        (
                            [long_name] => United Kingdom
                            [short_name] => GB
                            [type] => Array
                                (
                                    [0] => country
                                    [1] => political
                                )

                        )

                )

            [geometry] => SimpleXMLElement Object
                (
                    [location] => SimpleXMLElement Object
                        (
                            [lat] => 52.6030230
                            [lng] => -1.9175368
                        )

                    [location_type] => APPROXIMATE
                    [viewport] => SimpleXMLElement Object
                        (
                            [southwest] => SimpleXMLElement Object
                                (
                                    [lat] => 52.6013713
                                    [lng] => -1.9188871
                                )

                            [northeast] => SimpleXMLElement Object
                                (
                                    [lat] => 52.6040692
                                    [lng] => -1.9161892
                                )

                        )

                    [bounds] => SimpleXMLElement Object
                        (
                            [southwest] => SimpleXMLElement Object
                                (
                                    [lat] => 52.6020654
                                    [lng] => -1.9182516
                                )

                            [northeast] => SimpleXMLElement Object
                                (
                                    [lat] => 52.6033751
                                    [lng] => -1.9168247
                                )

                        )

                )

        )

)

现在的问题是,我应该如何取出第一个纬度/经度而不是整个东西,我只需要这两个值。

请注意,lat/long 是动态的,因为每次搜索的地址都会发生变化。

最好使用他们的 JSON API。

$url ="http://maps.googleapis.com/maps/api/geocode/json?address=WS9+8NS,+West+Midlands&sensor=false";
$result = file_get_contents($url);
print"<pre>";
$resultArray = json_decode($result, true);
print_r($result);

那就像数组一样访问吧!

例如

$resultArray['results']['geometry']['location']['lat']
$resultArray['results']['geometry']['location']['lng']

我会使用 cURL 而不是文件获取内容,因为它提供了更多控制。但这很好用。

在尝试对 $resultArray 执行任何操作之前,我还会检查它。

print_r可以看出内容的结构。那么,就是跟风了。[=​​14=]

这包含值:

$result->result->geometry->location

所以要分别打印它们,你可以说:

print "lat: " . $result->result->geometry->location->lat;
print "lng: " . $result->result->geometry->location->lng;

总计:

$url ="http://maps.googleapis.com/maps/api/geocode/xml?address=WS9+8NS,+West+Midlands&sensor=false";
$result = simplexml_load_file($url);
print "lat: " . $result->result->geometry->location->lat . " <br/>";
print "lng: " . $result->result->geometry->location->lng . " <br/>";