dongwang788787 2017-06-14 18:17
浏览 53
已采纳

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

I have a PHP page in which address of some person fecth from database table.

Like:

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

If admin clicks on click button of Mr. abc then google map should be open with address or zip of MR. abc and google location should be open on google map of MR. abc

I don't have any idea how to do this? Please give some suggestion.

Now I found a code:

 <?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.** 

Is it right code or not?

  • 写回答

1条回答 默认 最新

  • dongyi1921 2017-06-14 18:57
    关注

    You can simply add the address and zip in google map url and open it in new tab. The URL can look like this

    http://maps.google.com/maps?q=<address>+<zip>
    
    http://maps.google.com/maps?q=Delhi+110021
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 swiftui @query 报错
  • ¥50 怎么解决刷卡或扫码后,点击软件输入框,win10屏幕键盘不会自动弹出的问题
  • ¥15 如何使用arcgispro的训练深度模型,发现water和nowater精度为0?(相关搜索:深度学习)
  • ¥20 matlab作业不太懂呀有问题能给个代码吗
  • ¥15 自制电路图为何无法驱动ESP01S?
  • ¥15 前端加access数据库
  • ¥15 ARCGIS 多值提取到点 ERROR 999999
  • ¥15 mysql异常断电, [MY-011971] [InnoDB]
  • ¥15 uni.onBluetoothDeviceFound熄屏不运行
  • ¥15 求PHDA糖尿病并发症数据集,有偿