Overlapping polygons click - KML layer google maps API v3

1,884 views
Skip to first unread message

GAPINewbie

unread,
May 17, 2011, 5:08:57 PM5/17/11
to Google Maps JavaScript API v3
Hi,

I was wondering if its possible to identify which polygon was clicked
on the overlapping polygons. In my application I generate KML file
which has collection of zip codes' coordinates. Each zip code is a
placemark in the kml and has its own style setting with some
information to popup when clicked. Some of the zipcodes are
overlapping or contained inside another zip code. In these cases when
I click the polygon it pop ups only one zipcode information wherever I
click. I guess the problem is that it's not able to identify which
polygon was clicked. Is there a way to separate the polygons somehow
like draw the small one on top of the big one so we can differentiate
the clicks or some parameter to bring all polygon to front then being
stacked on each other. Any suggestions are highly appreciated

Rossko

unread,
May 17, 2011, 6:16:43 PM5/17/11
to Google Maps JavaScript API v3
> I was wondering if its possible to identify which polygon was clicked
> on the overlapping polygons.

If we can't tell which one the user intended to click, I'm not sure
how the code could. In Google Earth you could give them some
altitude, but maps are flat so that won't work here.

> In my application I generate KML file ...

Generate unambiguous KML ; you can create polygons with holes in,
where your 'overlapping' poly can fit. This should deal with 'island'
zipcodes inside other zipcodes.

GAPINewbie

unread,
May 27, 2011, 10:18:41 AM5/27/11
to Google Maps JavaScript API v3
Yes I did see that in Google earth using Altitude we can make some
differentiation.

Am not sure I understand your suggestion about creating polygon's with
holes. My understanding is that the geographic data(from the source
census.gov) would take care of holes if any. But am guessing may be
the data is messed up? Sorry am pretty new to this KML/Geo spatial
stuffs. Can you give me some e.g. how to go about creating holes in
the polygons while generating my KML.

GAPINewbie

unread,
May 31, 2011, 4:33:48 PM5/31/11
to Google Maps JavaScript API v3
I solved it. As per Rossko suggestiong of creating holes in the
polygon while generating KML files. This answer is for anyone who is
still searching:

Check if polygons are overlapping if so get the overlapping polygons
geography or polygon data and while generating KML add them under
<innerBoundaryIs> of the container one. say for e.g Zipcode A overlaps
zipcode B then you can add zipcode B's geographic data into
<innerBoundaryIs> of Zipcode A.

To find out if two polygons intersect what I did was, I get the list
of zipcodes and its corresponding geography instance, loop through
each one and check if it overlaps:

say sourceZipcode = collection[0].zipcode, souceGeography=
collection[0].geography

for(int i=0;i<collection.count;i++)
{
if (!collection[j].Zipcode.Equals(sourceZipcode ) &&
collection[j].Geography.STDifference(souceGeography).STNumPoints() ==
0)
{
GeoData newData = new GeoData{ Zipcode = zipcode,
OverlappingZipcode = geoCollection[j].Zipcode, OverlappingGeography =
geoCollection[j].Geography };
//check if entry exists
if (!_overlappingPolygons.Exists(s => s.OverlappingZipcode ==
newData.OverlappingZipcode &&
s.OverlappingGeography.STEquals(newData.OverlappingGeography).IsTrue))
{
//there is a overlapping polygon add it to list.
_overlappingPolygons.Add(newData);
}
}
}

this way I have the overlappingpolygons for the zipcode under check
and add them to innerboundaryIs tag.
Reply all
Reply to author
Forward
0 new messages