Hide Table of Contents
View KML with buffer sample in sandbox
KML with buffer


This sample demonstrates that the ArcGIS API for JavaScript can not only display KML files, but also gives you full access to the features from a KML file. First, add the KML to the map. Then once theKML layer loads, send the graphics to a geometry service to buffer them. Once the features have been buffered, send them to the PopulationSummary Geoprocessing service.This sample also shows how to rerun the buffer with a larger or smaller distance and rerun the population summary.

Note: The KML layer needs to be publicly accessible.


<!DOCTYPE html>
  <meta charset="utf-8">
  <meta name="viewport" content="initial-scale=1, maximum-scale=1,user-scalable=no">
  <title>KML with buffer</title>
  <link rel="stylesheet" href="https://js.arcgis.com/3.41/dijit/themes/tundra/tundra.css">
  <link rel="stylesheet" href="https://js.arcgis.com/3.41/esri/css/esri.css">
    body {
      height: 100%;
      width: 100%;
      margin: 0;
      padding: 0;

    #map {
      height: 100%;
      margin: 0;
      overflow: hidden;
      padding: 0;
      position: relative;

    #description {
      height: 160px;

    #info {
      color: #666;
      padding: 10px;
      font-family: sans-serif;
      width: 90%;
      margin: 0 auto;

    #info h3 {
      color: #E4A54F;
      margin: 0;
      padding: 0 0 5px 0;

    #info hr {
      border: 1px solid #666;

    #popWrapper {
      float: right;

    #totalPop {
      font-weight: bold;
      font-size: x-large;

    .ds {
      background: #000;
      overflow: hidden;
      position: absolute;
      z-index: 2;
      bottom: 0;

    #ds-h div {
      width: 100%;

    #ds .o1 {
      filter: alpha(opacity=10);
      opacity: .1;

    #ds .o2 {
      filter: alpha(opacity=8);
      opacity: .08;

    #ds .o3 {
      filter: alpha(opacity=6);
      opacity: .06;

    #ds .o4 {
      filter: alpha(opacity=4);
      opacity: .04;

    #ds .o5 {
      filter: alpha(opacity=2);
      opacity: .02;

    #ds .h1 {
      height: 1px;

    #ds .h2 {
      height: 2px;

    #ds .h3 {
      height: 3px;

    #ds .h4 {
      height: 4px;

    #ds .h5 {
      height: 5px;

  <script src="https://js.arcgis.com/3.41/"></script>
    var map, kml, kmlGeoms, bufferGraphics, gp;
      "esri/map", "esri/layers/KMLLayer", "esri/layers/GraphicsLayer",
      "esri/SpatialReference", "esri/graphic",
      "esri/tasks/GeometryService", "esri/tasks/Geoprocessor",
      "esri/tasks/BufferParameters", "esri/tasks/FeatureSet",
      "esri/symbols/SimpleFillSymbol", "esri/symbols/SimpleLineSymbol",
      "dojo/parser", "dojo/_base/array", "esri/Color", "dojo/_base/connect",
      "dojo/number", "dojo/dom", "dojo/dom-style", "dojo/dom-attr",

      "dijit/layout/BorderContainer", "dijit/layout/ContentPane",
    ], function(
      Map, KMLLayer, GraphicsLayer,
      SpatialReference, Graphic,
      GeometryService, Geoprocessor,
      BufferParameters, FeatureSet,
      SimpleFillSymbol, SimpleLineSymbol,
      parser, arrayUtils, Color, connect,
      number, dom, domStyle, domAttr
    ) {
      map = new Map("map", {
        basemap: "topo-vector",
        center: [-77.8, 39.3],
        zoom: 10


      // Graphics layer for the buffer
      bufferGraphics = new GraphicsLayer();

      var kmlUrl =
      kml = new KMLLayer(kmlUrl);

      // The geoprocessor
      var gpUrl =
      gp = new Geoprocessor(gpUrl);
        wkid: 102100

      kml.on("load", function(evt) {
        // Collect the line features from the KML layer
        kmlGeoms = arrayUtils.map(evt.layer.getLayers()[0].graphics,
          function(g) {
            return g.geometry;

        // Send to a function to do the buffer

      // Re-calculate the buffer and population when buffer distance changes
      connect.connect(dom.byId("bufferDistance"), "onchange", bufferKML);

      function bufferKML() {
        dom.byId("totalPop").innerHTML = "";
        domStyle.set("loading", "display", "inline-block");
        domAttr.set("bufferDistance", "disabled", true);

        var bufferDistance = dom.byId("bufferDistance").value;
        var params = new BufferParameters();
        params.geometries = kmlGeoms;
        params.distances = [bufferDistance];
        params.bufferSpatialReference = new SpatialReference({
          "wkid": 102100
        params.outSpatialReference = map.spatialReference;
        params.unit = GeometryService.UNIT_STATUTE_MILE;
        params.unionResults = true;
        //This service is for development and testing purposes only. We recommend that you create your own geometry service for use within your applications.
        var gsUrl =
        var gs = new GeometryService(gsUrl);
        gs.buffer(params, showBuffer);

      function showBuffer(buffer) {
        // Add the buffer graphic to the map
        var polySym = new SimpleFillSymbol()
          .setColor(new Color([56, 102, 164, 0.4]))
            new SimpleLineSymbol()
            .setColor(new Color([56, 102, 164, 0.8]))
        var bufferGraphic = new Graphic(buffer[0], polySym);

        // Send buffer to runction to calculate population

      function calcPop(aoi) {
        // Create a feature set for the population zonal stats GP service
        var fset = new FeatureSet();
        fset.features = [aoi];

        var params = {
          "inputPoly": fset
        gp.execute(params, handlePop);

      function handlePop(result) {
        if (result[0] && result[0].value.features[0].attributes.hasOwnProperty(
            'SUM')) {
          var pop = number.format((result[0].value.features[0].attributes.SUM)
            .toFixed() + '');
          dom.byId('totalPop').innerHTML = pop;
        } else {
          alert("Unable to get population summary. Please try again.");
        domStyle.set("loading", "display", "none");
        domAttr.set("bufferDistance", "disabled", false);

<body class="tundra">
  <div data-dojo-type="dijit/layout/BorderContainer" data-dojo-props="design:'headline',gutters:false"
    style="width: 100%; height: 100%; margin: 0;">
    <div id="description" data-dojo-type="dijit/layout/ContentPane" data-dojo-props="region:'top'">
      <div id="info">
        <h3>Buffer a KML Layer and then Send Result To a Geoprocessing Service</h3>
        KML layers in the ArcGIS API for JavaScript give you full access to the features
        in a KML file. For this app, a KML file representing most of the Chesapeake and Ohio Canal
        near Washington, DC is added to the map. The features from the KML layer
        are first buffered using a geometry service and then population within the buffer
        is calculated via a geoprocessing service.
        <hr />
        <div id="feedback">
          Buffer Distance (miles):
          <select id="bufferDistance">
          <option selected>1</option>
          <div id="popWrapper">
            <span id="loading"><img src="images/loading_orange.gif" /> </span>
            <span id="totalPop"></span> people within buffer.
    <div id="map" data-dojo-type="dijit/layout/ContentPane" data-dojo-props="region:'center'">
      <div id="ds">
        <div id="ds-h">
          <div class="ds h1 o1"></div>
          <div class="ds h2 o2"></div>
          <div class="ds h3 o3"></div>
          <div class="ds h4 o4"></div>
          <div class="ds h5 o5"></div>
Show Modal