Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/csharp-4.0/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 变量在子函数中变为未定义_Javascript_Google Maps - Fatal编程技术网

Javascript 变量在子函数中变为未定义

Javascript 变量在子函数中变为未定义,javascript,google-maps,Javascript,Google Maps,Im使用GoogleMapsJavaScriptAPI显示一张满是位置标记的地图。标记显示出来,但一旦在控制台上单击,就会出现错误:infowindow.setContent(“”+位置[i][0]+””)处的“未捕获类型错误:无法读取未定义的属性“0”)。我发现问题在于脚本不知道I是什么。我用一个数字替换I,验证了这个结论,它工作正常。到目前为止,我已经尝试将I作为一个全局变量,并将其值作为window.I进行测试。它仍然不起作用,但在控制台中显示了它的最后一次计数。我做错什么了吗 funct

Im使用GoogleMapsJavaScriptAPI显示一张满是位置标记的地图。标记显示出来,但一旦在控制台上单击,就会出现错误:
infowindow.setContent(“”+位置[i][0]+”

”)处的“未捕获类型错误:无法读取未定义的属性“0”)。我发现问题在于脚本不知道
I
是什么。我用一个数字替换
I
,验证了这个结论,它工作正常。到目前为止,我已经尝试将
I
作为一个全局变量,并将其值作为
window.I
进行测试。它仍然不起作用,但在控制台中显示了它的最后一次计数。我做错什么了吗

function initialize() {
    var locations = [<?php echo $jscript; ?>];

    map = new google.maps.Map(document.getElementById('map-canvas'), {
        zoom: 8,
        center: new google.maps.LatLng(32.639594, -97.117879),
        mapTypeId: google.maps.MapTypeId.ROADMAP
    });

    var infowindow = new google.maps.InfoWindow();

    var marker = '';
    var i = 0;
    for (i = 0; i < locations.length; i++) {
        var geocoder_map = new google.maps.Geocoder();
        var addlocat;

        geocoder_map.geocode({'address': locations[i][0]}, function (results, status) {
            if (results) {
                addlocat = results[0].geometry.location;

                marker = new google.maps.Marker({
                    position: addlocat,
                    map: map
                });

                google.maps.event.addListener(marker, 'click', (function (marker, i) {
                    return function () {
                        infowindow.setContent('<div><p>' + locations[i][0] + '</p></div>');
                        infowindow.open(map, marker);
                    }
                })(marker, i));

            }
        });
    }
}

google.maps.event.addDomListener(window, 'load', initialize);
函数初始化(){
var位置=[];
map=new google.maps.map(document.getElementById('map-canvas'){
缩放:8,
中心:新google.maps.LatLng(32.639594,-97.117879),
mapTypeId:google.maps.mapTypeId.ROADMAP
});
var infowindow=new google.maps.infowindow();
var标记='';
var i=0;
对于(i=0;i');
信息窗口。打开(地图、标记);
}
})(marker,i));
}
});
}
}
google.maps.event.addDomListener(窗口“加载”,初始化);

按照@FelixKling的结构,我破解了这个,第一次尝试就成功了:

<script>
function initialize() {
      locations = [<?php echo $jscript; ?>];

    map = new google.maps.Map(document.getElementById('map-canvas'), {
      zoom: 8,
      center: new google.maps.LatLng(32.639594, -97.117879),
      mapTypeId: google.maps.MapTypeId.ROADMAP
    });

    infowindow = new google.maps.InfoWindow();

    var i = 0;
    for (i = 0; i < locations.length; i++) {  
    geocode(i, locations[i])
   }
}

function geocode(i, location){
    var geocoder_map = new google.maps.Geocoder();
    var addlocat;

    geocoder_map.geocode( { 'address': location[0]}, function(results, status) {
            if(results){
            addlocat = results[0].geometry.location; 

      var marker = new google.maps.Marker({
        position: addlocat,
        map: map
      });

      google.maps.event.addListener(marker, 'click',
        return function() {
          infowindow.setContent('<div><p>'+location[0]+'</p></div>');
          infowindow.open(map, marker);
        });
      }
    });
}

google.maps.event.addDomListener(window, 'load', initialize);

    </script>

函数初始化(){
地点=[];
map=new google.maps.map(document.getElementById('map-canvas'){
缩放:8,
中心:新google.maps.LatLng(32.639594,-97.117879),
mapTypeId:google.maps.mapTypeId.ROADMAP
});
infowindow=new google.maps.infowindow();
var i=0;
对于(i=0;i');
信息窗口。打开(地图、标记);
});
}
});
}
google.maps.event.addDomListener(窗口“加载”,初始化);

如果已修复,那么问题出在哪里?如果控制台显示i的最新值,这就是它应该执行的操作,除非每次使用它时都将其归零,否则应将IIFE放在传递给
geocoder\u map.geocode
的函数周围,而不是事件处理程序周围。现在已经太晚了。另外,将
标记
放在
geocode
callback.ooo本地。哦,这里有一个大错误。你不能在客户端的服务器端javascript中使用php,这是不允许的,你可以使用ajax从服务器中填充你的字段,可能是和的副本。我将创建一个新函数,包含当前循环中的所有内容。然后您只需在循环内调用函数,如
geocode(i,locations[i])。然后避免循环问题。您可能需要传递更多参数(例如,
infowindow
)或使其成为全局参数(或者更好地,将其置于非全局的共享、更高的范围内)。但是将
i
和位置直接传递给函数。您也可以对事件处理程序执行相同的操作(正如我已经提到的),但是将代码拆分为多个函数也会提高可读性。在
geocode
内部,将
marker
设为本地(
var marker=…;
),然后可以删除
(函数(marker,I){……})(marker,I)
围绕事件处理程序展开。此外,您实际上没有在
geocode()
函数中使用
i
,因此无需将其作为参数传入。确定。谢谢你纠正我。