Hi,

The issue is that Polyline takes a reference to your points array, but you
reuse the points array in both Polylines. The result is that 2 identical
polylines are rendered on top of each other. Instead if you replace
    points.length = 0;  // Clobber the first array
with
    points = []; // Create a new array
your code will work as you expect.

Ben

On Sat, Nov 13, 2010 at 1:34 AM, Canam Group <canamgr...@gmail.com> wrote:

> Hi,
>
> I'm in the process of migrating our Map application from V2 to V3 (3.2).
> Sorry, I can't post a link to our map cause we use it internally (don't
> worry, we paid for this).
>
> I had a loop that read LatLng and put them in a array to display as a
> Polyline on the map.
> I then set the length of my array to 0 and then fill it again in the loop
> with other LatLng to display another Polyline... and so on (many times).
>
> It was working fine in V2, but not in V3.
> Since my code is complex, I tried to write the simpliest code possible to
> demonstrate my 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 is that only the second Polyline is shown (and not the first
> one).
> To make it works, I have to either use a different variable name (points1,
> points2, points3, ...) which I can't use because my code is normally in a
> loop or re-declare my variable each time in the loop instead of before it
> (var points=[] before each Polyline and remove the points.length=0 line).
>
> Maybe I'm missing something about JavaScript, but I usually declare my
> variable outside the loop (before) once and use it inside the loop.
>
> What am I doing wrong?
> Can someone help?
>
> Thanks!
>
> --
> You received this message because you are subscribed to the Google Groups
> "Google Maps JavaScript API v3" group.
> To post to this group, send email to
> google-maps-js-api...@googlegroups.com.
> To unsubscribe from this group, send email to
> google-maps-js-api-v3+unsubscr...@googlegroups.com<google-maps-js-api-v3%2bunsubscr...@googlegroups.com>
> .
> For more options, visit this group at
> http://groups.google.com/group/google-maps-js-api-v3?hl=en.
>

-- 
You received this message because you are subscribed to the Google Groups 
"Google Maps JavaScript API v3" group.
To post to this group, send email to google-maps-js-api...@googlegroups.com.
To unsubscribe from this group, send email to 
google-maps-js-api-v3+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/google-maps-js-api-v3?hl=en.

Reply via email to