使用PHP从Pin码获取经度和纬度


getting Latitude And Longitude From Pin Code Using PHP

在我的网站我想得到给定的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码或者邮政编码来取呢?

下面的代码为我工作

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

这对我有用:

<>之前$zip = 94043;$site = file_get_contents('http://geocoder.ca/?postal=')。$zip, false, NULL, 1000,1000);$goods = strstr($site, 'GPoint(');//切断第一部分直到$end = strpos($goods, ')');//坐标的结束括号$cord = substr($goods, 7, $end - 7);//返回只包含$array = explosion (', ',$cord);//将字符串转换为数组回声"& lt;前>";print_r(数组)美元;//输出要验证的数组
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;