-
Notifications
You must be signed in to change notification settings - Fork 0
/
map.js
119 lines (105 loc) · 3.79 KB
/
map.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
var elmap = document.getElementById("map");
var yaMap, myPlacemark;
if (elmap) {
var defaults = document.querySelector('.marker');
var resetButton = document.querySelector('.reset');
var eladdress = document.querySelector('.map__adress');
ymaps.ready(init);
}
function init(){
yaMap = new ymaps.Map("map", {
center: [Number(defaults.dataset.lng),Number(defaults.dataset.lat)],
zoom: 12,
controls: ['geolocationControl', 'trafficControl', 'typeSelector', 'fullscreenControl', 'zoomControl', 'routeButtonControl']
});
function places( place ) {
for ( var i = 0; i < place.length; i++ ) {
var lng = Number(place[i].dataset.lng);
var lat = Number(place[i].dataset.lat);
var name = place[i].dataset.name;
var adress = place[i].dataset.adress;
var ico = place[i].dataset.ico;
if (ico == '') {
var icon = {
preset: 'islands#redIcon'
}
} else {
var icon = {
iconLayout: 'default#image',
iconImageHref: ico,
iconImageSize: [100, 100],
// Смещение левого верхнего угла иконки относительно
// её "ножки" (точки привязки).
iconImageOffset: [-50, -95]
}
}
var myPlacemark = new ymaps.Placemark(
[lng, lat],
{
iconCaption: name,
iconContent: '',
balloonContentHeader: name,
balloonContentBody: adress
}, icon
);
yaMap.geoObjects.add(myPlacemark);
var mapAddress = document.querySelectorAll('.js-address');
mapAddress.forEach(function(el) {
el.addEventListener("click", function (e) {
lng = Number(e.currentTarget.dataset.lng);
lat = Number(e.currentTarget.dataset.lat);
name = e.currentTarget.dataset.name;
adress = e.currentTarget.dataset.adress;
mapAddress.forEach(el => {
el.classList.remove('active');
});
e.currentTarget.classList.add('active');
eladdress.classList.add('active');
yaMap.setCenter(myPlacemark.geometry.getCoordinates());
yaMap.balloon.close();
myPlacemark = new ymaps.Placemark(
[lng, lat],
{
iconCaption: name,
iconContent: '',
balloonContentHeader: name,
balloonContentBody: adress
}, icon
);
yaMap.geoObjects.add(myPlacemark);
});
});
}
}
var markers = document.querySelectorAll(".marker");
places(markers);
yaMap.behaviors.disable('scrollZoom');
/*Центрируем карту по маркерам*/
centerMap();
}
function centerMap () {
yaMap.setBounds(
yaMap.geoObjects.getBounds(), {
checkZoomRange:true
})
.then(
function(){
if(yaMap.getZoom() > 16) {
yaMap.setZoom(16)
}
}
);
}
function reset() {
centerMap();
var mapAddress = document.querySelectorAll('.js-address');
mapAddress.forEach(el => {
el.classList.remove('active');
});
eladdress.classList.remove('active');
}
if (resetButton) {
resetButton.addEventListener("click", () => {
reset();
});
}