【问题标题】:Get full address of clicked marker in google maps在谷歌地图中获取点击标记的完整地址
【发布时间】:2015-07-08 18:37:46
【问题描述】:

你能帮我看看谷歌地图吗?

我需要获取点击标记的完整地址并在 ASP 标签控件中显示地址详细信息。事实上,我一次只显示一个标记。

例如展位号或门牌号:15 街道名称:圣约瑟夫街 郊区:西尔弗顿 城市:约翰内斯堡 纬度:24.8545 经度:-28.23565

我正在为地图使用以下 JavaScript 代码,并且工作正常。

<script type="text/javascript">
    var map;
    var marker;
    var address;

    function initialize() {

        var latitude = document.getElementById('<%= hdnLat.ClientID%>').value;
        var longitude = document.getElementById('<%= hdnLng.ClientID%>').value;

        var myLatlng = new google.maps.LatLng(longitude, latitude);
        var mapOptions = {
            zoom: 18,
            center: myLatlng
        }
        map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions);

        marker = new google.maps.Marker({
            position: myLatlng,
            map: map,
            draggable:true,
            animation: google.maps.Animation.DROP
        });
        google.maps.event.addListener(marker, 'click', toggleBounce);
    }

    function toggleBounce() {

        if (marker.getAnimation() != null) {
            marker.setAnimation(null);
        } else {
            marker.setAnimation(google.maps.Animation.BOUNCE);
        }
    }

    google.maps.event.addDomListener(window, 'load', initialize);

</script>

我正在使用以下 ASP 控件来处理地图。

<asp:HiddenField ID="hdnLng" runat="server" />
<asp:HiddenField ID="hdnLat" runat="server" />

<div id="map-canvas" style="height:425px;"></div>

<asp:Label ID="addressStandNo" runat="server"></asp:Label>
<asp:Label ID="addressStreetName" runat="server"></asp:Label>
<asp:Label ID="addressSuburb" runat="server"></asp:Label>
<asp:Label ID="addressCity" runat="server"></asp:Label>
<asp:Label ID="addressLatitude" runat="server"></asp:Label>
<asp:Label ID="addressLongitude" runat="server"></asp:Label>
<asp:Label ID="addressPostalCode" runat="server"></asp:Label>

我只需要获取地图上点击标记的详细信息(地址)并将这些值分配给上面列出的 ASP 控件。

提前谢谢你。

【问题讨论】:

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


    【解决方案1】:

    您必须在标记的点击事件上执行以下操作

    1. reverse geocoding使用latlng获取位置信息
    2. 在 asp 标签中显示返回的地理编码响应信息。参考geocoding response

    //Add listener
        google.maps.event.addListener(marker, "click", function(event) {
          toggleBounce() 
          showInfo(this.position);
        });
        
        function showInfo(latlng) {
          geocoder.geocode({
            'latLng': latlng
          }, function(results, status) {
            if (status == google.maps.GeocoderStatus.OK) {
              if (results[1]) {
                // here assign the data to asp lables
                document.getElementById('<%=addressStandNo.ClientID %>').value = results[1].formatted_address;
              } else {
                alert('No results found');
              }
            } else {
              alert('Geocoder failed due to: ' + status);
            }
          });
        }

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-09-27
      • 2013-03-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多