-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
124 lines (85 loc) · 3.38 KB
/
app.js
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
// setmap with base map
var map = L.map("map", {
center: [18.802808, 98.950170],
zoom: 15
});
let osm = L.tileLayer('https://tile.openstreetmap.org/{z}/{x}/{y}.png', {
maxZoom: 19,
attribution: '© <a href="http://www.openstreetmap.org/copyright">OpenStreetMap</a>'
}).addTo(map);
// get cap get map//
// var wms_url = "http://localhost:8080/geoserver/wms?service=wms&version=1.1.1&request=GetCapabilities";
// axios.get(wms_url).then((res) => console.log(res.data));
let layer = L.tileLayer.wms("https://geoserver.nu.ac.th/geoserver/map_portal/wms?", {
layers: 'map_portal:cassava_area_64',
format: 'image/png',
legend: true,
transparent: true,
}).addTo(map)
let layer2 = L.tileLayer.wms("http://localhost:8080/geoserver/nurc/wms?", {
layers: 'nurc:Img_Sample',
format: 'image/png',
legend: true,
transparent: true,
})
var baseMaps = {
"แผนที&osm": osm
};
var overlayMaps = {
"เลเยอร์": layer,
"เลเยอร์2": layer2
};
map.on('overlayadd', onOverlayAdd);
function onOverlayAdd(e){
//do whatever
console.log(e.layer.options.layers);
layer_name = e.layer.options.layers
console.log(layer_name);
layerChange()
}
L.control.layers(baseMaps, overlayMaps).addTo(map);
// get graphiclagend
var layer_name = 'ne:countries'
let legend = L.control.attribution({ position: "bottomright" });
legend.onAdd = function (map) {
var div = L.DomUtil.create("div", "info legend");
div.innerHTML = ` <img src="http://localhost:8080/geoserver/wms?REQUEST=GetLegendGraphic&VERSION=1.0.0&FORMAT=image/png&WIDTH=20&HEIGHT=20&LAYER=${layer_name}" class="overlay" alt="" srcset="">
`
return div;
};
legend.addTo(map)
function layerChange(){
legend.onAdd = function (map) {
var div = L.DomUtil.create("div", "info legend");
div.innerHTML = ` <img src="https://geoserver.nu.ac.th/geoserver/map_portal/wms?REQUEST=GetLegendGraphic&VERSION=1.0.0&FORMAT=image/png&WIDTH=20&HEIGHT=20&LAYER=${layer_name}" class="overlay" alt="" srcset="">
`
return div;
};
legend.addTo(map)
}
map.addEventListener('click', onMapClick);
popup = new L.Popup({maxWidth: 1000000});
function onMapClick(e) {
var latlngStr = '(' + e.latlng.lat.toFixed(3) + ', ' + e.latlng.lng.toFixed(3) + ')';
var BBOX = map.getBounds()._southWest.lng+","+map.getBounds()._southWest.lat+","+map.getBounds()._northEast.lng+","
+map.getBounds()._northEast.lat;
var WIDTH= map.getSize().x;
var HEIGHT = map.getSize().y;
var X = map.layerPointToContainerPoint(e.layerPoint).x;
var Y = map.layerPointToContainerPoint(e.layerPoint).y;
let xx = X.toFixed(0)
let yy = Y.toFixed(0)
console.log(X,Y);
var url = `http://localhost:8080/geoserver/wms?request=GetFeatureInfo&service=WMS&version=1.1.1&layers=topp%3Astates&styles=&srs=EPSG%3A4326&format=image%2Fpng&bbox=-145.151041%2C21.73192%2C-57.154894%2C58.961059&width=${WIDTH}&height=${HEIGHT}&query_layers=topp%3Astates&info_format=application/json&feature_count=50&x=${xx}&y=${yy}&exceptions=application%2Fvnd.ogc.se_xml`
popup.setLatLng(e.latlng);
fetch(url)
.then((response) => response.json())
.then((data) => {
console.log(data.features)
let text = data.features
console.log(text[0].properties);
show_data = JSON.stringify
popup.setContent(`<div>state_name:${text[0].properties.STATE_NAME}</div><br><div> family:${text[0].properties.FAMILIES}</div>`);
});
map.openPopup(popup);
}