Dynamic Markers - Google Maps API - Markers not incrementing with i value in loop

我的未来我决定 提交于 2019-12-02 07:24:46

The old "function scope wrapper" trick (function(i) {...})(i) works here. Without it, the execution takes the final value of i. Also, if(i = 1) should be if(i == 1)

To play around: http://jsfiddle.net/BK8vv/

for (var i = 1; i < total; i++) {  
   (function(i) {

    // Use geocoder to grab latlong for user inputed address
    geocoder.geocode( { 'address': address[i]}, function(results, status) {
        if (status == google.maps.GeocoderStatus.OK) {
            // Redefine map center location
            if (i == 1){ map.setCenter(results[0].geometry.location); }

            // Create dynamic markers on map as per the address array
                    alert(i);
            var marker = new google.maps.Marker({
                map: map, 
                position: results[0].geometry.location,
                title:address[i],
                zIndex: i,
                // PROBLEM CODE --- Using the function below it creates all Markers with a "1" instead of the "i" value..??  
                icon: pinImage(i),
                shadow: pinShadow
            });         
        }
       });
     })(i);
    }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!