繁体   English   中英

获取谷歌地图中单击标记的完整地址

[英]Get full address of clicked marker in google maps

您能帮我用谷歌地图吗?

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

例如,展位号或门牌号:15街道名称:St Joseph Street郊区:Silverton城市:约翰内斯堡纬度: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控件即可。

先感谢您。

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

  1. 使用latlng进行反向地理编码以获取位置信息
  2. 在asp标签中显示返回的geocodig响应信息。 参考地理编码响应

 //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); } }); } 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM