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
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
  • ¥15 关于smbclient 库的使用
  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?