-
-
Notifications
You must be signed in to change notification settings - Fork 41
Expand file tree
/
Copy pathtooltip.js
More file actions
176 lines (140 loc) · 5.48 KB
/
tooltip.js
File metadata and controls
176 lines (140 loc) · 5.48 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
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
const URL = 'https://your-url.fr/index.php/lizmap/service/?repository=your-repo&project=your-project&SERVICE=WFS&REQUEST=GetFeature&VERSION=1.0.0&TYPENAME=your-layer-name&OUTPUTFORMAT=GeoJSON';
// You can add tooltip for multiple layer
const layersURL = [URL];
// You also need to provide the layer name
const layersNames = ['Name of your layer'];
// Function to retrieve data
async function getDataTooltip(url) {
try {
const response = await fetch(url);
if (!response.ok) {
throw new Error(`HTTP Error! Statut : ${response.status}`);
}
const data = await response.json();
return data;
} catch (error) {
console.error('An error occurred while fetching the data.', error);
throw error;
}
};
function createPoint(lon, lat) {
var lonLat = new OpenLayers.LonLat(lon, lat).transform(
new OpenLayers.Projection("EPSG:4326"),
new OpenLayers.Projection("EPSG:3857")
);
var pointGeom = new OpenLayers.Geometry.Point(lonLat.lon, lonLat.lat);
const feature = new OpenLayers.Feature.Vector(pointGeom);
return feature;
}
// Creation of the tooltip
function createSelectControl(vectorLayer, popupName) {
var map = lizMap.map;
// Create Control
var selectControl = new OpenLayers.Control.SelectFeature(vectorLayer, {
hover: true,
onSelect: function(feature) {
// Get data from entity
var info = "";
for (var key in feature.attributes) {
info += "<strong style='font-weight:bold;'>" + key + ":</strong> " + feature.attributes[key] + "<br/>";
}
// Show the data from the tooltip
feature.popup = new OpenLayers.Popup("popup_" + popupName,
feature.geometry.getBounds().getCenterLonLat(),
new OpenLayers.Size(180, 180),
info,
false
);
feature.popup.contentDiv.style.fontSize = "9px";
feature.popup.contentDiv.style.color = "black";
feature.popup.contentDiv.style.border = "1px solid black";
feature.popup.contentDiv.style.padding = "5px";
map.addPopup(feature.popup);
},
onUnselect: function(feature) {
// Delete tooltip
if (feature.popup) {
map.removePopup(feature.popup);
feature.popup.destroy();
delete feature.popup;
}
}
});
return selectControl;
}
function addCheckboxClickListener(layerName) {
// add an Event Listener when the layer is checked
$('#layer-' + layerName + ' button.btn.checkbox').click(function() {
var isChecked = $(this).hasClass('checked');
toggleSelectControl(layerName, isChecked);
});
var className = $('#layer-' + layerName)[0].className;
// Add the same behaviour if the button of the group is checked
if (className.includes('child-of-group')){
var idGroup = className.match(/child-of-(\S+)\s/)[1];
$('#' + idGroup + ' button.btn.checkbox').click(function() {
var isChecked = $(this).hasClass('checked');
toggleSelectControl(layerName, isChecked);
});
}
}
function toggleSelectControl(layerName, isEnabled) {
var selectControl = getSelectControl(layerName + '_tooltip');
if (selectControl) {
if (isEnabled) {
selectControl.activate();
} else {
selectControl.deactivate();
}
}
}
function getSelectControl(layerName) {
var map = lizMap.map;
var layers = map.getLayersByName(layerName);
if (layers.length > 0) {
var layer = layers[0];
var controls = layer.map.controls;
for (var control of controls) {
if ((control.CLASS_NAME === "OpenLayers.Control.SelectFeature") && (control.layer.name === layerName)) {
return control;
}
}
}
return null;
}
lizMap.events.on({
'uicreated' : function(_) {
const map = lizMap.map;
// The layer is invisible. You can change the pointRadius of the layer
const iconStyle = new OpenLayers.Style({
pointRadius: 7,
fillColor: '#ff0000',
fillOpacity: 0,
strokeWidth: 0,
strokeOpacity:0
});
$.each(layersURL, function(index, url){
const promise = getDataTooltip(url);
promise.then(data =>{
// Creation of the tooltip layer
const vectorLayer = new OpenLayers.Layer.Vector(layersNames[index] + '_tooltip', {
styleMap: iconStyle
});
$.each(data.features, function(_, featureData){
lon = featureData.geometry.coordinates[0];
lat = featureData.geometry.coordinates[1];
feature = createPoint(lon, lat, 'EPSG:4326');
feature.attributes = featureData.properties;
vectorLayer.addFeatures(feature);
})
// Create the control for the layer
var selectControl = createSelectControl(vectorLayer, layersNames[index] + '_tooltip');
map.addLayer(vectorLayer);
map.addControl(selectControl);
selectControl.deactivate();
})
// Add an Event Listener when the original layer is visible on the map
addCheckboxClickListener(layersNames[index]);
})
}
});