Get KML featureData Field Attributes

4,155 views
Skip to first unread message

Tyler Waring

unread,
Feb 6, 2012, 8:03:34 PM2/6/12
to google-map...@googlegroups.com
I am trying to get access to the field attributes for a clicked on feature in a KML file with the ultimate goal of populating an info window. 

What call is necessary to get information from the KML file? 

Thanks, Tyler 


geoco...@gmail.com

unread,
Feb 6, 2012, 8:30:23 PM2/6/12
to Google Maps JavaScript API v3

Tyler Waring

unread,
Feb 6, 2012, 9:07:58 PM2/6/12
to google-map...@googlegroups.com
That's kind of where I dead ended.

google.maps.event.addListener(kmlLayer, 'click', function(kmlEvent) {
  var name = kmlEvent.featureData.name;
  var text = kmlEvent.featureData.description;
  var pos = kmlEvent.latLng;
  var offset = kmlEvent.pixelOffset;
  alert(name);
  showInFormatedInfoWindow(name, text, pos, offset);
  });

function showInFormatedInfoWindow(name, text, position, offset) {
  iw.setOptions({
  content: "<div class='infowindow'><b>" + name + "<\/b><p>" + text + "<\/p><\/div>",
  position: position,
  pixelOffset: offset });
  iw.open(map);
 }

I ended up getting undefined values for kmlEvent.featureData.name and kmlEvent.featureData.description.

Not sure if there is a problem with my kml file or not. I converted it from a standard ESRI shapefile using QGIS.




geoco...@gmail.com

unread,
Feb 6, 2012, 9:31:15 PM2/6/12
to Google Maps JavaScript API v3
If you provide a link to your map and kml file (as requested in the
posting guidelines) it would be easier to help.

-- Larry

Tyler Waring

unread,
Feb 6, 2012, 9:49:25 PM2/6/12
to google-map...@googlegroups.com
Thanks Larry. 
I have a kml site up at https://sites.google.com/site/geogodesigns/kmlservices/newParcelKML.kml . I do not have a map site set up yet. I am just testing locally at this point and trying to make the switch from the ESRI/Flex world to that of the GoogleMaps/JavaScript. I'm still trying to connect all the dots. 

Thanks, Tyler 

geoco...@gmail.com

unread,
Feb 6, 2012, 10:18:31 PM2/6/12
to Google Maps JavaScript API v3
On Feb 6, 6:49 pm, Tyler Waring <geogodesi...@gmail.com> wrote:
> Thanks Larry.
> I have a kml site up at

https://sites.google.com/site/geogodesigns/kmlservices/newParcelKML.kml

http://maps.google.com/maps?q=https:%2F%2Fsites.google.com%2Fsite%2Fgeogodesigns%2Fkmlservices%2FnewParcelKML.kml&hl=en&sll=35.313024,-80.822983&sspn=0.113322,0.154324&t=m&z=12

The placemarks in that kml don't have name or description tags
associated with them.

-- Larry

geoco...@gmail.com

unread,
Feb 6, 2012, 10:30:28 PM2/6/12
to Google Maps JavaScript API v3
On Feb 6, 7:18 pm, "geocode...@gmail.com" <geocode...@gmail.com>
wrote:
> On Feb 6, 6:49 pm, Tyler Waring <geogodesi...@gmail.com> wrote:
>
> > Thanks Larry.
> > I have a kml site up at
>
> https://sites.google.com/site/geogodesigns/kmlservices/newParcelKML.kml
>
> http://maps.google.com/maps?q=https:%2F%2Fsites.google.com%2Fsite%2Fg...
>
> The placemarks in that kml don't have name or description tags
> associated with them.

You might find the kml reference useful (if you haven't found it
already):

http://code.google.com/apis/kml/documentation/kmlreference.html

Tyler Waring

unread,
Feb 6, 2012, 10:46:13 PM2/6/12
to google-map...@googlegroups.com
Thanks Larry.

So using a portion of my kml as an example:

<?xml version="1.0" encoding="utf-8" ?>
<kml xmlns="http://www.opengis.net/kml/2.2">
<Document><name>newParcelKML</name>
<Schema name="newParcelKML" id="newParcelKML">
    <SimpleField name="Name" type="string"></SimpleField>
    <SimpleField name="Description" type="string"></SimpleField>
    <SimpleField name="PID" type="string"></SimpleField>
    <SimpleField name="TaxPID" type="string"></SimpleField>
    <SimpleField name="NC_PIN" type="string"></SimpleField>
    <SimpleField name="CodeMunici" type="string"></SimpleField>
    <SimpleField name="Municipali" type="string"></SimpleField>
    <SimpleField name="CardNo" type="int"></SimpleField>
    <SimpleField name="Assess_Y_N" type="string"></SimpleField>
    <SimpleField name="Proposed_A" type="float"></SimpleField>
    <SimpleField name="Assess_Not" type="string"></SimpleField>
    <SimpleField name="ParcelID" type="string"></SimpleField>
    <SimpleField name="CurrentBui" type="float"></SimpleField>
    <SimpleField name="CurrentYar" type="float"></SimpleField>
    <SimpleField name="CurrentLan" type="float"></SimpleField>
    <SimpleField name="CurrentTot" type="float"></SimpleField>
    <SimpleField name="LUC" type="string"></SimpleField>
    <SimpleField name="Shape_Leng" type="float"></SimpleField>
    <SimpleField name="Shape_Area" type="float"></SimpleField>
