如何使用Google Maps API和PHP将邮政编码转换为纬度/经度?

时间:2015-01-05 11:03:59

标签: php xml google-maps google-maps-api-3 latitude-longitude

我使用PHP将邮政编码+县列表转换为纬度和经度。我尝试过使用google map 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
                                )

                        )

                )

        )

)

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

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

2 个答案:

答案 0 :(得分:2)

最好使用他们的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);

然后只需像数组一样访问!

e.g。

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

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

在尝试对它做任何事情之前,我也会检查$ resultArray。

答案 1 :(得分:2)

print_r您可以看到内容的结构。然后,这是一个跟随它的问题。

这包含值:

$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/>";