2

I created kml file from ArcMap and imported into my Google maps, it works. Not sure if you can open this URL: https://www.google.com/maps/d/edit?mid=13TuHmGuW9nnE-8TACTfGLdDDf6o&ll=38.90133009121122%2C-77.01476049999996&z=12

Now I want to display my map using Java API and I used this code https://developers.google.com/maps/documentation/javascript/kml tweaked it a little bit, inserted my API_Key at the end of the script but the problem is

<script>
      var map;
      var src = 'https://myGoogleMapURL

should be with the .kml extension if I am not mistaken. My question is, how do I generate kml file/url from my map URL mentioned above. I tried adding &output=kml at the end of the Google map URL as some people suggested but it does not work.

1
  • Welcome to GIS SE! As a new user please take the tour to learn about our focused Q&A format.
    – Midavalo
    Commented Mar 27, 2017 at 20:23

3 Answers 3

4

You can download the data directly from MyMaps with this syntax

https://www.google.com/maps/d/u/0/kml?mid={MAP_ID}&forcekml=1

The MAP_ID is the ID in the URL when you've got the MyMap open,

eg. your map

https://www.google.com/maps/d/edit?mid=13TuHmGuW9nnE-8TACTfGLdDDf6o&ll=38.90133009121122%2C-77.01476049999996&z=12

will be...

https://www.google.com/maps/d/u/0/kml?mid=13TuHmGuW9nnE-8TACTfGLdDDf6o&forcekml=1

or adding to your Google Maps Javascript API. But there is a limit of 2mb data size for this.

    let my_url = 'https://www.google.com/maps/d/u/0/kml?mid={MAP_ID}&forcekml=1'

    let kmlLayer = new google.maps.KmlLayer(my_url, {
      suppressInfoWindows: true,
      preserveViewport: false,
      map: map
    });

    kmlLayer.addListener('click', function(event) {
      var content = event.featureData.infoWindowHtml;
      var testimonial = document.getElementById('capture');
      testimonial.innerHTML = content;
    });

Take note of your sharing settings. This does expose your data publicly (If they have the link or even the ID) but that's whats needed to download the data directly

Access Control for MyMaps

6
  • Can this same URL syntax work for a specific layer of the map or only for the whole thing? Commented Apr 7, 2022 at 15:20
  • You cant really break up a KML the same way you can with geojson, so I doubt it would be possible Commented Apr 8, 2022 at 18:17
  • I'm looking for a way to retrieve KML from user map URLs to aggregate hurricane rescue maps. Downloading the KML form a URL of this form works in my browser but not from my script. What else does the request need to be allowed? Commented Oct 5 at 1:43
  • KML is notoriously difficult to parse and is why Google Maps on browser is so limited with the import. I've tried parsing KML without the browser and have never felt the results were ever good enough to move forward with KML. I have since always tried to move to geojson anywhere I can Commented Oct 7 at 13:29
  • I was able to get downloading Google user map KML to work by using Mechanize and giving it the cookies from my browser. I didn't try to figure out which cookies do & don't matter. Commented Oct 9 at 13:33
1

If I am not mistaken this line is supposed to be a link to where you KML file is hosted. The link that you posted at the beginning will not work with the google maps api. You need to be able to point to the raw file in which you are hosting.

You will need to find a place on the cloud for you to store your kml file or simply point to the directory on your computer.

Here is a link with some instructions on how to go about doing this using your google drive

3
  • As stated in the begging of my initial post, KML file is already hosted/imported into Google Maps (under my account), simply create new Map and there is an option to import KML file. It is there (first link provided) but for API to work I need that URL with KML extension. AvidDabbler, I`ll check the instructions that you provided as well. Tanks
    – Klika
    Commented Mar 27, 2017 at 18:43
  • In order to use a KML file in Google Maps API the file has to be hosted somewhere else other than My Maps. The example url that they show on the API (developers.google.com/maps/documentation/javascript/examples/…) is pointing to a raw KML file and not a My Maps link. If you were to actually go to that URL it would take you to a download of a KML file not a Google My Map Commented Mar 27, 2017 at 19:44
  • That`s weird, for my assignment my instructor gave us a couple of options where to store our KML file, one is Google maps and the other is ArcMap online (this one requires different approach). Someone is wrong here ........
    – Klika
    Commented Mar 27, 2017 at 19:56
1

I found out that you don't need to download and upload it. If you are using MyMaps and download as the KML there is already a link present within the file - open it up in a text editor (or simply click and drag the download into your browser and there should be a line . You can copy the link inside the square brakets and there you are.

I hope this helps.

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