PHP - 从地址获取纬度和经度

16

我已经使用了地理编码的自动完成功能,但是当我从下拉列表中进行选择时,它给我提供了地址的纬度和经度。

如果纬度和经度为空,我需要检查提交的地址并获取其纬度和经度。


1
你有什么烦恼吗?你需要哪个具体部分的帮助?(另外,参见[提问]以获取更好的提问技巧) - Amal Murali
1
可能是重复的问题:如何获取任何地址的经纬度? - Trenton McKinney
5个回答

40

假设你有一个名为mapLat和mapLong的隐藏值,以及一个名为location的输入字段,则:

<html>
<form>
<input type="hidden" name="mapLat">
<input type="hidden" name="mapLong">
<input type="text" name="location">
<input type="submit" name="submit" value="submit">
</form>
</html>



extract($_POST);
if($mapLat =='' && $mapLong ==''){
        // Get lat long from google
        $latlong    =   get_lat_long($location); // create a function with the name "get_lat_long" given as below
        $map        =   explode(',' ,$latlong);
        $mapLat         =   $map[0];
        $mapLong    =   $map[1];    
}


// function to get  the address
function get_lat_long($address){

    $address = str_replace(" ", "+", $address);

    $json = file_get_contents("http://maps.google.com/maps/api/geocode/json?address=$address&sensor=false&region=$region");
    $json = json_decode($json);

    $lat = $json->{'results'}[0]->{'geometry'}->{'location'}->{'lat'};
    $long = $json->{'results'}[0]->{'geometry'}->{'location'}->{'lng'};
    return $lat.','.$long;
}

嗨,我想问一下,如果我在PHP中使用这段代码而不获取API密钥,可以吗? - Will
不行,你需要使用API来获取结果。 - sandipshirsale
谢谢!我找了一个多小时才找到可用的代码,这个对我来说完美地运行了。 - Harsha
4
$region 未定义。 - secondman
演示版已经可以运行,谢谢您先生! - bharat savani
https://maps.googleapis.com/maps/api/geocode/json?address=1600+Amphitheatre+Parkway,+Mountain+View,+CA&key=xxxxxxxxxxxxxxxxxxxxxxxxx现在您需要一个密钥才能使用,获取密钥请访问: https://developers.google.com/maps/documentation/geocoding/get-api-key - Brijmohan Karadia

12

使用 CURL

<?php
$address = "Kathmandu, Nepal";
$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;
    var_dump($response);
}    
?>

1
https://maps.googleapis.com/maps/api/geocode/json?address=1600+Amphitheatre+Parkway,+Mountain+View,+CA&key=xxxxxxxxxxxxxxxxxxxxxxxxx现在您需要一个密钥才能使用,获取密钥请访问: https://developers.google.com/maps/documentation/geocoding/get-api-key - Brijmohan Karadia
file_get_contents() 方法对我来说似乎已经完全不起作用了。我用这个 CURL 解决方案替换它,现在它完美地工作了。 - Jeremy Caris

5

我尝试了上面的解决方案,但都没有起作用,然后我试图自己修复问题,下面是正确的代码:

    $going=$this->input->post('going');

    $address =$going; // Google HQ
    $prepAddr = str_replace(' ','+',$address);
    $apiKey = 'Add your API Key'; // Google maps now requires an API key.

    $geocode=file_get_contents('https://maps.googleapis.com/maps/api/geocode/json? 
   address='.urlencode($address).'&sensor=false&key='.$apiKey);

    //print_r($geocode);

    $output= json_decode($geocode);
    $latitude = $output->results[0]->geometry->location->lat;
    $longitude = $output->results[0]->geometry->location->lng;

这是2020年更为可接受的答案。 - zyrup
不需要那些看起来是针对@FahadKhan情况的特定内容。 - Mike Kormendy

2
<?php 
// We define our address
$address = 'Indore, MP 452001';
echo"<PRE>";
print_r(get_lat_long($address));

// function to get  the address
function get_lat_long($address) {
   $array = array();
   $geo = file_get_contents('http://maps.googleapis.com/maps/api/geocode/json?address='.urlencode($address).'&sensor=false');

   // We convert the JSON to an array
   $geo = json_decode($geo, true);

   // If everything is cool
   if ($geo['status'] = 'OK') {
      $latitude = $geo['results'][0]['geometry']['location']['lat'];
      $longitude = $geo['results'][0]['geometry']['location']['lng'];
      $array = array('lat'=> $latitude ,'lng'=>$longitude);
   }

   return $array;
}

https://maps.googleapis.com/maps/api/geocode/json?address=1600+Amphitheatre+Parkway,+Mountain+View,+CA&key=xxxxxxxxxxxxxxxxxxxxxxxxx现在您也需要一个密钥,要找到密钥: https://developers.google.com/maps/documentation/geocoding/get-api-key - Brijmohan Karadia

2

尝试使用以下代码获取地址:

<?
function getaddress($lat, $lng)
{
  $url = 'http://maps.googleapis.com/maps/api/geocode/json?latlng=' . trim($lat) . ',' . trim($lng) . '&sensor=false';
  $json = @file_get_contents($url);
  $data = json_decode($json);
  $status = $data->status;
  if ($status == "OK")
    return $data->results[0]->formatted_address;
  else
    return false;
}


$lat = 26.754347; //latitude
$lng = 81.001640; //longitude
$address = getaddress($lat, $lng);
if ($address) {
  echo $address;
} else {
  echo "Not found";
}

?>


2
完全相反。 - Gogol

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接