我有一個複雜的流程,我必須爲地圖上的每條多段線添加鼠標懸停事件。附加事件的代碼很簡單:爲什麼mouseover事件不會調度爲谷歌地圖中的折線?
google.maps.event.addListener(polyline, "mouseover", function() {
console.log('event fired');
});
但事件附加到幾條折線而不是其他。可能是什麼原因?
編輯
以下是一些更多的代碼即上面的代碼之前和用於定義折線:
this.polyline = new google.maps.Polyline({
path : [fromPosition, toPosition],
strokeColor : '#CCCCCC',
strokeOpacity : 1.0,
strokeWeight : 2
});
var polyline = this.polyline;
編輯05-APR-2012
以下是造成問題的代碼,請解釋它爲什麼會發生並推薦任何解決方案。由於
function Link(from, to) {
this.from = from;
this.to = to;
}
Link.prototype.show = function() {
this.line = new google.maps.Polyline({
path : [this.from, this.to],
strokeColor : "#0000FF",
strokeOpacity : 0.5,
strokeWeight : 6
});
this.line.setMap(map);
google.maps.event.addListener(this.line, 'mouseover', function() {
this.line.setOptions({
strokeOpacity : 1
});
});
google.maps.event.addListener(this.line, 'mouseout', function() {
this.line.setOptions({
strokeOpacity : 0.5
});
});
}
var links = [];
var link2 = new Link(new google.maps.LatLng(-3.5999999999999996, 23.4), new google.maps.LatLng(-4.5, 23.4)), link1 = new Link(new google.maps.LatLng(-3.5999999999999996, 23.4), new google.maps.LatLng(-3.5999999999999996, 18));
links.push(link1);
links.push(link2);
// I've a long list of links, so I'll prefer a loop
for(var i = 0; i < links.length; i++) {
links[i].show();
}
的jsfiddle演示:http://jsfiddle.net/wasimbhalli/9bg6x/
試試這個http://lab.gmtplusone.com/go ogle-maps/polyline-click-issue/ – 2012-03-27 19:10:38
你能發表更多的代碼嗎?是折線全局還是在其他內容的環境中聲明? – javram 2012-03-27 19:11:08
多義線是否重疊?預期的行爲是,當兩條多段線重疊時,監聽器應該爲兩條多段線啓動? – javram 2012-03-28 04:48:53