繁体   English   中英

从地址获取经度和纬度

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM