![](/img/trans.png)
[英]Get the Clicked marker in Google maps and send it to a function
[英]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控件即可。
先感謝您。
您必須對標記的點擊事件執行以下操作
//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.