首页 / 浏览问题 / 其他 / 问题详情
循环调用marker事件的匿名方法怎么闭包
22EXP 2017年05月18日
for(var i = 0; i<len; i++){
                    var jhBjdArr = new Array();
                    jhBjdArr = cxjgInfoArr[i].split(",");
                    
                    var jdxzmc = jhBjdArr[1];
                    jd = jhBjdArr[2];
                    wd = jhBjdArr[3];
                    var nsrsl = jhBjdArr[4];
                    
                    var size = new SuperMap.Size(30,25);
                    var offset =  new SuperMap.Pixel(8, -30);
                    var jhIcon = new SuperMap.Icon(Constants.contextRoot+"/map/superMap/theme/images/marker.png", size,offset);
                    var point = new SuperMap.LonLat(jd, wd);
                    var marker = new SuperMap.Marker(point,jhIcon);
                    
                    
                    marker.events.on({
                        "mousemove":function(){addJhMarkerInfo(marker,nsrsl,jdxzmc,point)}
                    });
                    marker.events.on({
                        "mouseout":removePopup
                    });
                    
                    markerlayer.addMarker(marker);
                }

想这个样子的代码,marker.events.on({"mousemove":function(){addJhMarkerInfo(marker,nsrsl,jdxzmc,point)}});这段代码怎么能一个标记点对应一个,而不是一个所有标记点对应最后一个,或者一次性像marker.events.on({"mousemove":(function(){addJhMarkerInfo(marker,nsrsl,jdxzmc,point)})()});这么写,加载就全执行了,事件根本没用

1个回答

您好,

1.mousemove事件代表的是鼠标在地图移动的时候就会触发,如果您想要实现鼠标放在marker上显示话请监听mouseover事件。

2.在您遍历输出marker的之前就应该遍历一下您的要传入的数组,您可以参考以下代码:

function processCompleted(queryEventArgs){ 
					 var result = queryEventArgs.result;
						if (result && result.totalCount>0) {
							  var resultTable="";
							  for (var i =0;i<result.recordsets.length;i++) {
									if (result.recordsets[i].features) {
										var arrFields = new Array();
										var intFieldCount=result.recordsets[i].fields.length;
										resultTable +="<table id='datatable' align='left' style='position:relative;width:300px;height:300px;overflow-y:auto;' border='1'>";
										var strTableHead="";
											for(var n=0;n<intFieldCount;n++)
											{   var fieldName=result.recordsets[i].fields[n];
												strTableHead +="<td>";
												strTableHead +=fieldName;
												arrFields.push(fieldName);
											}
											strTableHead +="<td>";
											strTableHead +="<input type='submit' value='refresh' >";
										    resultTable +="<tr>"+strTableHead+"</tr>";
											for(var k=0;k<result.recordsets[i].features.length;k++)
											{  var point = result.recordsets[i].features[k].geometry,
											   size = new SuperMap.Size(22, 20),
											   offset = new SuperMap.Pixel(-(size.w / 2), -size.h),
											   icon2 = new SuperMap.Icon("../theme/images/marker-gold.png", size, offset);
											   var feature=new SuperMap.Feature.Vector();
											   feature=result.recordsets[i].features[k]; 
											   var marker=new SuperMap.Marker(new SuperMap.LonLat(point.x, point.y), icon2);
											   marker.chao=feature;
											   markerLayerSQL.addMarker(marker);
                                               marker.events.on({//注册点击事件的处理方法
                                                                  "mouseover":openInfoWin,
                                                                  "mouseout": setTimeout111,
                                                                  "scope": marker
                                                                 });
											   resultTable += "<tr>";
												for(var j=0;j<intFieldCount;j++){
													resultTable +="<td>";
													resultTable += feature.attributes[arrFields[j]];
													if (j%2==1){
													  resultTable +="<td>";
													  resultTable +="<input type='checkbox' >";
													}
													resultTable +="</td>";
													
												}
												resultTable +="</tr>";
											}
									}
							  }
						}
						else{ resultTable="<p>无查询结果</p>";} 
						document.getElementById("queryResultPanel").innerHTML=resultTable;
						
				    }
                    var infowin = null;
		           function	openInfoWin() {
		           	      closeInfoWin()
                                 //关闭之前打开的窗口。
                          var marker = this;
                          var lonlat = marker.lonlat;
                          var rainObj=marker.chao;
                          var contentHTML = "<div style='font-size:.8em; opacity: 0.8; overflow-y:hidden;'>";
                              contentHTML += "<div>"+rainObj.attributes.name+ "</div>"
                            var size = new SuperMap.Size(0, 33);
                            var offset = new SuperMap.Pixel(0, -size.h);
                            var icon2 = new SuperMap.Icon("../theme/images/marker-gold.png", size, offset);
                            var popup = new SuperMap.Popup.FramedCloud("popwin",
                                      new SuperMap.LonLat(lonlat.lon,lonlat.lat),
                                              null,
                                              contentHTML,
                                              icon2,
                                              false);
 
                            infowin = popup;
                            map.addPopup(popup);
		           }

1,695EXP 2017年05月19日
主要是我必须用iemarker.events.on({//注册点击事件的处理方法 "mouseover":openInfoWin, "mouseout": setTimeout111, "scope": marker });像这种写法,我写成marker.events.on({
           "mousemove":addJhMarkerInfo,
           "scope":{marker,nsrsl,jdxzmc,point}
        });这样子,ie就不支持了,地图就不显示了
...