Skip to content

KML layer doesn't show #41

@endbek

Description

@endbek

Hello,

I am new to leaflet, and trying to use the code below to add an online kml file to leaflet. The background map shows, but the kml layer is not added. I am new to leaflet, please help:

<script src="http://unpkg.com/leaflet@1.4.0/dist/leaflet.js"></script> <script src="./L.KML.js"></script>
<script type="text/javascript"> // Make basemap const map = new L.Map('map', { center: new L.LatLng(10, 0.0), zoom: 3}); const osm = new L.TileLayer('http://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png');
        map.addLayer(osm);

        // Load kml file
        fetch('https://www.cpc.ncep.noaa.gov/products/international/gefs/gefs_week1_af_precip_obs.kml')
            .then(res => res.text())
            .then(kmltext => {
                // Create new kml overlay
                const parser = new DOMParser();
                const kml = parser.parseFromString(kmltext, 'text/xml');
                const track = new L.KML(kml);
                map.addLayer(track);

                // Adjust map to show the kml
                const bounds = track.getBounds();
                map.fitBounds(bounds);
            });
    </script>
</body>

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions