[英]Getting latitude and longitude from address
我不知道為什么無法從該地址獲取坐標。 任何幫助表示贊賞。
<?php
function getCoordinates($address){
$mapApiKey= 'AIzaSyBaEjohZpjSWkzIXQP0u01FZpZSe5Uxuhs';
$address = urlencode($address);
$url = "https://maps.googleapis.com/maps/api/geocode/json?sensor=false&address=" .$address ."&key=".$mapApiKey;
$response = file_get_contents($url);
$json = json_decode($response,true);
$lat = $json['results'][0]['geometry']['location']['lat'];
$lng = $json['results'][0]['geometry']['location']['lng'];
return array($lat, $lng);
}
$coords = getCoordinates("Patras+Greece");
print_r($coords);
?>
代替file_get_contents使用curl
$url = "http://maps.google.com/maps/api/geocode/json?address=".$address."&sensor=false";
$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);
if(isset($response_a))
{
$lat = $response_a->results[0]->geometry->location->lat;
$long = $response_a->results[0]->geometry->location->lng;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.