</Schema>
  <Placemark>
    <Style><LineStyle><color>ff0000ff</color></LineStyle><PolyStyle><fill>1</fill></PolyStyle></Style>
    <ExtendedData><SchemaData schemaUrl="#newParcelKML">
        <SimpleData name="PID">01915147</SimpleData>
        <SimpleData name="TaxPID">01915147</SimpleData>
        <SimpleData name="NC_PIN">4559728888</SimpleData>
        <SimpleData name="CodeMunici">0</SimpleData>
        <SimpleData name="Municipali">UNINC</SimpleData>
        <SimpleData name="CardNo">1</SimpleData>
        <SimpleData name="Assess_Y_N">N</SimpleData>
        <SimpleData name="Proposed_A">0.00000000000</SimpleData>
        <SimpleData name="Assess_Not"></SimpleData>
        <SimpleData name="ParcelID">01915147</SimpleData>
        <SimpleData name="CurrentBui">0.00000000000</SimpleData>
        <SimpleData name="CurrentYar">0.00000000000</SimpleData>
        <SimpleData name="CurrentLan">48500.00000000000</SimpleData>
        <SimpleData name="CurrentTot">48500.00000000000</SimpleData>
        <SimpleData name="LUC">R100</SimpleData>
        <SimpleData name="Shape_Leng">1723.12833221000</SimpleData>
        <SimpleData name="Shape_Area">133991.61199800001</SimpleData>
    </SchemaData></ExtendedData>
      <Polygon><outerBoundaryIs><LinearRing><coordinates>-80.81885739292926,35.3646306637987 -80.818901172224244,35.36460062913018 -80.818941301332146,35.364762850048407 -80.819008831602716,35.364969644631643 -80.819022329856011,35.365010662858126 -80.819034755532059,35.365041226978832 -80.819077764120735,35.365146217446622 -80.819117285279475,35.365243702194768 -80.819216229329186,35.365467558348982 -80.819340491586374,35.365698398510773 -80.81936703877156,35.365742325867693 -80.819376200596949,35.365757467613555 -80.819202771838306,35.365765732502368 -80.817534225999765,35.365900527872377 -80.817367592638192,35.365912645294621 -80.817577997927643,35.36572003866327 -80.817785505235349,35.365529892288805 -80.817752323138649,35.365508763539395 -80.817872276960514,35.36539704091409 -80.81805488811797,35.365236782992021 -80.818061465299508,35.365230858215362 -80.818114976465921,35.365185921453119 -80.818238394209843,35.365079120561418 -80.818329443961218,35.365005617838122 -80.818394995330507,35.364956021156409 -80.818475488021349,35.364897956043691 -80.818561301522067,35.364834836930797 -80.818690960100895,35.364744623175717 -80.818754562376256,35.364700908689045 -80.81885739292926,35.3646306637987</coordinates></LinearRing></outerBoundaryIs></Polygon>
  </Placemark>
</Document></kml>

If I wanted to access the attribute value for "PID", how would I do that with the Google Maps JavaScript API. I am completely missing the linkage between the JavaScript world and the kml world.

Thanks, Tyler

Rossko

unread,
Feb 7, 2012, 6:31:14 AM2/7/12
to Google Maps JavaScript API v3
> If I wanted to access the attribute value for "PID", how would I do that
> with the Google Maps JavaScript API.

The documentation for google.maps.KmlFeatureData suggests it only
gives the standard KML content of its choice, and gives you no access
to <ExtendedData> content. That's not too suprising.

I think you might have to look into client-side KML parsing and
probably customise it to get at that data.

geoco...@gmail.com

unread,
Feb 7, 2012, 8:24:21 AM2/7/12
to Google Maps JavaScript API v3
On Feb 6, 7:18 pm, "geocode...@gmail.com" <geocode...@gmail.com>
wrote:
> On Feb 6, 6:49 pm, Tyler Waring <geogodesi...@gmail.com> wrote:
>
> > Thanks Larry.
> > I have a kml site up at
>
> https://sites.google.com/site/geogodesigns/kmlservices/newParcelKML.kml
>
> http://maps.google.com/maps?q=https:%2F%2Fsites.google.com%2Fsite%2Fg...
>
> The placemarks in that kml don't have name or description tags
> associated with them.

Here is an example of the US map, displaying the <name> and
<description> data in the sidebar.

http://www.geocodezip.com/v3_GoogleEx_layer-kml_us_states_simpleA.html

Sureshkumar Jayapal

unread,
Jan 31, 2018, 4:34:47 PM1/31/18
to Google Maps JavaScript API v3
Hi 

is there a way to get all feature data name on the laod complete event.

Also I wanted to display the feature data name on the left hand side and when the user clicks the name, it should position to that record on screen

can this be done
Reply all
Reply to author
Forward
0 new messages