leaflet-kmz
A KMZ file loader for Leaflet Maps
For a working example see one of the following demos:
How to use
- include CSS & JavaScript
<head> ... <style> html, body, #map { height: 100%; width: 100%; padding: 0; margin: 0; } </style> <!-- Leaflet (JS/CSS) --> <link rel="stylesheet" href="https://unpkg.com/leaflet@1.9.4/dist/leaflet.css"> <script src="https://unpkg.com/leaflet@1.9.4/dist/leaflet.js"></script> <!-- Leaflet-KMZ --> <script src="https://unpkg.com/leaflet-kmz@1.1.2/dist/leaflet-kmz.js"></script> ... </head> - choose a div container used for the slippy map
<body> ... <div id="map"></div> ... </body> - create your first simple “leaflet-kmz” slippy map
<script> { const map = new (L.LeafletMap || L.Map)('map', { preferCanvas: true // recommended when loading large layers. }); map.setView(new L.LatLng(43.5978, 12.7059), 5); const OpenTopoMap = new L.TileLayer('https://{s}.tile.opentopomap.org/{z}/{x}/{y}.png', { maxZoom: 17, attribution: 'Map data: © <a href="http://www.openstreetmap.org/copyright">OpenStreetMap</a>, <a href="http://viewfinderpanoramas.org">SRTM</a> | Map style: © <a href="https://opentopomap.org">OpenTopoMap</a> (<a href="https://creativecommons.org/licenses/by-sa/3.0/">CC-BY-SA</a>)', opacity: 0.90 }); OpenTopoMap.addTo(map); // Instantiate KMZ layer (async) const kmz = L.kmzLayer().addTo(map); kmz.on('load', function(e) { control.addOverlay(e.layer, e.name); // e.layer.addTo(map); }); // Add remote KMZ files as layers (NB if they are 3rd-party servers, they MUST have CORS enabled) kmz.load('https://raruto.github.io/leaflet-kmz/examples/regions.kmz'); kmz.load('https://raruto.github.io/leaflet-kmz/examples/capitals.kmz'); kmz.load('https://raruto.github.io/leaflet-kmz/examples/globe.kmz'); const control = new (L.LayersControl || L.Control.Layers)(null, null, { collapsed:false }).addTo(map); } </script>
Notes:
- supported file formats: .kmz, .kml
- KMZ extraction uses native
DecompressionStream('deflate-raw')(current Chrome, Edge, Firefox and Safari); no ZIP library or download is required. Plain KML does not require this API. - Supported archives: single-volume ZIP32 with stored or DEFLATE entries and UTF-8 filenames, including data descriptors. ZIP64, encrypted and multi-volume archives are rejected.
load(url)andparse(kmzArrayBuffer, props)return promises that reject on archive or decompression errors. Handle these rejections withawait/catch.- to be able to use Google files (eg. through Google My Maps) you MUST use a valid third-party kml proxy server
Contributors: A-Lurker, BrandonXiang, Raruto