地理编码从谷歌地图API获取纬度和经度使用curl在php中


Geocode get latitude and longitude from google map api using curl in php

我需要通过地址从谷歌地图 api 获取纬度和经度的值。我使用了网址"http://maps.googleapis.com/maps/api/geocode/json?address=".$doctorAddress。&sensor=true"喜欢从谷歌地图API获取价值。我正在尝试使用 for 循环获取值列表。但我的问题是它只返回空值。我无法从 API 获取值。无论如何都可以得到它。谁能帮我做到这一点。

下面是在 php 中使用 curl 从谷歌地图 API 获取纬度和经度的代码

$address = "India+Panchkula";
$url = "http://maps.google.com/maps/api/geocode/json?address=$address&sensor=false&region=India";
$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_a = json_decode($response);
echo $lat = $response_a->results[0]->geometry->location->lat;
echo "<br />";
echo $long = $response_a->results[0]->geometry->location->lng;

您也可以使用以下方法实现相同的效果

$address = str_replace(" ", "+", $address);
$json = file_get_contents("http://maps.google.com/maps/api/geocode/json?address=$address&sensor=false&region=$region");
$json = json_decode($json);
$lat = $json->{'results'}[0]->{'geometry'}->{'location'}->{'lat'};
$long = $json->{'results'}[0]->{'geometry'}->{'location'}->{'lng'};