问题
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