Polyline()不接受Google Maps API MVCArray

时间:2011-05-27 19:26:23

标签: javascript jquery google-maps google-maps-api-3

我有一个简单的代码绘制标记,然后用折线将它们连接在一起,但我试图用Polyline的MVCArray不起作用(并且“不工作”我的意思是Polyline不是被绘制)。这是我的代码:

$(document).ready(function(){
    var latlng = new google.maps.LatLng(36.686041,-80.661621);
    var map = new google.maps.Map(document.getElementById("network_map"),{
        zoom: 6,
        center: latlng,
        mapTypeId: google.maps.MapTypeId.ROADMAP
    });

    $("input[name='submit']").click(function() {
        var geocoder = new google.maps.Geocoder();
        var locations = new google.maps.MVCArray();
        var i = 0;
        $("input[name='address[]']").each(function() {
            geocoder.geocode({"address" : this.value }, function(results) {
                var addrLl = results[0].geometry.location;
                locations.insertAt(locations.length, addrLl);
                var marker = new google.maps.Marker({
                    map: map,
                    position: addrLl
                });
            });
        });
        console.log(locations);
        var connectPath = new google.maps.Polyline({
            path: locations,
            strokeColor: "#FF0000",
            strokeOpacity: 1.0,
            strokeWeight: 100,
            map: map
        });
        return false;
    });
});

// console.log(locations); result from firebug
W { b=[0], gm_accessors_={...}, length=0}

1 个答案:

答案 0 :(得分:0)

我能够通过设置100ms超时来解决我的问题,以防止MVCArray被发送到Polyline,直到地理编码器完成地理编码。