Polygons from XML file

932 views
Skip to first unread message

PhilR8

unread,
Jan 22, 2010, 5:56:23 PM1/22/10
to Google Maps JavaScript API v3
I've been toying around with the XML tutorial written by Pamela
(http://gmaps-samples-v3.googlecode.com/svn/trunk/xmlparsing/
downloadurl_info.html) and am trying to do something similar with
polygons as well as markers. Here's what I've been fooling around
with so far:

http://pages.towson.edu/preese/av/multiple_polys_from_xml.html

There are two polygons (Alabama, Arkansas) that I'd like to see on the
map, but instead I only see Arkansas. I've been searching around the
group and figure that this is some sort of "last in a loop" JavaScript
bug (I think Ben Appleton posted this link http://scriptnode.com/article/the-last-in-a-loop-bug/)
but I'm still wrestling with how to fix the problem. Any thoughts?

Michał Biniek

unread,
Jan 23, 2010, 3:30:36 AM1/23/10
to Google Maps JavaScript API v3
Hi,

you have set array of points as global - so you overwrite points last
polygon's data.
Try put var pts = []; to loop instead of making it global, like this:

var pts = [];
for (var i = 0; i < points.length; i++) {
pts[i] = new google.maps.LatLng(parseFloat(points[i].getAttribute
("lat")),
parseFloat(points[i].getAttribute("lng")));
}

Regards,
Michal Biniek

On 22 Sty, 23:56, PhilR8 <phi...@gmail.com> wrote:
> I've been toying around with the XML tutorial written by Pamela
> (http://gmaps-samples-v3.googlecode.com/svn/trunk/xmlparsing/
> downloadurl_info.html) and am trying to do something similar with
> polygons as well as markers.  Here's what I've been fooling around
> with so far:
>
> http://pages.towson.edu/preese/av/multiple_polys_from_xml.html
>
> There are two polygons (Alabama, Arkansas) that I'd like to see on the
> map, but instead I only see Arkansas.  I've been searching around the
> group and figure that this is some sort of "last in a loop" JavaScript

> bug (I think Ben Appleton posted this linkhttp://scriptnode.com/article/the-last-in-a-loop-bug/)

PhilR8

unread,
Feb 26, 2010, 3:07:51 PM2/26/10
to Google Maps JavaScript API v3
Just realized that I never properly thanked you, Michal. Thanks!!!

PhilR8

unread,
Feb 26, 2010, 6:40:07 PM2/26/10
to Google Maps JavaScript API v3
So, similarly, here's a test map:

http://pages.towson.edu/preese/av/point_in_poly_test.html

As you can probably deduce from the URL, I'm trying to do some point-
in-polygon stuff. Specifically, using the click listener to open a
marker infowindow that is inside of the polygon. I have two polygons
in the demo above (Utah and Wyoming) and corresponding markers, but no
matter where I click in either polygon, the Wyoming infowindow is
opened. I used to do this successfully in V2 using Mike Williams'
point in polygon example, but I'm not having any luck figuring out how
to do it in V3 from the examples that I've found. Anyone have any
thoughts? Thanks in advance.

Marcelo

unread,
Feb 27, 2010, 1:29:20 AM2/27/10
to Google Maps JavaScript API v3
On Feb 27, 12:40 am, PhilR8 <phi...@gmail.com> wrote:
> So, similarly, here's a test map:
>
> http://pages.towson.edu/preese/av/point_in_poly_test.html
>
> As you can probably deduce from the URL, I'm trying to do some point-
> in-polygon stuff.  Specifically, using the click listener to open a
> marker infowindow that is inside of the polygon.  I have two polygons
> in the demo above (Utah and Wyoming) and corresponding markers, but no
> matter where I click in either polygon, the Wyoming infowindow is
> opened.  

You're not obtaining function closure on the polygons.
Just like you create the markers in a separate function, you need to
do the same with the polygons.
http://econym.org.uk/gmap/closure.htm

--
Marcelo - http://maps.forum.nu
--

PhilR8

unread,
Mar 1, 2010, 5:14:40 PM3/1/10
to Google Maps JavaScript API v3
Wow, thanks. I actually managed to do that, as you can see here:

http://pages.towson.edu/preese/av/point_in_poly_test.html (same link
as above)

But now the polygons look strange, like there are two or three (or
more) stacked on top of each other. Notice the difference from
before:

http://pages.towson.edu/preese/av/point_in_poly_test_bad.html

What am I doing wrong?

On Feb 27, 1:29 am, Marcelo <marcelo...@hotmail.com> wrote:
> On Feb 27, 12:40 am, PhilR8 <phi...@gmail.com> wrote:
>
> > So, similarly, here's a test map:
>
> >http://pages.towson.edu/preese/av/point_in_poly_test.html
>
> > As you can probably deduce from the URL, I'm trying to do some point-
> > in-polygon stuff.  Specifically, using the click listener to open a
> > marker infowindow that is inside of the polygon.  I have two polygons
> > in the demo above (Utah and Wyoming) and corresponding markers, but no
> > matter where I click in either polygon, the Wyoming infowindow is
> > opened.  
>
> You're not obtaining function closure on the polygons.
> Just like you create the markers in a separate function, you need to
> do the same with the polygons.http://econym.org.uk/gmap/closure.htm
>
> --

> Marcelo -http://maps.forum.nu

Marcelo

unread,
Mar 1, 2010, 5:39:34 PM3/1/10
to Google Maps JavaScript API v3
On Mar 1, 11:14 pm, PhilR8 <phi...@gmail.com> wrote:
> But now the polygons look strange, like there are two or three (or
> more) stacked on top of each other.  Notice the difference from
> before:

You are creating a new polygon for every individual point that you
read.
Indent your code properly and you will see it.

--
Marcelo - http://maps.forum.nu
--

>

PhilR8

unread,
Mar 1, 2010, 5:47:10 PM3/1/10
to Google Maps JavaScript API v3
Yep, there it was. I might have been wrong about you, Marcelo. I
couldn't have done it without you! Thanks!

On Mar 1, 5:39 pm, Marcelo <marcelo...@hotmail.com> wrote:
> On Mar 1, 11:14 pm, PhilR8 <phi...@gmail.com> wrote:
>
> > But now the polygons look strange, like there are two or three (or
> > more) stacked on top of each other.  Notice the difference from
> > before:
>
> You are creating a new polygon for every individual point that you
> read.
> Indent your code properly and you will see it.
>
> --

> Marcelo -http://maps.forum.nu

Reply all
Reply to author
Forward
0 new messages