【问题标题】:PHP - Get Latitude & Longitude from an address [closed]PHP - 从地址获取纬度和经度[关闭]
【发布时间】:2014-06-06 10:04:41
【问题描述】:

我使用了地理编码的自动完成功能,但是当我从下拉列表中选择时,它会显示地址的经纬度

我需要检查纬度和经度何时为空,然后从发布的地址中我必须获取纬度和经度

【问题讨论】:

标签: php google-maps google-geocoder


【解决方案1】:

假设你有隐藏的 lat 和 long 值是 mapLat & mapLong 和输入字段名称是位置 那么:

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

【讨论】:

  • 嗨,我想问一下我是否在没有获取 API 密钥的情况下在 php 中使用此代码,可以吗?
  • 不行,你需要使用api来获取结果
  • 谢谢!搜索了一个多小时的工作代码。这对我来说非常有用。
  • $region 未定义。
  • 工作演示谢谢先生!
【解决方案2】:

使用 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);
}    
?>

【讨论】:

【解决方案3】:

我尝试了上述解决方案,但没有一个有效,然后我尝试自己修复,所以下面是正确的代码:

    $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年更容易接受的答案
  • 不需要这些东西,这似乎是@FahadKhan 的场景所特有的。
【解决方案4】:
// 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;
}

【讨论】:

【解决方案5】:

试试这个来获取地址:

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

?>

【讨论】:

  • 恰恰相反。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2010-12-02
  • 1970-01-01
  • 1970-01-01
  • 2017-10-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多