【问题标题】:getting Latitude And Longitude From Pin Code Using PHP使用 PHP 从 Pin 码中获取纬度和经度
【发布时间】:2021-10-08 14:05:21
【问题描述】:

在我的网站中,我想获取给定 PIN 码或邮政编码的纬度和经度。 如果我将680721 作为 PIN 码或邮政编码,那么我想使用 php 代码获取其对应的纬度和经度。 我怎样才能做到这一点? 如果这在 PHP 中是可能的? 我有 php 代码来获取给定地名的纬度和经度。

$Url='http://maps.googleapis.com/maps/api/geocode/json?address='.$exp_pjctloc[$i].'&sensor=false';
if (!function_exists('curl_init')){
    die('Sorry cURL is not installed!');
}

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $Url);
curl_setopt($ch, CURLOPT_REFERER, "http://www.example.org/yay.htm");
curl_setopt($ch, CURLOPT_USERAGENT, "MozillaXYZ/1.0");
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
$output = curl_exec($ch);
curl_close($ch);
$search_data = json_decode($output);
$lat =  $search_data->results[0]->geometry->location->lat;
$lng =  $search_data->results[0]->geometry->location->lng;

但是我怎样才能通过使用 PIN 码或邮政编码获得呢?

【问题讨论】:

  • 我相信一些国家(khm、印度、khm)使用术语“pin 码”来指代区号或邮政编码。在我发现之前我很困惑。
  • 尝试给它更多相关数据,如果你只给它一个数字,那么地理编码api可能很难确定你想要什么,尝试添加国家、省等......如果你认识他们。

标签: php google-api latitude-longitude


【解决方案1】:

以下代码对我有用

 <?php
    $zipcode="92604";
    $url = "http://maps.googleapis.com/maps/api/geocode/json?address=".$zipcode."&sensor=false";
    $details=file_get_contents($url);
    $result = json_decode($details,true);

    $lat=$result['results'][0]['geometry']['location']['lat'];

    $lng=$result['results'][0]['geometry']['location']['lng'];

    echo "Latitude :" .$lat;
    echo '<br>';
    echo "Longitude :" .$lng;
    ?>

【讨论】:

  • @AgnesPalit,否
【解决方案2】:

这对我有用:

$zip = 94043; $site = file_get_contents('http://geocoder.ca/?postal='.$zip, false, NULL, 1000, 1000); $goods = strstr($site, 'GPoint('); // 截断第一部分直到 $end = strpos($goods, ')'); // 坐标的结束括号 $cords = substr($goods, 7, $end - 7); // 返回字符串,只有 $array = explode(', ',$cords); // 将字符串转换为数组 回声“
”;
print_r($array); // 输出要验证的数组

【讨论】:

    【解决方案3】:
    With the curl you can get lat and long from below code:
    
    $zipcode=90012;
    $url = "https://maps.googleapis.com/maps/api/geocode/json?address=".urlencode($zipcode)."&sensor=false&key=XXXXXXXX";
    $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_TIMEOUT, 10);
        $output = curl_exec($ch);
        curl_close($ch);
        $search_data = json_decode($output);
    
        $lat =  $search_data->results[0]->geometry->location->lat;
        $lng =  $search_data->results[0]->geometry->location->lng;
        
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-11-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-06-06
      相关资源
      最近更新 更多