-
Notifications
You must be signed in to change notification settings - Fork 1
/
script.js
244 lines (204 loc) · 5.81 KB
/
script.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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
/* eslint-disable no-undef */
/**
* control layers outside the map
*/
// config map
let config = {
minZoom: 1,
maxZoom: 12,
};
// magnification with which the map will start
const zoom = 3;
// co-ordinates
const lat = 22.918904;
const lng = 10.1343786;
// calling map
const map = L.map("map", config).setView([lat, lng], zoom);
// Used to load and display tile layers on the map
// Most tile servers require attribution, which you can set under `Layer`
/*L.tileLayer("https://tile.openstreetmap.org/{z}/{x}/{y}.png", {
attribution:
'© <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors',
}).addTo(map);
*/
L.tileLayer('https://{s}.basemaps.cartocdn.com/rastertiles/voyager/{z}/{x}/{y}.png', {
attribution: '© <a href="https://carto.com/">carto.com</a> contributors'
}).addTo(map);
function onEachFeature(feature, layer) {
layer.bindPopup(feature.properties.nazwa);
}
// adding geojson by fetch
// of course you can use jquery, axios etc.
fetch("../data/india.geojson")
.then(function (response) {
return response.json();
})
.then(function (data) {
// use geoJSON
L.geoJSON(data, { style: style }, {
onEachFeature: onEachFeature,
}).addTo(map);
});
function style(feature) {
return {
weight: 1.5,
opacity: 1,
color: 'white',
fillOpacity: getColor(feature.properties.ST_NM)
};
}
function getColor(d) {
return d == 'Maharashtra' ? '60%' : '05%';
}
// ------------------------------------------------------------
// async function to load geojson
async function fetchData(url) {
try {
const response = await fetch(url);
const data = await response.json();
return data;
} catch (err) {
console.error(err);
}
}
// fetching data from geojson
const poiLayers = L.layerGroup().addTo(map);
// center map on the clicked marker
function clickZoom(e) {
map.setView(e.target.getLatLng());
}
// geojson config
function geoJsonConfig(layerName) {
// create a new marker cluster group
window[layerName] = L.markerClusterGroup();
let geojsonOpts = {
pointToLayer: function (feature, latlng) {
// console.log(feature.properties);
if (
feature.properties.image === undefined ||
feature.properties.image === ""
) {
feature.properties.image = "images/no_img.png";
logoImg = "<a> </a>";
} else {
logoImg =
"<img src=" +
feature.properties.image +
" width='75px' height='55px' >";
}
if (
feature.properties.instagram === undefined ||
feature.properties.instagram === ""
) {
tooltipIG = "<a> </a>";
} else {
tooltipIG =
"<a href=" +
feature.properties.instagram +
" target='_blank'>Instagram </a>";
// console.log(tooltipIG);
}
if (
feature.properties.facebook === undefined ||
feature.properties.facebook === ""
) {
tooltipFB = "<a> </a>";
} else {
tooltipFB =
"<a href=" +
feature.properties.facebook +
" target='_blank'>Facebook </a>";
// console.log(tooltipFB);
}
if (
feature.properties.website === undefined ||
feature.properties.website === ""
) {
tooltipWEB = "<a> </a>";
} else {
tooltipWEB =
"<a href=" +
feature.properties.website +
" target='_blank'>Website </a>";
// console.log(tooltipWEB);
}
// add marker to markers cluster group
return window[layerName].addLayer(
L.marker(latlng, {
icon: L.icon({
className: "image-icon",
iconSize: [50, 30],
iconUrl: feature.properties.image,
popupAnchor: [0, -40],
}),
})
.bindPopup(
"<b>" +
feature.properties.name +
"</b>" +
"<br>" +
feature.properties.amenity +
"<br>" +
logoImg +
"<br>" +
tooltipIG +
"<br>" +
tooltipFB +
"<br>" +
tooltipWEB
)
.on("click", clickZoom)
);
},
};
return geojsonOpts;
}
const layersContainer = document.querySelector(".layers");
const layersButton = "all";
function generateButton(name) {
const id = name === layersButton ? "all-layers" : name;
const templateLayer = `
<li class="layer-element">
<label for="${id}">
<input type="checkbox" id="${id}" name="item" class="item" value="${name}" checked>
<span>${name}</span>
</label>
</li>
`;
layersContainer.insertAdjacentHTML("beforeend", templateLayer);
}
generateButton(layersButton);
// add data to geoJSON layer and add to LayerGroup
const arrayLayers = ["mandal", "business", "initiative"];
arrayLayers.map((json) => {
// let markers = L.markerClusterGroup();
generateButton(json);
fetchData(`./data/${json}.json`).then((data) => {
window["layer_" + json] = L.geoJSON(data, geoJsonConfig(json)).addTo(map);
});
});
document.addEventListener("click", (e) => {
e.stopPropagation();
const target = e.target;
const itemInput = target.closest(".item");
if (!itemInput) return;
showHideLayer(target);
});
function showHideLayer(target) {
if (target.id === "all-layers") {
arrayLayers.map((json) => {
checkedType(json, target.checked);
});
} else {
checkedType(target.id, target.checked);
}
const checkedBoxes = document.querySelectorAll("input[name=item]:checked");
const allLayers = document.querySelector("#all-layers");
allLayers.checked =
checkedBoxes.length - (allLayers.checked === true ? 1 : 0) < 3 ? false
: true;
}
function checkedType(id, type) {
map[type ? "addLayer" : "removeLayer"](window["layer_" + id]);
document.querySelector(`#${id}`).checked = type;
}