[GIS] Generate KML file/url from Google Maps

cartographygoogle-maps-apijavascriptkmlkmz

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.

Best Answer

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