Created
September 4, 2015 19:04
-
-
Save Ibochkarev/3e113c6057e1c7ecf664 to your computer and use it in GitHub Desktop.
Yandex group placemark Maps
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
ymaps.ready(init); | |
function init() { | |
// Создание экземпляра карты. | |
var myMap = new ymaps.Map('map', { | |
center: [55.75399400, 37.62209300], | |
zoom: 5, | |
controls: ['zoomControl'] | |
}, { | |
searchControlProvider: 'yandex#search' | |
}), | |
// Контейнер для меню. | |
menu = $('<ul class="menu"></ul>'); | |
for (var i = 0, l = groups.length; i < l; i++) { | |
createMenuGroup(groups[i]); | |
} | |
function createMenuGroup(group) { | |
// Пункт меню. | |
var menuItem = $('<li><a href="#">' + group.name + '</a></li>'), | |
// Коллекция для геообъектов группы. | |
collection = new ymaps.GeoObjectCollection(null, { | |
preset: group.style | |
}), | |
// Контейнер для подменю. | |
submenu = $('<ul class="submenu"></ul>'); | |
// Добавляем коллекцию на карту. | |
myMap.geoObjects.add(collection); | |
// Добавляем подменю. | |
menuItem.append(submenu) | |
// Добавляем пункт в меню. | |
.appendTo(menu) | |
// По клику удаляем/добавляем коллекцию на карту и скрываем/отображаем подменю. | |
.find('a') | |
.css('color', '#000') | |
.toggle(function () { | |
// myMap.geoObjects.remove(collection); | |
submenu.show(); | |
}, function () { | |
myMap.geoObjects.add(collection); | |
submenu.show(); | |
}); | |
for (var j = 0, m = group.items.length; j < m; j++) { | |
createSubMenu(group.items[j], collection, submenu); | |
} | |
} | |
function createSubMenu(item, collection, submenu) { | |
// Пункт подменю. | |
var submenuItem = $('<li><a href="#">' + item.name + '</a></li>'), | |
// Создаем метку. | |
placemark = new ymaps.Placemark(item.center, { | |
balloonContent: //item.name + '<br>' + item.address + '<br>' + item.phone | |
'<b> ' + item.city + '</b><br/>' | |
+ item.tc_shop + '<br/>' | |
+ '<b> ' + item.name + '</b><br/>' | |
+ '<b> Адрес: </b>' + item.city + ' '+ item.address | |
}); | |
// Добавляем метку в коллекцию. | |
collection.add(placemark); | |
// Добавляем пункт в подменю. | |
submenuItem.appendTo(submenu) | |
// При клике по пункту подменю открываем/закрываем баллун у метки. | |
.find('a') | |
.css('color', '#000') | |
.toggle(function () { | |
placemark.balloon.open(); | |
}, function () { | |
placemark.balloon.close(); | |
}); | |
} | |
// Добавляем меню в тэг BODY. | |
menu.appendTo($('#shops')); | |
// Выставляем масштаб карты чтобы были видны все группы. | |
//myMap.setBounds(myMap.geoObjects.getBounds()); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment