# OpenLayers.Format.KML

Read/Write KML. Create a new instance with the OpenLayers.Format.KML constructor.

#### Inherits from

- OpenLayers.Format.XML

Summary

OpenLayers.Format.KML | Read/Write KML.
Properties |
namespaces | {Object} Mapping of namespace aliases to namespace URIs.
kmlns | {String} KML Namespace to use.
placemarksDesc | {String} Name of the placemarks.
foldersName | {String} Name of the folders.
foldersDesc | {String} Description of the folders.
extractAttributes | {Boolean} Extract attributes from KML.
kvpAttributes | {Boolean} Only used if extractAttributes is true.
extractStyles | {Boolean} Extract styles from KML.
extractTracks | {Boolean} Extract gx:Track elements from Placemark elements.
trackAttributes | {Array} If extractTracks is true, points within gx:Track elements will be parsed as features with when, heading, tilt, and roll attributes.
internalns | {String} KML Namespace to use -- defaults to the namespace of the Placemark node being parsed, but falls back to kmlns.
features | {Array} Array of features
styles | {Object} Storage of style objects
styleBaseUrl | {String}
fetched | {Object} Storage of KML URLs that have been fetched before in order to prevent reloading them.
maxDepth | {Integer} Maximum depth for recursive loading external KML URLs Defaults to 0: do no external fetching
Constructor |
OpenLayers.Format.KML | Create a new parser for KML.
Functions and Properties |
read | Read data from a string, and return a list of features.
parseData | Read data from a string, and return a list of features.
parseLinks | Finds URLs of linked KML documents and fetches them
fetchLink | Fetches a URL and returns the result
parseStyles | Parses Style nodes
parseKmlColor | Parses a kml color (in ‘aabbggrr’ format) and returns the corresponding color and opacity or null if the color is invalid.
parseStyle | Parses the children of a Style node and builds the style hash accordingly
parseStyleMaps | Parses StyleMap nodes, but only uses the ‘normal’ key
parseFeatures | Loop through all Placemark nodes and parse them.
readers | Contains public functions, grouped by namespace prefix, that will be applied when a namespaced node is found matching the function name.
parseFeature | This function is the core of the KML parsing code in OpenLayers.
getStyle | Retrieves a style from a style hash using styleUrl as the key If the styleUrl doesn’t exist yet, we try to fetch it Internet
parseGeometry | Properties of this object are the functions that parse geometries based on their type.
parseGeometry.point | Given a KML node representing a point geometry, create an OpenLayers point geometry.
parseGeometry.linestring | Given a KML node representing a linestring geometry, create an OpenLayers linestring geometry.
parseGeometry.polygon | Given a KML node representing a polygon geometry, create an OpenLayers polygon geometry.
parseGeometry.multigeometry | Given a KML node representing a multigeometry, create an OpenLayers geometry collection.
parseAttributes |
parseExtendedData | Parse ExtendedData from KML.
parseProperty | Convenience method to find a node and return its value
write | Accept Feature Collection, and return a string.
createFolderXML | Creates and returns a KML folder node
createPlacemarkXML | Creates and returns a KML placemark node representing the given feature.
buildGeometryNode | Builds and returns a KML geometry node with the given geometry.
buildGeometry | Object containing methods to do the actual geometry node building based on geometry type.
buildGeometry.point | Given an OpenLayers point geometry, create a KML point.
buildGeometry.multipoint | Given an OpenLayers multipoint geometry, create a KML GeometryCollection.
buildGeometry.linestring | Given an OpenLayers linestring geometry, create a KML linestring.
buildGeometry.multilinestring | Given an OpenLayers multilinestring geometry, create a KML GeometryCollection.
buildGeometry.linearring | Given an OpenLayers linearring geometry, create a KML linearring.
buildGeometry.polygon | Given an OpenLayers polygon geometry, create a KML polygon.
buildGeometry.multipolygon | Given an OpenLayers multipolygon geometry, create a KML GeometryCollection.
buildGeometry.collection | Given an OpenLayers geometry collection, create a KML MultiGeometry.
buildCoordinatesNode | Builds and returns the KML coordinates node with the given geometry <coordinates>...</coordinates>
buildCoordinates |
buildExtendedData |

### Properties

### namespaces

{Object} Mapping of namespace aliases to namespace URIs.

