-
-
Notifications
You must be signed in to change notification settings - Fork 42
/
Copy pathexternalLinksFromMapClick_3.8.js
184 lines (169 loc) · 6.24 KB
/
externalLinksFromMapClick_3.8.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
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
177
178
179
180
181
182
183
184
/**
* @license Mozilla Public License Version 2.0
* This script has been developed by the "community"
* There isn't any guarantee that this script will work on another version of Lizmap Web Client.
*/
const lizmapExternalLinks = function() {
// ID of the dock (do not change)
const DOCK_ID = 'external-links';
// Icon of the dock menu and used before each link
// See https://getbootstrap.com/2.3.2/base-css.html#icons
const DOCK_ICON = 'icon-map-marker';
// Dock position: can be dock, minidock, right-dock
const DOCK_POSITION = 'right-dock';
// Title of the dock
const DOCK_TITLE = 'External resources';
// Description displayed at the top of the dock content
const DOCK_DESCRIPTION = 'You can view the current map area in other tools.';
// Sentence used when no click has been made on the map yet
const DOCK_PLEASE_CLICK = 'Please click on the map to get the updated links.';
// Default Z value
const DEFAULT_Z_VALUE = 15;
// List of links to display, with a title and a URL
// You must replace
// the longitude by LONGITUDE and the latitude by LATITUDE (upper case)
// AND the z value by Z
const DOCK_LINKS = [
{
'title': 'OpenStreetMap',
'url': `https://www.openstreetmap.org/?mlat=LATITUDE&mlon=LONGITUDE#map=Z/LATITUDE/LONGITUDE`
},
{
'title': 'Waze',
'url': `https://www.waze.com/livemap/?zoom=Z&lat=LATITUDE&lon=LONGITUDE`
},
{
'title': 'Google',
'url': `https://maps.google.com/maps?ll=LATITUDE,LONGITUDE&q=LATITUDE,LONGITUDE&hl=fr&t=m&z=Z`
},
];
// ---------------------------
// DO NOT EDIT AFTER THIS LINE
// ---------------------------
/**
* Add the Lizmap dock with the links
* every time the user clicks on the map
*
* @param event Click event
*/
function buildHtml(longitude = null, latitude = null) {
let dockHtml = '';
dockHtml += `<p style="padding: 5px;">${DOCK_DESCRIPTION}</p>`;
if (longitude && latitude) {
dockHtml += `<p style="padding: 5px;"><b>Longitude</b> : ${longitude}<br><b>Latitude</b> : ${latitude}</p>`;
dockHtml += '<table class="table table-condensed table-hover table-striped table-bordered table-">';
dockHtml += '<tbody>';
// Add a line for each link to display
for (let i in DOCK_LINKS) {
const link = DOCK_LINKS[i];
let href = link.url;
const latitudeRegex = /LATITUDE/g;
const longitudeRegex = /LONGITUDE/g;
const zRegex = /Z/g;
href = href.replace(longitudeRegex, longitude);
href = href.replace(latitudeRegex, latitude);
href = href.replace(zRegex, DEFAULT_Z_VALUE);
dockHtml += `
<tr class="external-link-line" title="${link.title}" style="cursor: pointer;" onclick="this.querySelector('a').click();">
<td>
<i class="${DOCK_ICON}"></i>
</td>
<td title="${href}">
${link.title}<a href="${href}" target="_blank" style="display: none;">link</a>
</td>
</tr>
`;
}
dockHtml += '</tbody>';
dockHtml += '</table>';
} else {
dockHtml += `<p style="padding: 5px; font-style: italic; font-size: 0.8em; background: lightgray;">${DOCK_PLEASE_CLICK}</p>`;
}
return dockHtml;
}
/**
* Create and add the new dock in Lizmap Web Client interface
*
*/
function addLinkDock() {
const content = buildHtml();
const html = `
<div id="lizmap-external-links" style="font-size: 11pt;">
${content}
</div>
`;
lizMap.addDock(
DOCK_ID, DOCK_TITLE, DOCK_POSITION, html, DOCK_ICON
);
}
/**
* Refresh the content of the external links dock
* every time the user clicks on the map
*
* @param event Click event
*/
function onMapClick(event) {
// update panel only if active
if (document.getElementById(DOCK_ID).classList.contains('active')) {
let point = new lizMap.ol.geom.Point(event.coordinate);
if(lizMap.map.projection.projCode != "EPSG:4326"){
// reproject point to 4326
point.transform(lizMap.map.projection.projCode, 'EPSG:4326');
}
const longitude = point.getCoordinates()[0].toFixed(6);
const latitude = point.getCoordinates()[1].toFixed(6)
let content = buildHtml(longitude, latitude);
divTarget = document.getElementById('lizmap-external-links');
if (divTarget) {
// Replace content
divTarget.innerHTML = content;
}
}
}
function onDockClosed(clickeDockId)
{
if (clickeDockId == DOCK_ID) {
// external link dock closed : enable popup behaviour
lizMap.mainLizmap.popup.active = true;
}
}
function onDockOpened(clickeDockId)
{
if (clickeDockId == DOCK_ID) {
// external link dock closed : disable popup behaviour
lizMap.mainLizmap.popup.active = false;
}
}
// Listen to Lizmap Web Client interface creation
lizMap.events.on({
uicreated: function () {
// Create the dock
addLinkDock();
// Register a click on OpenLayers > 2 map
lizMap.mainLizmap.map.on('singleclick', onMapClick);
},
dockopened: function(e) {
onDockOpened(e.id);
},
minidockopened: function(e) {
onDockOpened(e.id);
},
rightdockopened: function(e) {
onDockOpened(e.id);
},
// Dock closed
dockclosed: function(e) {
onDockClosed(e.id);
},
minidockclosed: function(e) {
onDockClosed(e.id);
},
rightdockclosed: function(e) {
onDockClosed(e.id);
}
});
return {
'id': DOCK_ID,
'title': DOCK_Title,
}
}();