Google Maps API MVCArray isn't being accepted by Polyline()

别来无恙 提交于 2019-12-25 08:57:20

问题


I have a simple code plotting markers and then connecting them together with a polyline but the MVCArray I am trying to use with the Polyline isn't working (and by "isn't working" I mean that the Polyline isn't being plotted). Here is my code:

$(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:


I was able to solve my issue by setting a 100ms timeout to prevent the MVCArray from being sent to Polyline until the geocoder had finished geocoding.



来源:https://stackoverflow.com/questions/6156801/google-maps-api-mvcarray-isnt-being-accepted-by-polyline

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!