### kmlns

{String} KML Namespace to use. Defaults to 2.0 namespace.

### placemarksDesc

{String} Name of the placemarks. Default is “No description available”.

### foldersName

{String} Name of the folders. Default is “OpenLayers export”. If set to null, no name element will be created.

### foldersDesc

{String} Description of the folders. Default is “Exported on [date].” If set to null, no description element will be created.

### extractAttributes

{Boolean} Extract attributes from KML. Default is true. Extracting styleUrls requires this to be set to true Note that currently only Data and SimpleData elements are handled.

### kvpAttributes

{Boolean} Only used if extractAttributes is true. If set to true, attributes will be simple key-value pairs, compatible with other formats, Any displayName elements will be ignored. If set to false, attributes will be objects, retaining any displayName elements, but not compatible with other formats. Any CDATA in displayName will be read in as a string value. Default is false.

### extractStyles

{Boolean} Extract styles from KML. Default is false. Extracting styleUrls also requires extractAttributes to be set to true

### extractTracks

{Boolean} Extract gx:Track elements from Placemark elements. Default is false. If true, features will be generated for all points in all gx:Track elements. Features will have a when (Date) attribute based on when elements in the track. If tracks include angle elements, features will have heading, tilt, and roll attributes. If track point coordinates have three values, features will have an altitude attribute with the third coordinate value.

### trackAttributes

{Array} If extractTracks is true, points within gx:Track elements will be parsed as features with when, heading, tilt, and roll attributes. Any additional attribute names can be provided in trackAttributes.

### internalns

{String} KML Namespace to use -- defaults to the namespace of the Placemark node being parsed, but falls back to kmlns.

### features

{Array} Array of features

### styles

{Object} Storage of style objects

### styleBaseUrl

{String}

### fetched

{Object} Storage of KML URLs that have been fetched before in order to prevent reloading them.

### maxDepth

{Integer} Maximum depth for recursive loading external KML URLs Defaults to 0: do no external fetching

### Constructor

### OpenLayers.Format.KML

Create a new parser for KML.

#### Parameters

options | {Object} An optional object whose properties will be set on this instance.

### Functions and Properties

### read

> read: function( | data | )

Read data from a string, and return a list of features.

#### Parameters

data | {String} or {DOMElement} data to read/parse.

#### Returns

{Array(OpenLayers.Feature.Vector)} List of features.

### parseData

> parseData: function( | data,
> | options | )

Read data from a string, and return a list of features.

#### Parameters

data | {String} or {DOMElement} data to read/parse.
options | {Object} Hash of options

#### Returns

{Array(OpenLayers.Feature.Vector)} List of features.

### parseLinks

> parseLinks: function( | nodes,
> | options | )

Finds URLs of linked KML documents and fetches them

#### Parameters

nodes | {Array} of {DOMElement} data to read/parse.
options | {Object} Hash of options

### fetchLink

> fetchLink: function( | href | )

Fetches a URL and returns the result

#### Parameters

href | {String} url to be fetched

### parseStyles

> parseStyles: function( | nodes,
> | options | )

Parses Style nodes

#### Parameters

nodes | {Array} of {DOMElement} data to read/parse.
options | {Object} Hash of options

### parseKmlColor

> parseKmlColor: function( | kmlColor | )

Parses a kml color (in ‘aabbggrr’ format) and returns the corresponding color and opacity or null if the color is invalid.

#### Parameters

kmlColor | {String} a kml formated color

#### Returns

{Object}

### parseStyle

> parseStyle: function( | node | )

Parses the children of a Style node and builds the style hash accordingly

#### Parameters

node | {DOMElement} Style node

### parseStyleMaps

> parseStyleMaps: function( | nodes,
> | options | )

Parses StyleMap nodes, but only uses the ‘normal’ key

#### Parameters

nodes | {Array} of {DOMElement} data to read/parse.
options | {Object} Hash of options

### parseFeatures

> parseFeatures: function( | nodes,
> | options | )

Loop through all Placemark nodes and parse them. Will create a list of features

#### Parameters

nodes | {Array} of {DOMElement} data to read/parse.
options | {Object} Hash of options

### readers

Contains public functions, grouped by namespace prefix, that will be applied when a namespaced node is found matching the function name. The function will be applied in the scope of this parser with two arguments: the node being read and a context object passed from the parent.

