Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create a tooltip with JS when your tooltip on lizmap doesn't work #77

Open
wants to merge 49 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
49 commits
Select commit Hold shift + click to select a range
ac7f1e9
Create README.md
altheaFeu Mar 7, 2024
9552a40
Add files via upload
altheaFeu Mar 7, 2024
a39a598
Update README.md
altheaFeu Mar 7, 2024
754389d
Update tour.css
altheaFeu Mar 7, 2024
eed7472
Update tour.js
altheaFeu Mar 7, 2024
738857d
Update tour.js
altheaFeu Mar 8, 2024
765fec9
Update tour.css
altheaFeu Mar 8, 2024
b089815
Update README.md
altheaFeu Mar 8, 2024
29c651d
Create README.md
altheaFeu Mar 13, 2024
3308d58
Add transparency script
altheaFeu Mar 13, 2024
7883fd3
Update README.md
altheaFeu Mar 13, 2024
a108c68
Update README.md
altheaFeu Mar 13, 2024
d031ea4
Delete library/ui/change_opacity directory
altheaFeu Mar 14, 2024
147525b
Update README.md
altheaFeu Mar 14, 2024
f419db3
Create data_table.js
altheaFeu Mar 19, 2024
05f5081
Create README.md
altheaFeu Mar 19, 2024
770af45
Update README.md
altheaFeu Mar 19, 2024
be93fe9
Update README.md
altheaFeu Mar 19, 2024
5d6211e
Add demo.gif
altheaFeu Mar 19, 2024
6f52b17
Update README.md
altheaFeu Mar 19, 2024
ea50903
Update README.md
altheaFeu Mar 19, 2024
95772ec
Update tour.js
altheaFeu Mar 19, 2024
d923ed4
Add demo.png
altheaFeu Mar 19, 2024
987e255
Update README.md
altheaFeu Mar 19, 2024
fd4b414
Create README.md
altheaFeu Mar 25, 2024
7a60894
Create table-dock.js
altheaFeu Mar 25, 2024
c1fb2d8
Create table-dock.css
altheaFeu Mar 25, 2024
808cbde
Create table-XY.js
altheaFeu Mar 25, 2024
bb6002f
Update table-XY.js
altheaFeu Mar 25, 2024
da49258
Update table-dock.js
altheaFeu Mar 25, 2024
5febfd5
Create table-style.js
altheaFeu Mar 25, 2024
77ba2e9
Update README.md
altheaFeu Mar 25, 2024
0ea982c
Update README.md
altheaFeu Mar 26, 2024
8e143d7
Update README.md
altheaFeu Mar 26, 2024
4ec702b
Update table-XY.js
altheaFeu Mar 26, 2024
8d1aa07
Create workerXY.js
altheaFeu Mar 26, 2024
3ddfc45
Update table-dock.js
altheaFeu Mar 26, 2024
bff19d0
Update table-XY.js
altheaFeu Mar 26, 2024
6fa4510
Update README.md
altheaFeu Mar 27, 2024
b1ca250
Delete library/ui/driver_tutorial directory
altheaFeu Mar 27, 2024
08a08ad
Delete library/ui/data_table directory
altheaFeu Mar 27, 2024
4b829c4
Delete library/ui/map-excel directory
altheaFeu Mar 27, 2024
6ad38e5
Update README.md
altheaFeu Mar 27, 2024
ff98f51
Create README.MD
altheaFeu Mar 27, 2024
3c4a753
Image for the readme.md
altheaFeu Mar 27, 2024
124e2c6
Update README.MD
altheaFeu Mar 27, 2024
b10963d
Update README.MD
altheaFeu Mar 27, 2024
3259893
Update README.MD
altheaFeu Mar 28, 2024
aefd291
Create tooltip.js
altheaFeu Mar 28, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ If you are developing a new feature, do not hesitate to let us know so that we c
* [Remove button](./library/ui/remove_button) to make the UI lighter and easier
* [Resize Dock with splitter](./library/ui/resize_dock_with_splitter)
* [Resize Dock with splitter 2 (interact.js)](./library/ui/resize_dock_2)
* [A tooltip if your tooltip doesn't work (tooltip.js)](./library/ui/tooltip)
* [Simplify export options](./library/ui/simplify-export-options)
* [Export attribute table to CSV](./library/ui/export-attributetable-csv)
* [Add button to toggle dock full width](./library/ui/add_dock_resize_button)
Expand Down
7 changes: 7 additions & 0 deletions library/ui/tooltip/README.MD
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Create a tooltip if your lizmap tooltip doesn't work
![tooltip](./tooltip.png)

LizMap offers you the possibility to create your own tooltips within its QGIS extension. However, if you encounter bugs, issues with this tool, or if you have an older version of Lizmap, you can use this js script instead.
It will create a new invisible layer for the user from a point layer, activating its tooltip when the user hovers over any of the features in the layer.

This tooltip is based on [OpenLayers 2.13.1](https://openlayers.org/two/)
176 changes: 176 additions & 0 deletions library/ui/tooltip/tooltip.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,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]);
})
}
});
Binary file added library/ui/tooltip/tooltip.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.