-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMapLibre-render-KML-source-with-popup.html
More file actions
88 lines (73 loc) · 2.59 KB
/
MapLibre-render-KML-source-with-popup.html
File metadata and controls
88 lines (73 loc) · 2.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
<!doctype html>
<html class="no-js" lang="">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>KML/KMZ Viewer</title>
<style>
#map-container {
width: 100%;
height: 600px; /* Adjust the height as needed */
}
</style>
<link rel='stylesheet' href='https://unpkg.com/maplibre-gl@2.0.0/dist/maplibre-gl.css' />
<script src='https://unpkg.com/maplibre-gl@2.0.0/dist/maplibre-gl.js'></script>
<script src="https://unpkg.com/@tmcw/togeojson"></script>
</head>
<body>
<div id="map-container"></div>
<script>
var map = new maplibregl.Map({
container: 'map-container',
center: [-71.698, 42.041],
zoom: 7.5,
style: "https://basemaps.cartocdn.com/gl/voyager-gl-style/style.json",
maplibreLogo: true,
attributionControl: true
});
var popup = new maplibregl.Popup();
map.on('load', function () {
fetch('https://s3.amazonaws.com/geospatial.data/usa/ma/RTA_Boundaries.kml')
.then(response => response.text())
.then(kml => {
var geojson = toGeoJSON.kml(new DOMParser().parseFromString(kml, 'text/xml'));
console.log("JSON data attribute name:", geojson.name); // Log the JSON data attribute name
console.log("Feature Collection data:", geojson); // Log the Feature Collection data
map.addSource('kml-source', {
type: 'geojson',
data: geojson,
generateId: true
});
map.addLayer({
id: 'kml-layer',
type: 'fill',
source: 'kml-source',
paint: {
'fill-color': 'red',
'fill-opacity': 0.8
}
});
map.on('mouseenter', 'kml-layer', function (e) {
var properties = e.features[0].properties;
var popupContent = "<div>";
for (var prop in properties) {
popupContent += "<p><strong>" + prop + ":</strong> " + properties[prop] + "</p>";
}
popupContent += "</div>";
popup.setLngLat(e.lngLat)
.setHTML(popupContent)
.addTo(map);
});
map.on('mousemove', 'kml-layer', function (e) {
var featureId = e.features[0].id;
map.setPaintProperty('kml-layer', 'fill-color', ['match', ['id'], featureId, 'blue', 'red']);
});
map.on('mouseleave', 'kml-layer', function () {
map.setPaintProperty('kml-layer', 'fill-color', 'red');
popup.remove();
});
});
});
</script>
</body>
</html>