javascript - Google Maps API v3: Add Polylines inside a loop? -
i'm in process of migrating our map application v2 v3 (3.2). had loop read latlng , put them in array display polyline on map. set length of array 0 , fill again in loop other latlng display polyline... , on (many times).
it working fine in v2, not in v3. since code complex, tried write simpliest code possible demonstrate issue:
function setpolyline(points) { var polyline = new google.maps.polyline({ path: points, strokecolor: '#ff0000', strokeopacity: 0.5, strokeweight: 2 }); polyline.setmap(map); } var mapdiv = document.getelementbyid('map'); var mapcenter = new google.maps.latlng(0,0); var mapoptions = { zoom: 2, center: mapcenter, backgroundcolor: '#e1e1e1', maptypeid: google.maps.maptypeid.roadmap } map = new google.maps.map(mapdiv, mapoptions); var points=[]; points[0]=new google.maps.latlng(-35, 71); points[1]=new google.maps.latlng(-36, 75); points[2]=new google.maps.latlng(-37, 91); setpolyline(points); points.length=0; points[0]=new google.maps.latlng(-31, 71); points[1]=new google.maps.latlng(-32, 75); setpolyline(points);
what happens second polyline shown (and not first one). make works, have either use different variable name (points1, points2, points3, ...) can't use because code in loop or re-declare variable each time in loop instead of before (var points=[] before each polyline , remove points.length=0 line).
maybe i'm missing javascript, declare variable outside loop (before) once , use inside loop.
what doing wrong? can help?
here's simple map did demonstrate issue:
http://www.canamgroup.ws/gm.nsf/map?openpage
(it's not in loop, issue same. last polyline displayed if don't use different variable name array or if don't re-declare each time)
thanks!
in case might somebody, see other discussion: http://code.google.com/apis/maps/documentation/javascript/forum.html?place=topic%2fgoogle-maps-js-api-v3%2flp9tyywkph4%2fdiscussion
Comments
Post a Comment