### parseFeature

> parseFeature: function( | node | )

This function is the core of the KML parsing code in OpenLayers. It creates the geometries that are then attached to the returned feature, and calls parseAttributes() to get attribute data out.

#### Parameters

node | {DOMElement}

#### Returns

{OpenLayers.Feature.Vector} A vector feature.

### getStyle

> getStyle: function( | styleUrl,
> | options | )

Retrieves a style from a style hash using styleUrl as the key If the styleUrl doesn’t exist yet, we try to fetch it Internet

#### Parameters

styleUrl | {String} URL of style
options | {Object} Hash of options

#### Returns

{Object} | (reference to) Style hash

### parseGeometry

Properties of this object are the functions that parse geometries based on their type.

### parseGeometry.point

Given a KML node representing a point geometry, create an OpenLayers point geometry.

#### Parameters

node | {DOMElement} A KML Point node.

#### Returns

{OpenLayers.Geometry.Point} A point geometry.

### parseGeometry.linestring

Given a KML node representing a linestring geometry, create an OpenLayers linestring geometry.

#### Parameters

node | {DOMElement} A KML LineString node.

#### Returns

{OpenLayers.Geometry.LineString} A linestring geometry.

### parseGeometry.polygon

Given a KML node representing a polygon geometry, create an OpenLayers polygon geometry.

#### Parameters

node | {DOMElement} A KML Polygon node.

#### Returns

{OpenLayers.Geometry.Polygon} A polygon geometry.

### parseGeometry.multigeometry

Given a KML node representing a multigeometry, create an OpenLayers geometry collection.

#### Parameters

node | {DOMElement} A KML MultiGeometry node.

#### Returns

{OpenLayers.Geometry.Collection} A geometry collection.

### parseAttributes

> parseAttributes: function( | node | )

#### Parameters

node | {DOMElement}

#### Returns

{Object} An attributes object.

### parseExtendedData

> parseExtendedData: function( | node | )

Parse ExtendedData from KML. Limited support for schemas/datatypes. See http://code.google.com/apis/kml/documentation/kmlreference.html#extendeddata for more information on extendeddata.

### parseProperty

> parseProperty: function( | xmlNode,
> | namespace,
> | tagName | )

Convenience method to find a node and return its value

#### Parameters

xmlNode | {<DOMElement>}
namespace | {String} namespace of the node to find
tagName | {String} name of the property to parse

#### Returns

{String} The value for the requested property (defaults to null)

### write

> write: function( | features | )

Accept Feature Collection, and return a string.

#### Parameters

features | {Array(OpenLayers.Feature.Vector)} An array of features.

#### Returns

{String} A KML string.

### createFolderXML

> createFolderXML: function()

Creates and returns a KML folder node

#### Returns

{DOMElement}

### createPlacemarkXML

> createPlacemarkXML: function( | feature | )

Creates and returns a KML placemark node representing the given feature.

#### Parameters

feature | {OpenLayers.Feature.Vector}

#### Returns

{DOMElement}

### buildGeometryNode

> buildGeometryNode: function( | geometry | )

Builds and returns a KML geometry node with the given geometry.

#### Parameters

geometry | {OpenLayers.Geometry}

#### Returns

{DOMElement}

### buildGeometry

Object containing methods to do the actual geometry node building based on geometry type.

### buildGeometry.point

Given an OpenLayers point geometry, create a KML point.

#### Parameters

geometry | {OpenLayers.Geometry.Point} A point geometry.

#### Returns

{DOMElement} A KML point node.

### buildGeometry.multipoint

Given an OpenLayers multipoint geometry, create a KML GeometryCollection.

#### Parameters

geometry | {OpenLayers.Geometry.Point} A multipoint geometry.

#### Returns

{DOMElement} A KML GeometryCollection node.

### buildGeometry.linestring

Given an OpenLayers linestring geometry, create a KML linestring.

#### Parameters

geometry | {OpenLayers.Geometry.LineString} A linestring geometry.

#### Returns

{DOMElement} A KML linestring node.

### buildGeometry.multilinestring

Given an OpenLayers multilinestring geometry, create a KML GeometryCollection.

#### Parameters

geometry | {OpenLayers.Geometry.Point} A multilinestring geometry.

