我想通過使用谷歌地圖API 3.谷歌地圖API:打開URL點擊標記
點擊一個標記,遺憾的是沒有爲谷歌地圖API的例子很多,打開一個新的窗口,我發現,此代碼:
google.maps.event.addListener(marker, 'click', function() {
window.location.href = marker.url;
});
如何使用它,當我用循環創建標記?我用很多方式嘗試過,沒有負擔。
這是我的代碼 - 我把它簡單和短:
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
<style type="text/css">
html { height: 100% }
body { height: 100%; margin: 0; padding: 0 }
#map_canvas { height: 100% }
</style>
<script type="text/javascript" src="http://maps.googleapis.com/maps/api/js?sensor=false"></script>
<script type="text/javascript">
var points = [
['name1', 59.9362384705039, 30.19232525792222, 12],
['name2', 59.941412822085645, 30.263564729357767, 11],
['name3', 59.939177197629455, 30.273554411974955, 10]
];
function setMarkers(map, locations) {
var shape = {
coord: [1, 1, 1, 20, 18, 20, 18 , 1],
type: 'poly'
};
for (var i = 0; i < locations.length; i++) {
var flag = new google.maps.MarkerImage('markers/' + (i + 1) + '.png',
new google.maps.Size(17, 19),
new google.maps.Point(0,0),
new google.maps.Point(0, 19));
var place = locations[i];
var myLatLng = new google.maps.LatLng(place[1], place[2]);
var marker = new google.maps.Marker({
position: myLatLng,
map: map,
icon: flag,
shape: shape,
title: place[0],
zIndex: place[3]
});
}
}
function initialize() {
var myOptions = {
center: new google.maps.LatLng(59.91823239768787, 30.243222856188822),
zoom: 12,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
var map = new google.maps.Map(document.getElementById("map_canvas"),myOptions);
setMarkers(map, points);
}
</script>
</head>
<body onload="initialize()">
<div id="map_canvas" style="width:50%; height:50%"></div>
</body>
</html>
如何通過點擊標記與上面的代碼打開URL?
感謝您的回覆。通過這種方式,每個標記都會打開最後一個網址。我試過這個,並沒有找到解決的辦法。我檢查了你的例子(上面的鏈接):據我可以判斷:也有同樣的問題 - 當你點擊標記有每個標記「stackoverflow鏈接」。 – idobr 2012-01-07 21:16:44
對不起,你是對的,我已經更新了我的答案。 – scessor 2012-01-07 21:44:31
這真的太棒了!有用。我認爲問題出在傾聽者的地方。非常感謝你。 – idobr 2012-01-07 22:22:51