【问题标题】:Reverse Geocode On Google Maps api v3Google Maps api v3 上的反向地理编码
【发布时间】:2012-06-08 15:39:12
【问题描述】:

我想知道是否有人可以帮助我。

我使用下面显示的代码在 Google 地图上正确绘制从 MySQL 数据库检索到的标记。

<script type="text/javascript">
             //Sample code written by August Li
             var icon = new google.maps.MarkerImage("images/location-marker-2.png")
             new google.maps.Point(16, 32);
             var center = null;
             var map = null;
             var bounds = new google.maps.LatLngBounds();
             function addMarker(lat, lng, info) {
             var pt = new google.maps.LatLng(lat, lng);
             bounds.extend(pt);
             var marker = new google.maps.Marker({
             position: pt,
             icon: icon,
             map: map
             });
             }
             function initMap() {
             map = new google.maps.Map(document.getElementById("gmaps-canvas"), {
             center: new google.maps.LatLng(0, 0),
             zoom: 6,
             scrollwheel: true,     
             draggable: true, 
             mapTypeId: google.maps.MapTypeId.SATELLITE
             });
                <?php

                        include("admin/link.php");
                        include("admin/opendb.php");

                        $query = mysql_query("SELECT * FROM `detectinglocations` WHERE `locationid` = '$lid'");
                        while ($row = mysql_fetch_array($query)){
                            $locationname=$row['locationname'];
                            $osgb36lat=$row['osgb36lat'];
                            $osgb36lon=$row['osgb36lon'];
                            echo ("addMarker($osgb36lat, $osgb36lon,'<b>$locationname</b><br/>');\n");
                        }
                             mysql_close($connect);
                 ?>

                         center = bounds.getCenter();
                         map.fitBounds(bounds);
                        }
</script> 

我现在要做的是添加更多功能,允许用户也可以单击地图来绘制新标记,本质上使用数据库中预先存在的标记作为工作点,执行 @ 987654322@.

我已经研究了好几天了,我已经尝试实现了一大堆教程,但我似乎无法让这两个部分的功能都正常工作。

我知道要启用 on-click 事件,我需要合并以下内容:

google.maps.event.addListener(map, 'click', function(event) { marker.setPosition(event.latLng) geocode_lookup( 'latLng', event.latLng ); }); }

但我必须承认我有点不确定我还需要合并什么。

我只是想知道是否有人可以看看这个,如果有人能告诉我我哪里出错了,我将非常感激。

非常感谢和亲切的问候

【问题讨论】:

  • 没看清楚。您希望获得从数据库中添加的每个标记的地址吗?或者当点击地图时?单击数据库标记时?上述所有的?反向地理编码后,地址应该去哪里?
  • 嗨@HeitorChang,感谢您抽出宝贵时间回复我的帖子,对于没有明确说明这一点,我深表歉意。当地图加载时,地图上已经有一个标记。这是从存储在 MySQL 数据库中的 lat 和 lng 加载的。我现在需要尝试并做的是添加功能,用户也可以单击地图并执行reverse geocode,而不会影响预先填充的标记。我希望这使它更清楚一点。亲切的问候

标签: php google-maps-api-3 reverse-geocoding


【解决方案1】:

我写了一个单独的地图页面,只有点击反向地理编码功能

http://jsfiddle.net/ZDQeM/

我认为地址详细信息令人困惑。结果是一个数组,具有不同的精确度,一个可能包括县,另一个是州,另一个是街道地址。通常我只使用结果[0]。详细信息在文档中:https://developers.google.com/maps/documentation/javascript/geocoding#GeocodingResponses

如果您需要特定信息,获取它的可靠方法是遍历整个结果数组,直到找到您需要的信息(例如,包含 postal_code 的 types[])。

    google.maps.event.addListener(map, 'click', function(event) {
      userMarker = new google.maps.Marker({
        map: map,
        position: event.latLng
      });

      geocoder.geocode({'latLng': event.latLng}, function(results, status) {
        if(status == google.maps.GeocoderStatus.OK) {
          if (results[0]) {
            alert(results[0].formatted_address);
          }
          else {
            alert("No results");
          }
        }
        else {
          alert("Geocoding unsuccessful: Status " + status);
        }
      });
    });

你的代码在哪里?

<script type="text/javascript">
         //Sample code written by August Li
         var icon = new google.maps.MarkerImage("images/location-marker-2.png")
         new google.maps.Point(16, 32);
         var center = null;
         var map = null;
         var bounds = new google.maps.LatLngBounds();
         function addMarker(lat, lng, info) {
         var pt = new google.maps.LatLng(lat, lng);
         bounds.extend(pt);
         var marker = new google.maps.Marker({
         position: pt,
         icon: icon,
         map: map
         });
         }
         function initMap() {
         map = new google.maps.Map(document.getElementById("gmaps-canvas"), {
         center: new google.maps.LatLng(0, 0),
         zoom: 6,
         scrollwheel: true,     
         draggable: true, 
         mapTypeId: google.maps.MapTypeId.SATELLITE
         });
            <?php

                    include("admin/link.php");
                    include("admin/opendb.php");

                    $query = mysql_query("SELECT * FROM `detectinglocations` WHERE `locationid` = '$lid'");
                    while ($row = mysql_fetch_array($query)){
                        $locationname=$row['locationname'];
                        $osgb36lat=$row['osgb36lat'];
                        $osgb36lon=$row['osgb36lon'];
                        echo ("addMarker($osgb36lat, $osgb36lon,'<b>$locationname</b><br/>');\n");
                    }
                         mysql_close($connect);
             ?>

                     center = bounds.getCenter();
                     map.fitBounds(bounds);

                     var geocoder = new google.maps.Geocoder();

                     google.maps.event.addListener(map, 'click', function(event) {
                       var userMarker = new google.maps.Marker({
                         map: map,
                         position: event.latLng
                       });

                     geocoder.geocode({'latLng': event.latLng}, function(results, status) {
                       if(status == google.maps.GeocoderStatus.OK) {
                         if (results[0]) {
                           alert(results[0].formatted_address);
                         }
                         else {
                           alert("No results");
                         }
                       }
                       else {
                         alert("Geocoding unsuccessful: Status " + status);
                       }
                     });
                   });
                 }
</script> 

【讨论】:

  • 您好,非常感谢您帮我解决这个问题,非常感谢。请原谅我问这个问题,因为我是一个相对初学者,但你能告诉我我需要在哪里将它合并到我现有的代码中,以便功能的两个部分一起工作。非常感谢和问候。
  • 以上答案已更新。最后,对我来说,一切似乎都是个好地方。如果没有 php,我无法正确测试,看看是否适合您。
  • 嗨@Heitor Chang,这真是太棒了。非常感谢你的帮助。效果很好。所有最好的和亲切的问候。
猜你喜欢
  • 2011-09-27
  • 2013-04-05
  • 1970-01-01
  • 1970-01-01
  • 2013-12-18
  • 1970-01-01
  • 2011-07-17
  • 1970-01-01
  • 2013-10-05
相关资源
最近更新 更多