【问题标题】:match zip code or address from php page to google map将邮政编码或地址从 php 页面匹配到谷歌地图
【发布时间】:2017-11-17 00:41:27
【问题描述】:

我有一个 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

如果管理员点击Mr.的点击按钮 abc 那么google map应该是用MR的addresszip打开的。 abc 和谷歌位置应该在 MR 的谷歌地图上打开。 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.** 

代码是否正确?

【问题讨论】:

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


【解决方案1】:

您可以简单地在谷歌地图网址中添加地址和 zip 并在新标签中打开它。 URL 可能如下所示

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

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

【讨论】:

  • 感谢您的回复
  • maps.google.com/maps?q=<?php echo $address1; ?>" > 但是在哪里添加邮政编码?
  • 正在显示 cloudsoft 技术的结果,地块编号 9,区域 1,mp nagar bhopal 462011。未找到 cloudsoft 技术的结果,地块编号 9,区域 1,mp nagar bhopal462011。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-01-25
  • 1970-01-01
  • 2016-01-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多