匹配邮政编码或地址从php页面到谷歌地图

时间:2017-06-14 18:17:02

标签: javascript php ajax google-maps google-maps-api-3

我有一个PHP页面,其中某个人的地址来自数据库表。

喜欢:

Person name  address  zip     MAP

MR. xyz      city1    1234    click
MR. abc      city2    1234    click
MR. dfe      city3    1244    click
MR. rrr      city4    1284    click

如果管理员点击了先生的点击按钮。 abc 然后谷歌地图应该以 MR 地址压缩打开。应该在 MR的谷歌地图上打开abc 和谷歌位置。 ABC

我不知道该怎么做?请给出一些建议。

现在我找到了一个代码:

 <?php
include("connection.php");

 function geocode($address){

   // url encode the address
  $address = urlencode($address);

  // google map geocode api url
  $url = "http://maps.google.com/maps/api/geocode/json?address={$address}";

  // get the json response
  $resp_json = file_get_contents($url);

  // decode the json
  $resp = json_decode($resp_json, true);

    // response status will be 'OK', if able to geocode given address 
   if($resp['status']=='OK'){

    // get the important data
    $lati = $resp['results'][0]['geometry']['location']['lat'];
    $longi = $resp['results'][0]['geometry']['location']['lng'];
    $formatted_address = $resp['results'][0]['formatted_address'];

    // verify if data is complete
    if($lati && $longi && $formatted_address){

        // put the data in the array
        $data_arr = array();            

        array_push(
            $data_arr, 
                $lati, 
                $longi, 
                $formatted_address
            );

        return $data_arr;

    }else{
        return false;
    }

    }else{
    return false;
  }
 }


  if($_REQUEST){

   // get latitude, longitude and formatted address
   $data_arr = geocode($_REQUEST['address']);

    // if able to geocode the address
   if($data_arr){

    $latitude = $data_arr[0];
    $longitude = $data_arr[1];
    $formatted_address = $data_arr[2];

   ?>

    <!-- google map will be shown here -->
   <div id="gmap_canvas">Loading map...</div>
   <div id='map-label'>Map shows approximate location.</div>

   <!-- JavaScript to show google map -->
    <script type="text/javascript" src="http://maps.google.com/maps/api/js"></script>    
    <script type="text/javascript">
    function init_map() {
        var myOptions = {
            zoom: 14,
            center: new google.maps.LatLng(<?php echo $latitude; ?>, <?php echo $longitude; ?>),
            mapTypeId: google.maps.MapTypeId.ROADMAP
        };
        map = new google.maps.Map(document.getElementById("gmap_canvas"), myOptions);
        marker = new google.maps.Marker({
            map: map,
            position: new google.maps.LatLng(<?php echo $latitude; ?>, <?php echo $longitude; ?>)
        });
        infowindow = new google.maps.InfoWindow({
            content: "<?php echo $formatted_address; ?>"
        });
        google.maps.event.addListener(marker, "click", function () {
            infowindow.open(map, marker);
        });
        infowindow.open(map, marker);
    }
    google.maps.event.addDomListener(window, 'load', init_map);
    </script>

    <?php

    // if unable to geocode the address
    }else{
    echo "No map found.";
   }
   }

   ?>

     But it gave an ERROR:  **Map shows approximate location.** 

是不是正确的代码?

1 个答案:

答案 0 :(得分:1)

您只需在Google地图网址中添加地址和zip,然后在新标签中将其打开即可。网址可能如下所示

http://maps.google.com/maps?q=<address>+<zip>

http://maps.google.com/maps?q=Delhi+110021