I am trying to see if it is possible to upload a KML layer to display on the map. I have looked into using KMLLayer, but that requires a public URL to load in, and I can't use the file path of the file for security reasons (and because modern browsers change it to "c:\fakepath\file.kml" anyways for that very reason).

I tried to see if I can do client side conversion from KML to GeoJSON with gtran-kml and from GeoJSON to ArcGIS Geometry with arcgis-to-geojson-utils, but whenever I try to load the modules with require after installing them with npm, I keep on getting a module defined error in the console. I saw that it was trying to pull the module from the ArcGIS API. So I tried looking through the require.config file to see if it was set to the API, and it doesn't appear so. Using ./gtran-kml instead of gtran-kml went to the root URL, but still gave the same error being thrown by ArcGIS's init.js file. I don't know if this is an issue with npm, with not installing in the correct directory, with not calling modules correctly (I used var a = require('gtran-kml') then a.fnName to use it), or all three.

I'm still relatively new to working with JavaScript. So if anymore information is needed, please tell me.


The KMLLayer class is really expecting that the KML file be sitting on an HTTP-accessible web server or file hosting service somewhere.

One fairly easy option is to log into ArcGIS Online, and create a hosted feature layer from the KML file. That gives you a REST endpoint, which you can then use as the url property of the KMLLayer.

For example: http://www.arcgis.com/home/item.html?id=cf2f3a0f46c144dea3f88c8a4c3fe94c https://www.arcgis.com/sharing/rest/content/items/cf2f3a0f46c144dea3f88c8a4c3fe94c/data

Another option is to place the file on a web server somewhere, also making it HTTP-accessible.

For example: https://jimbarry.github.io/junkdrawer/farmers_markets.kml

Beyond that, if you really need that file to stay on your local file system and appear as if it is an HTTP_accessible web resource, perhaps look into FileReader:

Here: https://developer.mozilla.org/en-US/docs/Web/API/FileReader

Pretty sure how that would work is that FileReader.readAsDataURL() would then put a URL into the FileReader.result property. You'd then use that URL as the source for the KMLLayer. (that bit i'm not 100% sure on though)

For more info: https://developers.arcgis.com/javascript/3/jssamples/layers_kml.html

  • I had looked into using FileReader. The problem was it would encode the data as a giant URL string. So that string contained the actual data, and did not create a link to the file. I figured the work around would be to upload it to a public file sharing service, but I wasn't sure if there was a way in the API to be able to upload the KML without having to do this. – Pyro Apr 8 at 17:32
  • Another option for keeping the .kml file on your local file system is to use a localhost web server. (Mac comes with Apache, Windows comes with IIS). So then, any files you want accessible to a locally running web app, just put it somewhere in the web server's root, and bobsyouruncle. If you want your end-user to be able to choose a file at runtime, use a file dialog, but either way, the file will need to be under the web server root, or in a virtual directory already setup with the web server. But in the end, yes, the KMLLayer is expecting, at a minimum, that the file be HTTP-accessible. – JimBarry Apr 9 at 15:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.