如何按地址获取邮政编码

时间:2016-01-12 05:56:52

标签: php google-maps google-places-api

我想通过输入地址获取邮政编码,我已经尝试了谷歌自动完成地点搜索,但它为某些地方提供了错误的邮政编码。

根据给定的地址提供正确的邮政编码吗?

谢谢,

2 个答案:

答案 0 :(得分:1)

尝试以下代码,这可能会对您有所帮助:

// Decode json
$decoded_json = json_decode(file_get_contents('http://maps.googleapis.com/maps/api/geocode/json?address=1600+Amphitheatre+Parkway,+Mountain+View,+CA&sensor=false'));

foreach($decoded_json->results as $results)
{

    foreach($results->address_components as $address_components)
    {
        // Check types is set then get first element (may want to loop through this to be safe,
        // rather than getting the first element all the time)
        print_r($address_components);
        if(isset($address_components->types) && $address_components->types[0] == 'postal_code')
        {
                    // Do what you want with data here
            echo $address_components->long_name;            
        }
    }
}

答案 1 :(得分:0)

使用以下代码可以帮助您。

      <?php 

    $db = mysqli_connect("localhost","root","") or die("Could not connect");

    mysqli_select_db($db, "search_db") or die("No database");

    $textbox = "";
    $out = "";
    $title = "";
    if (isset($_GET['search'])) {

    $searchq = $_GET['search'];



    $query = mysqli_query($db, "SELECT * FROM `pincodes_db` WHERE `pincode` LIKE '%$searchq%' OR `divisionname` LIKE '%$searchq%' OR `statename` LIKE '%$searchq%' OR `regionname` LIKE '%$searchq%' OR `officename` LIKE '%$searchq%' OR `circlename` LIKE '%$searchq%'" ) or die("Could not search");

    $count = mysqli_num_rows($query);

    if($count == 0){

        $output = 'There was no records found';

        }else{


            while($row = mysqli_fetch_array($query)){

            $pincode = $row['pincode'];
            $officename = $row['officename'];
            $Deliverystatus = $row['Deliverystatus'];
                    $divisionname = $row['divisionname'];
                            $Taluk = $row['Taluk'];
                                            $RelatedHeadoffice = $row['RelatedHeadoffice'];
                                                    $RelatedSuboffice = $row['RelatedSuboffice'];
                                                                    $circlename = $row['circlename'];
                                                                    $regionname = $row['regionname'];
                                                                    $contactno = $row['Telephone'];



          $out .= '<div  style="border-bottom:1px dotted green;">

          <h2 style="font-size:18px;"><a href="search-indian-postal-code-in-map.php?postalcodeof='.$pincode.'"/><b><font color="green">Postal code of :</font></b><b><font color="green">'.$pincode.'</font></b> <b><font color="green">'.$officename.'</font></b> <b><font 

color="green">'.$regionname.'</font>,</b> <b><font color="green">'.$circlename.'</font>,</b> <b><font color="green">'.$contactno.'</font></b></a></h2>


      <table><tr><th>Description</th><th>Result</th></tr><tr>

      <td>Pincode</td>
      <td>' .$pincode.'</td></tr>

      <td>Office Name</td>
      <td>' .$officename. '</td></tr>

      <td>Delivery system</td>
      <td>' . $Deliverystatus. '</td></tr>

      <td>Division Name</td>
      <td>' . $divisionname. '</td></tr>

      <td>Taluk</td>
      <td>' . $Taluk. '</td></tr>

      <td>Related Head Office</td>
      <td>' . $RelatedHeadoffice. '</td></tr>

      <td>Related Sub Office</td>
      <td>' . $RelatedSuboffice. '</td></tr>

      <td>Circle Name</td>
      <td>' . $circlename. '</td></tr>

      <td>Region</td>
      <td>' . $regionname. '</td></tr>

      <table/></div>';


            }


        }
}

?>

我在网站上使用了相同的代码请参考

http://www.myworkbook.in/indian-postal-code-search/indian-states.php