Map single polygon from a KML file

1,253 views
Skip to first unread message

robbom

unread,
Jun 6, 2010, 7:26:02 AM6/6/10
to Google Maps JavaScript API v3
Hi,
Using the Google Maps API v 3, I wish to extract out one polygon from
a KML file and display that polygon as an overlay.

The KML file has 55 polygons, however based on passing a unique ID
(e.g. the name of an area, attached as an attibute on each polygon),
to the KML file I wish to just store one of the polygons in a polygon
variable, then use that single polygon as an overlay on the map.


Is this possible? Thanks.

Chad Killingsworth

unread,
Jun 6, 2010, 8:44:26 AM6/6/10
to Google Maps JavaScript API v3
You won't be able to use the KMLLayer to do this - it will render the
entire file.

There isn't an easily pre-written way to do that. You'd have to read
in the KML file, parse the results and find your specific polygon,
then create a google maps polygon using the specified points.

Chad Killingsworth

William

unread,
Jun 6, 2010, 11:03:02 PM6/6/10
to Google Maps JavaScript API v3
this is possible using a data source which supports queries. At the
moment there are two options, either a Fusion Map Layer or the Maps
Data API KML Feature Feed.

You can specify a query using the Maps Data API KML Feed with the mq
parameter.

For example if you were searching for the State of New York, and it
had an id of NY, then the url would include this parameter ?mq=[id:NY]

if we take the State Boundary KML polygons from this map
http://2010.census.gov/2010census/take10map/

and load them into a "My Map" which is accessible to the Maps Data
API, then you can make a map like the following example which allows
the user to choose a State, and also allows the user to decide how to
highlight the state, either a blue boundary with light shaded infill,
or a solid black boundary with inverse shading of areas outside the
state (use the "Inverse" checkbox to toggle the symbology display)

http://www.william-map.com/20100607/1/states.htm

...

Stephen Lead

unread,
Jun 6, 2010, 11:48:38 PM6/6/10
to google-map...@googlegroups.com
William,

This is very useful - thanks for posting it. At the top of your script you use:

var stateBoundaryFeed =
"http://maps.google.com/maps/feeds/features/108113052932458397912.00048842e1d3c599c5f02/full";

Presumably this is serving out the US state boundary polygons? How did
you find this URL, and are there similar feeds for other data sources
around the world?

Cheers,
Steve

> --
> 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-map...@googlegroups.com.
> To unsubscribe from this group, send email to google-maps-js-a...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/google-maps-js-api-v3?hl=en.
>

William

unread,
Jun 7, 2010, 12:49:52 AM6/7/10
to Google Maps JavaScript API v3
Hi Stephen, I made that feed myself as a learning exercise using a KML
file of State Boundary Polygons (which I sourced from the google Take
10 Census map mentioned above).

The steps to create the feed were:
1. Include attribute data in each KML Placemark as <ExtendedData> with
the standard State abbreviations as an identifier for each State.
2. Break any <MultiGeometry> into separate <Polygon> Placemarks,
because the Maps Data API only allows a single geometric feature. For
example Hawaii has multiple Polygons.
3. Use the Maps Data API to create a new blank map (which by default
will has api_visible set to true) and record the map id for future
reference.
4. Use "My Maps" at maps.google.com to load the KML into the map,
using the Import KML function.

If you use "My Maps" to upload the KML data, then attribute data
should be held as <ExtendedData> in the Placemarks, but if you use the
Maps Data API to upload the data, the attribute data should be held as
<gd:customProperty> on the <atom:entry>

The feed identifier has two parts: the first identifies the user
(standard google account) and the second identifies the map.

The next step for me is to upload County and State boundaries with the
FIPS codes as the search criteria.

...

robbom

unread,
Jun 7, 2010, 7:52:58 AM6/7/10
to Google Maps JavaScript API v3
Thanks for your interesting reponses guys.

William, your example is what I am after, thanks for posting. I
haven't had allot to do with the Maps Data API, however I had a quick
look into it. The way I understand it is that you can either upload a
KML file to "My Maps" using either the Maps Data API (some good
examples of different client libraries here:
http://code.google.com/apis/maps/documentation/mapsdata/examples.html)
or using the maps.google.com interface (the Import KML function, which
in turn creates a feed). If you do the later, am I correct in saying
that you still have to use the Maps Data API to obtain the feed
identifier? i.e. the user and map id.

William

unread,
Jun 7, 2010, 5:51:55 PM6/7/10
to Google Maps JavaScript API v3
>If you do the later, am I correct in saying
> that you still have to use the Maps Data API to obtain the feed
> identifier? i.e. the user and map id.
these are visible in My Maps when you click the "Link" button, the
"msid" is included in the url.

There are two tasks:
1. Creating a Map, and
2. Creating Features in a Map.

You create an empty map then import data (features) into into it.

I found it easier to create an empty map with the API because the
property "api_visible" is automatically set to true. If you create
the map with My Maps, then you need to use the API to change the
"api_visible" property afterwards and I couldn't get that to work.

Also I found it easier to import data into the map using My Maps
because it automatically reduces the number of points in polygons.
There seems to be an upper limit on the number of points in a polygon
(500 points) and this prevented me loading some polygons using the API
(for example Texas boundary). This limit requires further
investigation.

robbom

unread,
Jun 9, 2010, 12:48:53 AM6/9/10
to Google Maps JavaScript API v3
Thanks for the clarification William. I also struck the issue of the
polygon node limitation. I used FME software to generalise the
polygons prior to construting the KML file, which seemed to do the
trick.

William

unread,
Jun 9, 2010, 1:20:13 AM6/9/10
to Google Maps JavaScript API v3
Another thing for the future, there was an announcement at the Google
IO 2010 presentation about the Maps Data API where they promised JSON
upload/download of Maps data instead of KML. This will make working
with the API a lot easier in javascript.
Reply all
Reply to author
Forward
0 new messages