本文介绍了Google Maps Trouble:Closures&amp;通过引用传递的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 限时删除!! 我有一些Google Maps / Javascript问题。我想我知道麻烦是什么,但只是不知道它的方式。 我的问题的一个例子是这里。无论您点击什么标记,第二个都会显示出来。我显然将错误的信息传递给我的事件侦听器,但我似乎无法获得正确的代码。下面是代码的一部分: 首先,这是我的代码的一部分: if(GBrowserIsCompatible()&& mapResults!= null){ //在JSON中读取 var mapDetailsArray = loadJSON(); //创建地图 var map = new google.maps.Map2(document.getElementById(elementId)); map.setCenter(new google.maps.LatLng(mapDetailsArray [0] .getLatitude(), mapDetailsArray [0] .getLongitude()),13); map.addControl(new google.maps.SmallMapControl()); //添加点和中心 var mgr = new google.maps.MarkerManager(map); var bounds = new google.maps.LatLngBounds(); for(var i = 0; i< mapDetailsArray.length; i ++){ var mapDetails = mapDetailsArray [i]; var point = new google.maps.LatLng(mapDetails.getLatitude(),mapDetails .getLongitude()); bounds.extend(point); //创建我们的标记 var marker = new google.maps.Marker(point); //我们用什么动作来显示悬停 var infoAction =mouseover; marker.value = mapDetails; google.maps.Event.addListener(marker,infoAction,function(){ alert(marker.value); map.openInfoWindowHtml(point,getResultInfoContent(marker.value )); }); mgr.addMarker(marker,1); } mgr.refresh(); } marker.value总是被设置为mapDetails的最后一个值是。如果有的话,我希望它是未定义的,因为一旦循环结束,不应该这个价值消失吗?我尝试了各种不同的值(例如marker.value = i + 1;),但它仍然会以两个点的相同值出现。 有没有人任何想法如何将正确的值传递给事件监听器? 解决方案更改此项: google.maps.Event.addListener(marker,infoAction,function(){ alert(marker.value); map.openInfoWindowHtml (point,getResultInfoContent(marker.value)); }); b google.maps.Event.addListener(marker,infoAction,(function(marker,point,map)){return function(){ alert(marker.value); map。 openInfoWindowHtml(point,getResultInfoContent(marker.value)); }})(marker,point,map)); 这会创建你想要的闭包。 $ b $ pre $ var $ f函数(标记,点,地图) {返回函数() { alert(marker.value); map.openInfoWindowHtml(point,getResultInfoContent(marker.value)); google.maps.Event.addListener(marker,infoAction,f(marker,point,map)); I'm having some Google Maps/Javascript problems. I think I know what the trouble is but just don't know a way round it.An example of my problem is here. Whatever marker you click on, the second shows up. I'm obviously passing the wrong info into my event listener but I just can't seem to get the code right. Here's a cut down portion of the code:First, here's a portion of my code:if (GBrowserIsCompatible() && mapResults != null) { // Read in the JSON var mapDetailsArray = loadJSON(); // Create a map var map = new google.maps.Map2(document.getElementById(elementId)); map.setCenter(new google.maps.LatLng(mapDetailsArray[0].getLatitude(), mapDetailsArray[0].getLongitude()), 13); map.addControl(new google.maps.SmallMapControl()); // Add the points and center var mgr = new google.maps.MarkerManager(map); var bounds = new google.maps.LatLngBounds(); for ( var i = 0; i < mapDetailsArray.length; i++) { var mapDetails = mapDetailsArray[i]; var point = new google.maps.LatLng(mapDetails.getLatitude(), mapDetails .getLongitude()); bounds.extend(point); // Create our marker var marker = new google.maps.Marker(point); // What action do we use to show the hover var infoAction = "mouseover"; marker.value = mapDetails; google.maps.Event.addListener(marker, infoAction, function() { alert(marker.value); map.openInfoWindowHtml(point, getResultInfoContent(marker.value)); }); mgr.addMarker(marker, 1); } mgr.refresh();}"marker.value" always ends up set as the last value of what mapDetails was. If anything, I'd expect it to be "undefined" as once the loop has finished, shouldn't that value be gone anyway? I've tried various different values (e.g. marker.value = i+1;) but it will still come up as the same value for both points.Does anyone have any idea how I can pass the correct values into the event listener? 解决方案 Change this:google.maps.Event.addListener(marker, infoAction, function() { alert(marker.value); map.openInfoWindowHtml(point, getResultInfoContent(marker.value)); });to this:google.maps.Event.addListener(marker, infoAction, (function(marker, point, map) { return function() { alert(marker.value); map.openInfoWindowHtml(point, getResultInfoContent(marker.value)); }})(marker, point, map));This will create the closure you want.Split out for clarity:var f = function(marker, point, map){ return function() { alert(marker.value); map.openInfoWindowHtml(point, getResultInfoContent(marker.value)); }}google.maps.Event.addListener(marker, infoAction, f(marker, point, map)); 这篇关于Google Maps Trouble:Closures&amp;通过引用传递的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 1403页,肝出来的..
09-06 10:27