Search code examples
google-mapsgoogle-maps-api-3kml

How to download a KML file from Google Maps API via "mid" param?


I am doing an application where I want to download a KML file of a Google Maps referenced by a "mid" parameter, like this: https://www.google.com/maps/d/u/0/viewer?mid=1Mdlz2iXu_iZCPhUhzKvT2B2j-6E

I can download it manually clicking in the "three dots button" > "Download KML file"but I want to automatize this process.

Is there anyway to do it?


Solution

  • Put http://www.google.com/maps/d/kml?forcekml=1&mid= before the unique identifier:

    http://www.google.com/maps/d/kml?forcekml=1&mid=1Mdlz2iXu_iZCPhUhzKvT2B2j-6E

    (Related question: Render a My Maps using Google Maps JavaScript API)

    code snippet displaying that KML on a map:

    function initMap() {
      var map = new google.maps.Map(document.getElementById('map'), {
        zoom: 11,
        center: {
          lat: 41.876,
          lng: -87.624
        }
      });
    
      var ctaLayer = new google.maps.KmlLayer({
        url: 'http://www.google.com/maps/d/kml?forcekml=1&mid=1Mdlz2iXu_iZCPhUhzKvT2B2j-6E',
        map: map
      });
    }
    /* Always set the map height explicitly to define the size of the div
     * element that contains the map. */
    
    #map {
      height: 100%;
    }
    
    
    /* Optional: Makes the sample page fill the window. */
    
    html,
    body {
      height: 100%;
      margin: 0;
      padding: 0;
    }
    <div id="map"></div>
    <!-- Replace the value of the key parameter with your own API key. -->
    <script async defer src="https://maps.googleapis.com/maps/api/js?key=AIzaSyCkUOdZ5y7hMm0yrcCQoCvLwzdM6M8s5qk&callback=initMap">
    </script>