谷歌地图点击事件不完全正确
google maps click event not working completely correct
>我使用以下脚本生成此页面
function initialize() {
var mapCanvas = document.getElementById('map');
var mapOptions = {center:new google.maps.LatLng(latitudeMid,longitudeMid),zoom:15,mapTypeId:google.maps.MapTypeId.ROADMAP,streetViewControl:false,mapTypeControl:true,scaleControl:true,scaleControlOptions:{position:google.maps.ControlPosition.TOP_RIGHT}};
var map = new google.maps.Map(mapCanvas, mapOptions);
var i;
var insertion;
var previousMarker;
// -------------------------------
//show locations on the map
// -------------------------------
for (i = 0; i < fotoCount; i++) {
var myLatLng=new google.maps.LatLng(Latituden[i], Longituden[i]);
var marker = new StyledMarker({styleIcon:new StyledIcon(StyledIconTypes.MARKER,{color:'00ff00',text:Letters[i]}),position:myLatLng,map:map});
marker.set('zIndex', -i);
insertion='<img src='"http://www.pdavis.nl/Ams/'.concat(Bestanden[i],'.jpg'"></img>');
insertion=insertion.concat('<table class=width100><tr><td>Bestand: ',Bestanden[i],'</td><td class=pright>Lokatie: ',Latituden[i],' °N., ',Longituden[i],' °E. (',Letters[i],')</td>');
insertion=insertion.concat('<td class=pright>Genomen: ',Datums[i],'</td></tr><td colspan=3>Object: ',Objecten[i],'</td></table>');
google.maps.event.addListener(marker, 'click', function() {
$('#photo').html(insertion);
this.styleIcon.set('color', 'ff0000');
if(previousMarker!=null){previousMarker.styleIcon.set('color', '00ff00')};
previousMarker=this;
});
}
单击标记应执行两件事:(i) 将标记变为红色(以及任何现有的红色标记为绿色)和 (ii) 在右侧面板中显示带有信息的相应照片。第一个确实有效,但第二个始终显示与最后一个标记相对应的照片。用"警报(插入);" 显示这对每个标记都是正确的。
你不能这样做,因为在循环结束时,"i"将永远是最后一个索引。当然,当您单击标记时,回调中的"i"值是最后一个索引,因此您应该始终显示最后一张图片。
仅仅将插入代码放入点击回调是不够的,因为 i 值。您没有绑定任何内容来修复回调中的值,因此您会遇到同样的问题。
下面的解决方案使用标记对象绑定"i"值,像这样你可以在回调中使用它。
在网页上测试的脚本:)。
随心所欲地调整它!
function initialize() {
var mapCanvas = document.getElementById('map');
var mapOptions = {center:new google.maps.LatLng(latitudeMid,longitudeMid),zoom:15,mapTypeId:google.maps.MapTypeId.ROADMAP,streetViewControl:false,mapTypeControl:true,scaleControl:true,scaleControlOptions:{position:google.maps.ControlPosition.TOP_RIGHT}};
var map = new google.maps.Map(mapCanvas, mapOptions);
var i;
var previousMarker;
// -------------------------------
//show locations on the map
// -------------------------------
for (i = 0; i < fotoCount; i++) {
var myLatLng=new google.maps.LatLng(Latituden[i], Longituden[i]);
var marker = new StyledMarker({styleIcon:new StyledIcon(StyledIconTypes.MARKER,{color:'00ff00',text:Letters[i]}),position:myLatLng,map:map});
marker.set('zIndex', -i);
marker.myIndex = i;
google.maps.event.addListener(marker, 'click', function() {
var insertion = "";
insertion='<img src='"http://www.pdavis.nl/Ams/'.concat(Bestanden[this.myIndex],'.jpg'"></img>');
insertion=insertion.concat('<table class=width100><tr><td>Bestand: ',Bestanden[this.myIndex],'</td><td class=pright>Lokatie: ',Latituden[this.myIndex],' °N., ',Longituden[this.myIndex],' °E. (',Letters[this.myIndex],')</td>');
insertion=insertion.concat('<td class=pright>Genomen: ',Datums[this.myIndex],'</td></tr><td colspan=3>Object: ',Objecten[this.myIndex],'</td></table>');
$('#photo').html(insertion);
this.styleIcon.set('color', 'ff0000');
if(previousMarker!=null){previousMarker.styleIcon.set('color', '00ff00')};
previousMarker=this;
});
}
}
插入应该是一个数组。这样,当您迭代时,在 eacj 迭代中,您只是覆盖插入的内容。最后,您将图像数组中的最后一个值作为插入。
var insertionArr = [];
...
insertion=insertion.concat('<td class=pright>Genomen: ',Datums[i],'</td></tr><td colspan=3>Object: ',Objecten[i],'</td></table>');
insertionArr[marker] = insertion; // Add it to the array
google.maps.event.addListener(marker, 'click', function() {
$('#photo').html(insertionArr[this]);// get it from the array
...
});
这不是经过测试的代码。
相关文章:
- 如何测试主干视图是否正确侦听特定事件
- 将事件发送到javascript文档的react本机模块是否正确
- ReactJS:如何使用react正确播放带有onClick事件的html5视频
- 如何正确停止mousemove事件
- 事件侦听器,用于完全关闭后重新启动的应用程序
- MVC 下拉列表更改事件未正确调用控制器中的操作
- 当我们的设备从纵向模式定向到横向模式时,当地图完全加载时,是否有任何 Map 事件被触发
- 错误:'这'对象不正确;在JQuery中单击事件-单击时的意外行为
- extjs网格真实的完全后渲染事件
- 为什么ng模式获胜't对来自非用户输入事件的更改进行正确验证
- 如果放置目标被完全占用,则不会触发HTML5-拖放-ondragover事件
- 什么是完全删除GoogleMaps自动完成的正确方法
- 如何正确捕捉mouseout事件
- onChange事件在以编程方式加载后未正确触发
- 如何正确使用'$(这个)'以调用触发该事件的DOM元素
- SAPUI5:在表单完全呈现时获取事件
- 找不到使用 ckEditor 成功上传的正确事件
- 输入日期-当前日期函数很接近,但不完全正确
- 只有在发生正确事件时才注册Java脚本
- 谷歌地图点击事件不完全正确