#### Returns

{DOMElement} A KML GeometryCollection node.

### buildGeometry.linearring

Given an OpenLayers linearring geometry, create a KML linearring.

#### Parameters

geometry | {OpenLayers.Geometry.LinearRing} A linearring geometry.

#### Returns

{DOMElement} A KML linearring node.

### buildGeometry.polygon

Given an OpenLayers polygon geometry, create a KML polygon.

#### Parameters

geometry | {OpenLayers.Geometry.Polygon} A polygon geometry.

#### Returns

{DOMElement} A KML polygon node.

### buildGeometry.multipolygon

Given an OpenLayers multipolygon geometry, create a KML GeometryCollection.

#### Parameters

geometry | {OpenLayers.Geometry.Point} A multipolygon geometry.

#### Returns

{DOMElement} A KML GeometryCollection node.

### buildGeometry.collection

Given an OpenLayers geometry collection, create a KML MultiGeometry.

#### Parameters

geometry | {OpenLayers.Geometry.Collection} A geometry collection.

#### Returns

{DOMElement} A KML MultiGeometry node.

### buildCoordinatesNode

> buildCoordinatesNode: function( | geometry | )

Builds and returns the KML coordinates node with the given geometry <coordinates>...</coordinates>

#### Parameters

geometry | {OpenLayers.Geometry}

#### Returns

{DOMElement}

### buildCoordinates

> buildCoordinates: function( | point | )

#### Parameters

point | {OpenLayers.Geometry.Point}

Returns {String} a coordinate pair

### buildExtendedData

> buildExtendedData: function( | attributes | )

#### Parameters

attributes | {Object}

Returns {DOMElement} A KML ExtendedData node or {null} if no attributes.

{Boolean} Extract gx:Track elements from Placemark elements.

> read: function( | data | )

Read data from a string, and return a list of features.

> parseData: function( | data,
> | options | )

Read data from a string, and return a list of features.

> parseLinks: function( | nodes,
> | options | )

Finds URLs of linked KML documents and fetches them

> fetchLink: function( | href | )

Fetches a URL and returns the result

> parseStyles: function( | nodes,
> | options | )

Parses Style nodes
This class represents a UserStyle obtained from a SLD, containing styling rules.

> parseKmlColor: function( | kmlColor | )

Parses a kml color (in ‘aabbggrr’ format) and returns the corresponding color and opacity or null if the color is invalid.

> parseStyle: function( | node | )

Parses the children of a Style node and builds the style hash accordingly

> parseStyleMaps: function( | nodes,
> | options | )

Parses StyleMap nodes, but only uses the ‘normal’ key

> parseFeatures: function( | nodes,
> | options | )

Loop through all Placemark nodes and parse them.

> parseFeature: function( | node | )

This function is the core of the KML parsing code in OpenLayers.

> getStyle: function( | styleUrl,
> | options | )

Retrieves a style from a style hash using styleUrl as the key If the styleUrl doesn’t exist yet, we try to fetch it Internet

> parseAttributes: function( | node | )

> parseExtendedData: function( | node | )

Parse ExtendedData from KML.

> parseProperty: function( | xmlNode,
> | namespace,
> | tagName | )

Convenience method to find a node and return its value

> write: function( | features | )

Accept Feature Collection, and return a string.

> createFolderXML: function()

Creates and returns a KML folder node

> createPlacemarkXML: function( | feature | )

Creates and returns a KML placemark node representing the given feature.

> buildGeometryNode: function( | geometry | )

Builds and returns a KML geometry node with the given geometry.

> buildCoordinatesNode: function( | geometry | )

Builds and returns the KML coordinates node with the given geometry coordinates...</coordinates>

> buildCoordinates: function( | point | )

> buildExtendedData: function( | attributes | )

Create a new parser for KML.
Read and write XML.
{Array} If extractTracks is true, points within gx:Track elements will be parsed as features with when, heading, tilt, and roll attributes.
Vector features use the OpenLayers.Geometry classes as geometry description.
Point geometry class.
A LineString is a Curve which, once two points have been added to it, can never be less than two points long.
Polygon is a collection of Geometry.LinearRings.
A Collection is exactly what it sounds like: A collection of different Geometries.
A Geometry is a description of a geographic object.
A Linear Ring is a special LineString which is closed.
Close