几年前,从一个地址获取经纬度曾经是一项相当复杂的任务,幸好现在使用 Google Maps API 变得非常简单!

其工作方式是调用 API: 

http://maps.google.com/maps/api/geocode

上面的地址会调用谷歌,它需要多一点才能完成它需要格式和地址,以及区域:

http://maps.google.com/maps/api/geocode/json?address=UK+Hull&sensor=false&region=England

需要指出的重要部分是 address=UK+HUll 该地址也需要一个地址,因为这是一个 Web URL,它不能有空格,而是使用 + 代替空格。

在浏览器中访问 URL 将得到以下 JSON 对象

{
   "results" : [
      {
         "address_components" : [
            {
               "long_name" : "Hull",
               "short_name" : "Hull",
               "types" : [ "locality", "political" ]
            },
            {
               "long_name" : "England",
               "short_name" : "England",
               "types" : [ "administrative_area_level_1", "political" ]
            },
            {
               "long_name" : "United Kingdom",
               "short_name" : "GB",
               "types" : [ "country", "political" ]
            }
         ],
         "formatted_address" : "Hull, UK",
         "geometry" : {
            "bounds" : {
               "northeast" : {
                  "lat" : 53.81325020,
                  "lng" : -0.24139640
               },
               "southwest" : {
                  "lat" : 53.71949550,
                  "lng" : -0.42257510
               }
            },
            "location" : {
               "lat" : 53.74567090,
               "lng" : -0.33674130
            },
            "location_type" : "APPROXIMATE",
            "viewport" : {
               "northeast" : {
                  "lat" : 53.81325020,
                  "lng" : -0.24139640
               },
               "southwest" : {
                  "lat" : 53.71949550,
                  "lng" : -0.42257510
               }
            }
         },
         "types" : [ "locality", "political" ]
      }
   ],
   "status" : "OK"
}

为了使这个有用,您不想将 URL 放在地址栏中,而是使用 PHP 来使用该地址并返回数据,有两种方法可以使用 curl 或 file_get_contents。

Curl

首先是 CURL,这是它提供更多控制的首选方式,它也比 file_get_contents 更快。

以下 $url 变量将用于两个示例

$url = "http://maps.google.com/maps/api/geocode/json?address=UK+Hull&sensor=false&region=England";

接下来启动 curl 并设置 curl 选项,第一个选项是要调用的 url,第二个选项告诉请求带回数据,其余指定端口和主机。
curl_exec 实际上运行 curl 请求。

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_PROXYPORT, 3128);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
$response = curl_exec($ch);
curl_close($ch);

来自 curl 请求的数据从那里返回并存储在 $response 中,解码 json 对象。

$response = json_decode($response);

要指定所需对象中的项目,请相应地在结果集中进行,在这种情况下,带回经度和纬度并将它们存储到变量中。

$lat = $response->results[0]->geometry->location->lat;
$long = $response->results[0]->geometry->location->lng;

完整Curl代码

$url = "http://maps.google.com/maps/api/geocode/json?address=UK+Hull&sensor=false&region=England";

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_PROXYPORT, 3128);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
$response = curl_exec($ch);
curl_close($ch);

$response = json_decode($response);

$lat = $response->results[0]->geometry->location->lat;
$long = $response->results[0]->geometry->location->lng;

File_get_contents

这次不是发出 curl 请求,而是将 url 传递给 file_get_contents 函数,这将返回 json 对象,将其传递给 json_decode 函数,然后指定所需对象的部分。 

$url = file_get_contents("http://maps.google.com/maps/api/geocode/json?address=UK+Hull&sensor=false&region=UK");
$response = json_decode($url);

$lat = $response->results[0]->geometry->location->lat;
$long = $response->results[0]->geometry->location->lng;

 

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。