-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
361 lines (342 loc) · 12.7 KB
/
app.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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
/**
* Yandex Maps API parser infrastructure objects.
* Created by Gleb "xenongee" Rublev, 21.11.2022.
*/
const boundLatitude = 0.004500/2; // horizontal
const boundLongitude = 0.008500/2; // vertical
let mapObject,
geoObject,
geoObjectCoordinates,
geoObjectSearchBounds,
searchCategories,
searchControl,
searchResults;
let searchAddress = document.getElementById("infra-search-input-address");
let searchButton = document.getElementById("infra-search-btn-search");
let searchStatusWrapper = document.getElementById("infra-search-status-wrapper");
let searchStatusText = document.getElementById("infra-search-status");
let searchObjCount = document.getElementById("infra-search-obj-count");
let searchFinalResult = document.getElementById("infra-search-result");
/**
* Initialize
*/
function init() {
parserStatus("Инициализация.");
// suggestions on search field
let searchAddressSuggestView = new ymaps.SuggestView("infra-search-input-address");
// run geocode on click event
searchButton.onclick = () => geocode();
}
/**
* Start ymaps geocode
*/
function geocode() {
parserStatus("Геокодируем адрес в координаты, получаем границы объекта поиска.");
// value from html form
let searchAddressValue = searchAddress.value;
// geocoding object of search
ymaps.geocode(searchAddressValue).then(function (res) {
// getting object from geocoder
geoObject = res.geoObjects.get(0);
let error;
if (geoObject) {
switch (geoObject.properties.get("metaDataProperty.GeocoderMetaData.precision")) {
case "exact":
break;
case "number":
case "near":
case "range":
case "street":
error = "Неполный адрес, уточните номер дома";
break;
case "other":
default:
error = "Неточный адрес, уточните адрес";
}
} else {
error = "Адрес не найден, уточните адрес";
}
if (error) {
parserStatus(error);
} else {
// setting icon for map
geoObject.options.set("preset", "islands#blueHomeCircleIcon");
// getting bounds and coordinates
// let geoObjectBounds = geoObject.properties.get("boundedBy");
geoObjectCoordinates = geoObject.geometry.getCoordinates();
geoObjectSearchBounds = [
// [(geoObjectBounds[0][0]-boundLatitude),(geoObjectBounds[0][1]-boundLongitude)],
// [(geoObjectBounds[1][0]+boundLatitude),(geoObjectBounds[1][1]+boundLongitude)]
[(geoObjectCoordinates[0] - boundLatitude), (geoObjectCoordinates[1] - boundLongitude)],
[(geoObjectCoordinates[0] + boundLatitude), (geoObjectCoordinates[1] + boundLongitude)]
];
// map settings
let mapState = {
center: geoObjectCoordinates,
zoom: 12,
controls: []
};
let searchControlParams = {
options: {
provider: "yandex#search",
noPopup: true,
noSuggestPanel: true,
results: 100,
boundedBy: geoObjectSearchBounds
}
};
createMap(mapState, searchControlParams);
}
});
}
/**
* Creating yandex.map object
* @param {object} state map settings
* @param {object} searchParams search settings
* @param {boolean} [recreate=false] recreate map, only for final results
* @param {object} [parseResults=null] if 'recreate' param is true, this param is required
*/
function createMap(state, searchParams, recreate = false, parseResults = null) {
// destroying map
if (mapObject && recreate) {
parserStatus("Удаляем старую карту.");
mapObject.destroy();
mapObject = null;
}
// if map object is not created
if (!mapObject) {
parserStatus("Создаем карту, отображаем объект в виде иконки и границы.");
// creating map
mapObject = new ymaps.Map("ymap", state, {
restrictMapArea: true
});
if (!recreate) {
// add searchControl for searching process
searchControl = new ymaps.control.SearchControl(searchParams);
mapObject.controls.add(searchControl);
}
// creating rectangle from bounds positions
let rectangle = new ymaps.Rectangle(geoObjectSearchBounds, {}, {
fillColor: "#ff0000",
fillOpacity: 0.2,
strokeColor: "#ff0000",
strokeOpacity: 0.3,
strokeWidth: 2,
borderRadius: 3
});
// adding graphics on map
mapObject.geoObjects.add(geoObject); // dot on object
mapObject.geoObjects.add(rectangle); // rectangle of search object
if (recreate && parseResults) {
showIcons(parseResults);
}
}
if (!recreate) {
parseMap();
}
mapObject.setBounds(geoObjectSearchBounds);
}
/**
* Parsing map
* @async
* @returns {Promise<any>}
*/
async function parseMap() {
parserStatus("Ищем новые объекты рядом с объектом поиска. Подождите...");
searchCategories = [
[
"Государственные лечебные учреждения",
"islands#blueMedicalCircleIcon",
],
[
"Аптеки",
"islands#blueMedicalCircleIcon",
],
[
"Детские сады",
"islands#blueFamilyCircleIcon",
],
[
"Общеобразовательные школы, лицеи",
"islands#blueEducationCircleIcon",
],
[
"Развивающие центры",
"islands#blueEducationCircleIcon",
],
[
"Среднее специальное образование",
"islands#blueEducationCircleIcon",
],
[
"Высшее образование",
"islands#blueEducationCircleIcon",
],
[
"Дополнительное образование",
"islands#blueEducationCircleIcon",
],
[
"Научные организации",
"islands#blueScienceCircleIcon",
],
[
"Библиотека",
"islands#blueBookCircleIcon",
],
[
"Магазин",
"islands#blueShoppingCircleIcon",
],
[
"Торговые центры",
"islands#blueShoppingCircleIcon",
],
[
"Продукты питания",
"islands#blueShoppingCircleIcon",
],
[
"Универсальные магазины",
"islands#blueShoppingCircleIcon",
],
[
"Кафе, ресторан, столовая",
"islands#blueFoodCircleIcon",
],
[
"Детская площадка",
"islands#blueFamilyCircleIcon",
],
[
"Спортплощадка",
"islands#blueSportCircleIcon",
],
[
"Собачья площадка",
"islands#blueDogCircleIcon",
],
[
"Остановка общественного транспорта",
"islands#blueMassTransitCircleIcon"
]
];
let parseResults = new Object();
let parseResultsSearchObjects = new Object();
for (let i = 0; i < searchCategories.length; i++) {
await search(i);
let idObj = 0;
// main parser object
parseResults[i] = {
categoryName: searchCategories[i][0],
categoryIcon: searchCategories[i][1],
objects: {}
};
// reset previous results
parseResultsSearchObjects = [];
for (let j = 0; j < searchResults.length; j++) {
let searchObjectCoordinates = searchResults[j].geometry._coordinates;
if (geoObjectSearchBounds[0][0] <= searchObjectCoordinates[0] &&
searchObjectCoordinates[0] <= geoObjectSearchBounds[1][0] &&
geoObjectSearchBounds[0][1] <= searchObjectCoordinates[1] &&
searchObjectCoordinates[1] <= geoObjectSearchBounds[1][1]) {
parseResults[i].objects = Object.assign(parseResultsSearchObjects, {
[idObj]: {
objCategory: searchResults[j].properties._data.categoriesText,
objName: searchResults[j].properties._data.name,
objAddress: searchResults[j].properties._data.address,
objCoordinates: searchResults[j].geometry._coordinates,
}
});
++idObj;
}
}
}
showResults(parseResults);
}
/**
* Searching infrastructure objects
* @async
* @param {number} idCategories index of categories for search
* @returns {Promise<any>}
*/
async function search(idCategories) {
let searchCategoriesValue = searchCategories[idCategories][0];
let term = "> Поиск по категории: " + searchCategoriesValue;
parserStatus(term)
await searchControl.search(searchCategoriesValue).then(function () {
searchResults = searchControl.getResultsArray();
});
}
/**
* Prints results
* @param {object} parseResults
*/
function showResults(parseResults) {
parserStatus("Оформляем результат...");
let parseResultsLength = Object.keys(parseResults).length;
let mapState = {
center: geoObjectCoordinates,
zoom: 14,
controls: []
};
let searchControlParams = {};
createMap(mapState, searchControlParams, true, parseResults);
let resultToHtml = "",
parseResultsEntryLength = 0;
for (let i = 0; i < parseResultsLength; i++) {
let parseEntry = parseResults[i];
let parseObjectsEntryLength = Object.keys(parseEntry.objects).length;
let parseObjectEntryName = parseEntry.categoryName;
resultToHtml += [i + 1, ") ", parseObjectEntryName, "<br>"].join("");
parseResultsEntryLength += parseObjectsEntryLength;
if (parseObjectsEntryLength > 0) {
for (let j = 0; j < parseObjectsEntryLength; j++) {
resultToHtml += [i + 1, ".", j + 1, ") ", parseEntry.objects[j].objCategory, " - ", parseEntry.objects[j].objName, "<br>"].join("");
resultToHtml += [parseEntry.objects[j].objAddress, " (", parseEntry.objects[j].objCoordinates, ")", "<br><br>"].join("");
}
} else {
resultToHtml += "Результаты отсутствуют <br><br>";
}
}
parserStatus("Готово.");
searchButton.onclick = () => {
parserStatus("Перезагрузите страницу и введите новый запрос.");
mapObject.setBounds(geoObjectSearchBounds);
}
searchObjCount.innerHTML = parseResultsEntryLength;
searchFinalResult.innerHTML = resultToHtml;
}
/**
* Adds a ballons on map
* @param {object} parseResults
*/
function showIcons(parseResults) {
parserStatus("!!!Отображаем объекты с парсера.");
let parseResultsLength = Object.keys(parseResults).length;
for (let i = 0; i < parseResultsLength; i++) {
let parseEntry = parseResults[i];
let parseObjectsEntryLength = Object.keys(parseEntry.objects).length;
let parseGeoObjects = new ymaps.GeoObjectCollection({}, {
preset: parseEntry.categoryIcon,
});
for (let j = 0; j < parseObjectsEntryLength; j++) {
parseGeoObjects.add(new ymaps.Placemark(parseEntry.objects[j].objCoordinates, {
balloonContentHeader: parseEntry.objects[j].objName,
balloonContentBody: ["Адрес:", parseEntry.objects[j].objAddress].join(" "),
balloonContentFooter: [parseEntry.objects[j].objCategory, "<br>", parseEntry.objects[j].objCoordinates].join(" "),
hintContent: parseEntry.objects[j].objName
}));
mapObject.geoObjects.add(parseGeoObjects);
}
}
}
/**
* Prints status
* @param {*} message
*/
function parserStatus(message) {
searchStatusText.innerHTML += message + "<br>";
searchStatusWrapper.scrollTop = 9999;
}
ymaps.ready(init);