Hide Table of Contents
esri
Map
plugins
process
toolbars
workers
Class: KMLLayer
[ AMD Module Require | Legacy Module Require ]

dojo.require("esri.layers.KMLLayer");

Description

Class added at v2.4
The KMLLayer class is used to create a layer based on a KML file (.kml,.kmz). KML is an XML-based file format used to represent geographic features. To add a KML file(.kml or .kmz) to a map, the KML must be available via a publicly accessible URL. Locally hosted or KML files inside a firewall are not supported. Accessing features from a KML file

Features from the KML file are accessible via the api so you can query features and use them as input to geometry or geoprocessing services. You can access and manipulate a KML layer's features like any other layer. Because of the wide range of feature types that can be stored in a KML file, a KML layer added to a map is internally made up of several layers. An ArcGIS API for JavaScript KML layer can be thought of as more of a group layer rather than a single layer.

Geometries from a KML file are stored as feature layers: one each for points, lines and polygons. Feature layers for a particular geometry type are only created if there are features of that geometry type in the KML file. The getLayers() method returns the layers that make up a KML file. The following code snippet shows how to get a KML layer's layers, checks that a layer has graphics, unions the layer extents and then zooms to the new extent:


  var kmlExtent, layers = kml.getLayers();

  dojo.forEach(layers, function(lyr) {

    if ( lyr.graphics && lyr.graphics.length > 0 ) {

      var lyrExtent = esri.geometry.geographicToWebMercator(

        esri.graphicsExtent(lyr.graphics)

      );

      if ( globals.kmlExtent ) {

        kmlExtent = kmlExtent.union(lyrExtent);

      } else {

        kmlExtent = lyrExtent;

      }

  });

  map.setExtent(kmlExtent);

Supported Features

The following KML features are supported at version 2.4:

  • Placemarks
  • Network links without refresh parameters
  • Ground overlays
  • Folders
  • Point, polyline, and polygon symbology, including icons
  • HTML in feature descriptions
Support for the following was added at version 2.5:
  • Network links with refresh parameters
  • ExtendedData feature attributes
  • Time
The following features are not supported:
  • Screen overlays
  • Regionated KML
  • Regions inside network links

Samples

Search for samples that use this class.

Class hierarchy

esri/layers/Layer
|_esri/layers/KMLLayer

Constructors

NameSummary
new esri.layers.KMLLayer(id,url,options?)Creates a new KMLLayer object

Properties

NameTypeSummary
attributionDataUrlStringThe URL, when available, where the layer's attribution data is stored.
classNameStringclass attribute of the layer's node.
credentialCredentialProvides credential information for the layer such as userid and token if the layer represents a resource that is secured with token-based authentication.
featureInfosObject[]

An array of objects that describe top-level KML features ids and their types.

foldersKMLFolder[]An array of KMLFolder objects that describe the folders and nested folders defined in the KML file.
hasAttributionDataBooleanWhen true the layer has attribution data.
idStringID assigned to the layer.
linkInfoObjectA link info object with properties that describe the network link.
loadErrorErrorSet if the layer failed to load.
loadedBooleanWhen the layer is loaded, the value becomes "true", and layer properties can be accessed.
maxScaleNumberMaximum visible scale for the layer.
minScaleNumberMinimum visible scale for the layer.
opacityNumberOpacity or transparency of layer.
refreshIntervalNumberRefresh interval of the layer in minutes.
showAttributionBooleanWhen true the layer's attribution is displayed on the map.
suspendedBooleanWhen true the layer is suspended.
urlStringURL to the ArcGIS Server REST resource that represents a map service.
visibleBooleanVisibility of the layer
visibleAtMapScaleBooleanWhen true, the layer is visible at the current map scale.

Methods

NameTypeSummary
attr(name,value)LayerAdds a new attribute or changes the value of an existing attribute on the layer's node.
getAttributionData()DeferredAsynchrously returns custom data for the layer when available.
getFeature(featureInfo)Object

Get the KML feature identified by the input feature info.

getLayers()Layer[]Get an array of map layers that were created to draw placemarks, ground and screen overlays.
getMap()MapReturns reference to the map control the layer is added to.
getNode()HTMLElementReturns the layer's DOM node.
hide()NoneSets the visibility of the layer to "false".
isVisibleAtScale(scale)BooleanReturns true if the layer is visible at the given scale.
resume()NoneResumes layer drawing.
setFolderVisibility(folder,isVisible)NoneSet the visibility for the specified folder
setMaxScale(scale)NoneSet the maximum scale for the layer.
setMinScale(scale)NoneSet the minimum scale for the layer.
setOpacity(opacity)NoneSets the opacity of the layer.
setRefreshInterval(interval)LayerChanges the layer's refresh interval to the given value (in minutes).
setScaleRange(minScale,maxScale)NoneSet the scale range for the layer.
setVisibility(isVisible)NoneSets the visibility of the layer.
show()NoneSets the visibility of the layer to "true"
suspend()NoneSuspends layer drawing.

Events

[ On Style Events | Connect Style Event ]
All On Style event listeners receive a single event object. The event properties listed below are accessed from this event object. Additionally, the event object also contains a 'target' property whose value is the object which fired the event.

Events

NameEvent PropertiesSummary
error
<Error> error
Fires when there is a problem retrieving a layer.
load
<Layer> layer
Fires after layer properties for the layer are successfully populated.
opacity-change
<Number> opacity
Fires when the layer opacity has been changed, and returns an object with the opacity value.
refreshFired after the layer is refreshed.
refresh-interval-changeThis event is fired when the layer's refreshInterval is modified.
resumeFires when a layer resumes drawing.
scale-range-changeFires when a layer's minScale and/or maxScale is changed.
scale-visibility-changeFires when a layer's scale visibility changes.
suspendFires when a layer suspends drawing.
updateFires any time a layer has finished loading or updating itself.
update-end
<Error> error
Fires when a layer has finished updating its content.
update-startFires when a layer begins to update its content.
visibility-change
<Boolean> visible
Fires when the layer visibility has been changed, and returns an object with a Boolean visible property containing the new visibility value of the layer.
Constructor Details

new esri.layers.KMLLayer(id,url,options?)

Creates a new KMLLayer object.
Arguments:
<String> id Required Id to assign to the layer. This id is used for the layer and as the base for all sub layers that are created.
<String> url Required The url for a .kml or .kmz file.
<Object> options Optional Optional parameters. See options list. View the Layer object for additional options.
Parameters:
<String> className Optional Class attribute to set for the layer's node.
<SpatialReference> outSR Optional The output spatial reference for the KMLLayer.
<Number> refreshInterval Optional Refresh interval of the layer in minutes. Non-zero value sets up automatic layer refresh at the specified interval.
Sample:

var kmlUrl = 'http://www.ci.redding.ca.us/KML/SacRiverTrail.kml';

kml = new esri.layers.KMLLayer(kmlUrl);

Property Details

<String> attributionDataUrl

The URL, when available, where the layer's attribution data is stored. (Added at v3.1)

<String> className

class attribute of the layer's node. (Added at v3.7)

<Credential> credential

Provides credential information for the layer such as userid and token if the layer represents a resource that is secured with token-based authentication. This value is available after the layer has been loaded i.e. layer.loaded is true. (Added at v2.5)

<Object[]> featureInfos

An array of objects that describe top-level KML features ids and their types. Objects in the array have the following properties:


  {

    "type":<Number>,

    "id": <Number>

  }

The type can be one of the following:Folder, GroundOverlay, Line, NetworkLink,Point, Polygon, ScreenOverlay.

<KMLFolder[]> folders

An array of KMLFolder objects that describe the folders and nested folders defined in the KML file. Use the parentFolderId and subFolderIds to identify the hierarchical relationship between folders.

<Boolean> hasAttributionData

When true the layer has attribution data. The default value is false. Use the Layer.getAttributionData method to retrieve this data as JSON. (Added at v3.1)

<String> id

ID assigned to the layer. If not assigned, esri.Map assigns value. By default, the ID of the layer is "layer" followed by a number. The ID can be user defined only in the layer constructor.
Sample:
  • Setting the layer ID in the layer constructor.
    population = new esri.layers.ArcGISDynamicMapServiceLayer("http://myserver/arcgis/rest/population/MapServer/Layers", {id:"population"});
    
  • Setting the layer ID after a layer is initialized.
    population.id = "population";
  • Retrieving the layer ID.
    function getMapLayers() {
      for (var j=0, jl=myMap.layerIds.length; j<jl; j++) {
        var currentLayer = myMap.getLayer(myMap.layerIds[j]);
        alert("id: " + currentLayer.id);
      }
    }
    

<Object> linkInfo

A link info object with properties that describe the network link. The object has the following properties:

{

  "id": <Number>,

  "name": <String>,

  "description": <String>,

  "snippet": <String>,

  "visibility": <Number>

  "refreshMode": <String>,

  "refreshInterval": <Number>,

  "viewRefreshMode": <String>,

  "viewRefreshTime": <Number>,

  "viewBoundScale": <Number>,

  "viewFormat": <String>,

  "httpQuery": <String>

}

(Added at v2.5)

<Error> loadError

Set if the layer failed to load. (Added at v3.9)

<Boolean> loaded

When the layer is loaded, the value becomes "true", and layer properties can be accessed. The onLoad event is also fired.
Known values: true | false

<Number> maxScale

Maximum visible scale for the layer. If the map is zoomed in beyond this scale, the layer will not be visible. A value of 0 means the layer does not have a maximum scale. The default value is 0. (Added at v3.1)

<Number> minScale

Minimum visible scale for the layer. If the map is zoomed out beyond this scale, the layer will not be visible. A value of 0 means the layer does not have a visible scale. The default value is 0. (Added at v3.1)

<Number> opacity

Opacity or transparency of layer. Values range from 0.0 to 1.0, where 0.0 is 100% transparent and 1.0 has no transparency.
Known values: 0.0 - 1.0
Default value: 1.0

<Number> refreshInterval

Refresh interval of the layer in minutes. Non-zero value indicates automatic layer refresh at the specified interval. Value of 0 indicates auto refresh is not enabled. (Added at v3.7)

<Boolean> showAttribution

When true the layer's attribution is displayed on the map. The default value is true. (Added at v3.1)

<Boolean> suspended

When true the layer is suspended. A layer is considered to be suspended when one of the following is true:
  • The layer is hidden.
  • The layer is not visible at the current map scale.
  • The layer is explicitly suspended by calling the Layer.suspend method.
(Added at v3.1)

<String> url

URL to the ArcGIS Server REST resource that represents a map service. To obtain the URL, use Services Directory. An example URL for the Street Map service on ArcGIS Online is:
http://server.arcgisonline.com/ArcGIS/rest/services/ESRI_StreetMap_World_2D/MapServer. (Added at v1.2)

<Boolean> visible

Visibility of the layer.
Known values: true | false
Default value: true

<Boolean> visibleAtMapScale

When true, the layer is visible at the current map scale. (Added at v3.1)
Method Details

attr(name,value)

Adds a new attribute or changes the value of an existing attribute on the layer's node. Removes the attribute if the value is null or undefined. (Added at v3.7)
Return value: Layer
Arguments:
<String> name Required The name of the attribute.
<String> value Required The value of the attribute. Set this value as null to remove the attribute.

getAttributionData()

Asynchrously returns custom data for the layer when available. (Added at v3.1)
Return value: Deferred

getFeature(featureInfo)

Get the KML feature identified by the input feature info. The table below lists the type of objects returned for the KML feature types.

KML <Feature>Class name of returned object
PlacemarkGraphic
GroundOverlayKMLGroundOverlay
ScreenOverlayKMLScreenOverlay (Not Implemented)
NetworkLinkKMLLayer
FolderKMLFolder
Return value: Object
Arguments:
<Object> featureInfo Required Feature info for the kml feature.
Sample:
var featureInfos = kmlLayer.folders[0].featureInfos;
dojo.forEach(featureInfos,function(info){
  var feature = kmlLayer.getFeature(info);
  //do something with the feature here...
});

getLayers()

Get an array of map layers that were created to draw placemarks, ground and screen overlays. The returned array can have instances of the following layer types: FeatureLayer, MapImageLayer or KMLLayer . This method can be used to override the renderer for feature layers.
Return value: Layer[]
Sample:

var layers = kmlLayer.getLayers();

dojo.forEach(layers, function(layer){

  if(layer.declaredClass === "esri.layers.FeatureLayer"){

  }

  else if(layer.declaredClass === "esri.layers.MapImageLayer"){

   

  }

});

getMap()

Returns reference to the map control the layer is added to. Returns null or undefined if it is not added to a map. (Added at v3.7)
Return value: Map

getNode()

Returns the layer's DOM node. (Added at v3.7)
Return value: HTMLElement

hide()

Sets the visibility of the layer to "false". The layer is not removed, but it is hidden from view.

isVisibleAtScale(scale)

Returns true if the layer is visible at the given scale. (Added at v3.1)
Return value: Boolean
Arguments:
<Number> scale Required The scale at which to check if the layer is visible.

resume()

Resumes layer drawing. (Added at v3.1)

setFolderVisibility(folder,isVisible)

Set the visibility for the specified folder.
Arguments:
<KMLFolder> folder Required A KML folder.
<Boolean> isVisible Required The visibility of the folder and all kml features within the folder.
Sample:
var folders = kmlLayer.folders;
dojo.forEach(folders,function(folder){
  kmlLayer.setFolderVisibility(folder,true);
});

setMaxScale(scale)

Set the maximum scale for the layer. (Added at v3.1)
Arguments:
<Number> scale Required The maximum scale at which the layer is visible.

setMinScale(scale)

Set the minimum scale for the layer. (Added at v3.1)
Arguments:
<Number> scale Required The minimum scale at which the layer is visible.

setOpacity(opacity)

Sets the opacity of the layer. Values range from 0.0 to 1.0, where 0.0 is 100% transparent and 1.0 has no transparency.
Arguments:
<Number> opacity Required Value from 0 to 1, where 0 is 100% transparent and 1 has no transparency. The default value is 1.
Sample:

layer.setOpacity(0.5);

setRefreshInterval(interval)

Changes the layer's refresh interval to the given value (in minutes). Non-zero value sets up automatic layer refresh at the specified interval. Value of 0 stops auto refresh. (Added at v3.7)
Return value: Layer
Arguments:
<Number> interval Required Refresh interval of the layer in minutes. Non-zero value indicates automatic layer refresh at the specified interval. Value of 0 indicates auto refresh is not enabled.

setScaleRange(minScale,maxScale)

Set the scale range for the layer. If minScale and maxScale are set to 0 then the layer will be visible at all scales. (Added at v3.1)
Arguments:
<Number> minScale Required The minimum scale at which the layer is visible.
<Number> maxScale Required The maximum scale at which the layer is visible.

setVisibility(isVisible)

Sets the visibility of the layer. When true, the layer is visible.
Arguments:
<Boolean> isVisible Required Set the visibility of the layer.

show()

Sets the visibility of the layer to "true".

suspend()

Suspends layer drawing. (Added at v3.1)
Event Details
[ On Style Events | Connect Style Event ]

error

Fires when there is a problem retrieving a layer. Should be used in favor of onError (Added at v3.5)
Properties:
<Error> error ArcGIS Server error message returned in a JavaScript error object.

load

Fires after layer properties for the layer are successfully populated. This event must be successful before the layer can be added to the map. Should be used in favor of onLoad (Added at v3.5)
Properties:
<Layer> layer The loaded layer.

opacity-change

Fires when the layer opacity has been changed, and returns an object with the opacity value. Should be used in favor of onOpacityChange (Added at v3.5)
Properties:
<Number> opacity Fires when the layer opacity (transparency) changes. A number property named opacity that indicates the new opacity. Values range from 0.0 to 1.0, where 0.0 is 100% transparent and 1.0 has no transparency.

refresh

Fired after the layer is refreshed. If the layer represents a network link it is automatically refreshed based on the network link parameters defined in the linkInfo property. Layers can be explicitly refreshed by calling the refresh method. (Added at v3.6)

refresh-interval-change

This event is fired when the layer's refreshInterval is modified. (Added at v3.7)

resume

Fires when a layer resumes drawing. Should be used in favor of onResume (Added at v3.5)

scale-range-change

Fires when a layer's minScale and/or maxScale is changed. Should be used in favor of onScaleRangeChange (Added at v3.5)

scale-visibility-change

Fires when a layer's scale visibility changes. The scale visibility changes when a layer is initially visible and becomes invisible because the map scale does not intersect the layer's scale range or vice versa. Should be used in favor of onScaleVisibilityChange (Added at v3.5)

suspend

Fires when a layer suspends drawing. Should be used in favor of onSuspend (Added at v3.5)

update

Fires any time a layer has finished loading or updating itself. For an ArcGISTiledMapServiceLayer, the event fires when all required tiles have been retrieved and displayed. For an ArcGISDynamicMapServiceLayer or ArcGISImageServiceLayer, the event fires when the map image has been retrieved and displayed. For a GraphicsLayer, the event fires when all graphics have been redrawn after a pan or zoom. Deprecated at v2.0. Use onUpdateStart and onUpdateEnd instead. (Added at v3.6)

update-end

Fires when a layer has finished updating its content. It is the responsibility of the subclass to determine when this event is fired. Should be used in favor of onUpdateEnd (Added at v3.5)
Properties:
<Error> error Optional argument. The error object is available when an error occurs during the update.

update-start

Fires when a layer begins to update its content. It is the responsibility of the subclass to determine when this event is fired. Should be used in favor of onUpdateStart (Added at v3.5)

visibility-change

Fires when the layer visibility has been changed, and returns an object with a Boolean visible property containing the new visibility value of the layer. Should be used in favor of onVisibilityChange (Added at v3.5)
Properties:
<Boolean> visible Fires when the layer visibility changes. A boolean property named visible indicates whether or not the layer is visible after visibility changed.