-
Notifications
You must be signed in to change notification settings - Fork 53
/
select_syncs.html
157 lines (139 loc) · 5.16 KB
/
select_syncs.html
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
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Leaflet Sync Demo - with three maps listening. Select what to sync</title>
<link rel="stylesheet" href="https://unpkg.com/[email protected]/dist/leaflet.css" crossorigin=""/>
<script src="https://unpkg.com/[email protected]/dist/leaflet-src.js" crossorigin=""></script>
<style type="text/css">
html, body { width: 100%; height: 100%; margin: 0; }
#map, #container { width: 50%; height: 100%; }
#map { float: left; }
#container { float: right; }
#container .map { width: 100%; height: 50%; }
.button {
background-color: white;
width: 30px;
height: 30px;
display: flex;
align-items: center;
justify-content: center;
}
</style>
</head>
<body>
<div id="map"></div>
<div id="container">
<div id="mapA" class="map"></div>
<div id="mapB" class="map"></div>
</div>
<script src="../L.Map.Sync.js"></script>
<script type="text/javascript">
var center = [52.517, 13.388];
var zoom = 14;
var options = {
attribution:
'<a href="https://openstreetmap.org/copyright">' +
'© OpenStreetmap and Contributors</a>',
subdomains: 'abc',
minZoom: 0,
maxZoom: 20
};
var url = '//{s}.tile.openstreetmap.org/{z}/{x}/{y}.png';
var mapA = L.map('mapA', {
layers: [L.tileLayer(url, options)],
center: center,
zoom: zoom,
zoomControl: false
});
var mapB = L.map('mapB', {
layers: [L.tileLayer(url, options)],
center: center,
zoom: zoom,
zoomControl: false
});
var map = L.map('map', {
layers: [L.tileLayer(url, options)],
center: center,
zoom: zoom
});
function offsetGlobal (center, zoom, refMap, tgtMap) {
var refC = refMap.getContainer();
var tgtC = tgtMap.getContainer();
var pt = refMap.project(center, zoom)
.subtract([refC.offsetLeft, refC.offsetTop])
.subtract(refMap.getSize().divideBy(2))
.add([tgtC.offsetLeft, tgtC.offsetTop])
.add(tgtMap.getSize().divideBy(2));
return refMap.unproject(pt, zoom);
}
var doAll = false;
var doA = false;
var doB = false;
var p = location.search;
if (p != '') {
var params = p.substring(1).split('&');
for (var i = 0; i < params.length; i++) {
var param = params[i].split('=');
switch (param[0]) {
case 'all':
doAll = parseInt(param[1]);
break;
case 'a':
doA = parseInt(param[1]);
break;
case 'b':
doB = parseInt(param[1]);
break;
}
}
}
var syncControl = L.Control.extend({
initialize: function (otherMap, text, options) {
this._otherMap = otherMap;
this._text = text || 'sync';
this.options = L.extend({position: 'topright'}, options);
},
onAdd: function (map) {
var container = L.DomUtil.create('div', 'leaflet-bar leaflet-control leaflet-control-custom button');
container.style.color = 'red';
container.innerHTML = this._text;
var that = this;
container.onclick = function (e) {
if (map.isSynced(that._otherMap)) {
map.unsync(that._otherMap);
container.style.color = 'red';
} else {
map.sync(that._otherMap, {offsetFn: offsetGlobal});
container.style.color = 'blue';
}
};
return container;
},
});
var contA0 = new syncControl(map, '<', {position: 'topleft'});
mapA.addControl(contA0);
var cont0A = new syncControl(mapA, '>', {position: 'topright'});
map.addControl(cont0A);
var contB0 = new syncControl(map, '<', {position: 'bottomleft'});
mapB.addControl(contB0);
var cont0B = new syncControl(mapB, '>', {position: 'bottomright'});
map.addControl(cont0B);
var contAB = new syncControl(mapB, 'v', {position: 'bottomleft'});
mapA.addControl(contAB);
var contBA = new syncControl(mapA, '^', {position: 'topleft'});
mapB.addControl(contBA);
// If you want interaction with mapA|B to be synchronized on map,
// add other links as well
// or add ?all=1 to the url.
if (doAll || doA) {
contA0._container.onclick();
cont0A._container.onclick();
}
if (doAll || doB) {
contB0._container.onclick();
cont0B._container.onclick();
}
</script>
</body>
</html>