我需要通过地址从谷歌地图api获取纬度和经度值。我使用了网址" http://maps.googleapis.com/maps/api/geocode/json?address="。$ doctorAddress。"& sensor = true"喜欢从谷歌地图API获取价值。我试图使用for循环获取值列表。但我的问题是它只返回null值。我无法从API获得价值。无论如何得到它。任何人都可以帮我这样做。
答案 0 :(得分:1)
下面是使用php中的curl从google map api获取lat和long的代码
$address = "India+Panchkula";
$url = "http://maps.google.com/maps/api/geocode/json?address=$address&sensor=false®ion=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®ion=$region");
$json = json_decode($json);
$lat = $json->{'results'}[0]->{'geometry'}->{'location'}->{'lat'};
$long = $json->{'results'}[0]->{'geometry'}->{'location'}->{'lng'};