如何使用谷歌地图API和PHP将邮编转换为lat/long ?

时间:2021-07-18 21:17:21

I am using PHP to convert a list of postcodes + County into latitude and longitude. I have tried using the google map api and the simplexml_load_file() function like so:

我正在使用PHP将邮政编码+郡的列表转换为纬度和经度。我尝试使用谷歌映射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);

Now this works fine as I am able to get the following:

现在这个方法很有效,因为我可以得到以下信息:

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
                                )

                        )

                )

        )

)

Problem now is, how should I take out the first latitude / longitude and not the whole thing, I only need these two values.

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

Please note that the lat/long are dynamic as the address changes for each search.

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

2 个解决方案

#1


2  

From the print_r you can see the structure of the content. Then, it is a matter of following it.

从print_r中可以看到内容的结构。那么,这就是遵循它的问题。

This contains the values:

这包含的值:

$result->result->geometry->location

So to print them separately, you can say:

要分开打印,你可以说:

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

All together:

在一起:

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

#2


2  

Better to use their JSON API.

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

Then just access like an array!

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

e.g.

如。

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

I would use cURL instead of file get contents as it gives more control. But this works fine.

我将使用cURL而不是file get内容,因为它提供了更多的控制。但这没问题。

I would also check the $resultArray before attempting to do anything with it.

我还将检查$resultArray,然后再尝试使用它。

#1


2  

From the print_r you can see the structure of the content. Then, it is a matter of following it.

从print_r中可以看到内容的结构。那么,这就是遵循它的问题。

This contains the values:

这包含的值:

$result->result->geometry->location

So to print them separately, you can say:

要分开打印,你可以说:

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

All together:

在一起:

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

#2


2  

Better to use their JSON API.

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

Then just access like an array!

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

e.g.

如。

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

I would use cURL instead of file get contents as it gives more control. But this works fine.

我将使用cURL而不是file get内容,因为它提供了更多的控制。但这没问题。

I would also check the $resultArray before attempting to do anything with it.

我还将检查$resultArray,然后再尝试使用它。