如何通过php从动态地址中获取纬度和经度

如何通过php从动态地址中获取纬度和经度,php,google-maps-api-3,geolocation,Php,Google Maps Api 3,Geolocation,如何通过php从地址获取纬度和经度。我需要从地址获取纬度和经度 $address = "Chennai, India"; $url = "http://maps.google.com/maps/api/geocode/json?address=".urlencode($address);    $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_RETUR

如何通过php从地址获取纬度和经度。我需要从地址获取纬度和经度

$address = "Chennai, India";
$url = "http://maps.google.com/maps/api/geocode/json?address=".urlencode($address);
                  
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$responseJson = curl_exec($ch);
curl_close($ch);
                  
$response = json_decode($responseJson);
                  
if ($response->status == 'OK') {
    $latitude = $response->results[0]->geometry->location->lat;
    $longitude = $response->results[0]->geometry->location->lng;
    echo 'Latitude: ' . $latitude; 
    echo '<br />'; 
    echo 'Longitude: ' . $longitude;
} else {
    echo $response->status;
}
$address=“印度钦奈”;
$url=”http://maps.google.com/maps/api/geocode/json?address=“.urlencode($地址);
  
$ch=curl_init();
curl_setopt($ch,CURLOPT_URL,$URL);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
$responseJson=curl\u exec($ch);
卷曲关闭($ch);
  
$response=json_decode($responseJson);
  
如果($response->status=='OK'){
$latitude=$response->results[0]->geometry->location->lat;
$longitude=$response->results[0]->geometry->location->lng;
回声“纬度:”.$LATIONE;
回声“
”; 回显“经度:”.$经度; }否则{ echo$response->status; }
试试这个

 <?php
    $address = "Ahmedabad, India";
    $formattedAddr = str_replace(' ','+',$address);
            //Send request and receive json data by address
            $geocodeFromAddr = file_get_contents('https://maps.googleapis.com/maps/api/geocode/json?address='.$formattedAddr.'&sensor=false&key=key');
            $output = json_decode($geocodeFromAddr);
            print_r($output);
            //Get latitude and longitute from json data
            $data['latitude']  = $output->results[0]->geometry->location->lat;
            $data['longitude'] = $output->results[0]->geometry->location->lng;
            //Return latitude and longitude of the given address
            if(!empty($data)){
                return $data;
            }else{
                return false;
            }

    ?>


你必须使用API密钥来验证谷歌地图平台API的每个请求
而不是购买,我建议你先使用试用版,谷歌给你1个月的免费试用期。你能解释一下你的问题吗?当您使用它时,是否有任何东西不起作用?@devpro不,它们不起作用。您确实需要一个密钥,并且必须在您的帐户中启用计费功能。我的意思是他们不会像你提到的那样“给你一个月的免费试用期”。您有每月200美元的免费信贷。那是另一回事。一切都解释清楚了。