I’m trying to load a simple KML layer into my leaflet map, I’m using the
Leaflet KML layer plugin with vue.js
for some reason, this doesn’t work and return invalid bounds from the L.KML.js script
just mention that if I’m running this code as a plain HTML file like in the example all works well, what can go wrong by using this plugin with vue.js?
code snippets for the vue mounted and the HTML example
mounted() {
this.setupMap();
},
methods: {
setupMap() {
const map = new L.Map("map", {
center: new L.LatLng(58.4, 43.0),
zoom: 11,
});
const osm = new L.TileLayer(
"http://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png"
);
map.addLayer(osm);
// Load kml file
fetch("@/assets/kml.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);
});
}
<html>
<head>
<link rel="stylesheet" href="http://unpkg.com/[email protected]/dist/leaflet.css" />
<script src="http://unpkg.com/[email protected]/dist/leaflet.js"></script>
<script src="./L.KML.js"></script>
</head>
<body>
<div style="width: 100vw; height: 100vh" id="map"></div>
<script type="text/javascript">
// Make basemap
const map = new L.Map('map', { center: new L.LatLng(58.4, 43.0), zoom: 11 });
const osm = new L.TileLayer('http://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png');
map.addLayer(osm);
// Load kml file
fetch('assets/example1.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>
</html>