如何通过PHP从动态地址获得纬度和经度



我如何从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;
}

尝试这个

 <?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;
            }
    ?>

最新更新