Skip to content

Instantly share code, notes, and snippets.

@cyberlex404
Created December 2, 2017 23:21
Show Gist options
  • Select an option

  • Save cyberlex404/11ce424b3971bd4ca6fe8d698644b993 to your computer and use it in GitHub Desktop.

Select an option

Save cyberlex404/11ce424b3971bd4ca6fe8d698644b993 to your computer and use it in GitHub Desktop.
(function ($, Drupal, settings) {
"use strict";
Drupal.behaviors.YandexMapsSimple = {
attach: function (context) {
drupalSettings['geofield_yamaps_simple'].init_count ++;
console.log(drupalSettings['geofield_yamaps_simple'].init_count);
if (drupalSettings['geofield_yamaps_simple']) {
$(context).find('.geofield-yamaps-map').once('geofield-processed').each(function (index, element) {
var mapid = $(element).attr('id');
// Check if the Map container really exists and hasn't been yet initialized.
if (drupalSettings['geofield_yamaps_simple'][mapid] && !Drupal.YaMapsSimple.map_data[mapid]) {
var map_settings = drupalSettings['geofield_yamaps_simple'][mapid]['map_settings'];
var data = drupalSettings['geofield_yamaps_simple'][mapid]['data'];
// Set the map_data[mapid] settings.
Drupal.YaMapsSimple.map_data[mapid] = map_settings;
ymaps.ready(function () {
Drupal.YaMapsSimple.map_initialize(mapid, map_settings, data);
});
}
});
}
}
};
Drupal.YaMapsSimple = {
geocoder: null,
map_data: {},
firstMapId: null,
getGeofieldYaMaps: function (mapid) {
var self = this;
var map = {};
var zoom_start = self.map_data[mapid].entity_operation !== 'edit' ? Number(self.map_data[mapid].zoom_start) : Number(self.map_data[mapid].zoom_focus);
var zoom_min = Number(self.map_data[mapid].zoom_min);
var zoom_max = Number(self.map_data[mapid].zoom_max);
var center = self.map_data[mapid].position;
var options = {
zoom: zoom_start,
minZoom: zoom_min,
maxZoom: zoom_max,
center: self.map_data[mapid].position,
// mapTypeId: self.map_data[mapid].map_type,
// mapTypeControl: !!self.map_data[mapid].map_type_selector,
// mapTypeControlOptions: {
// position: google.maps.ControlPosition.TOP_RIGHT
// },
// scaleControl: true,
// streetViewControlOptions: {
// position: google.maps.ControlPosition.TOP_RIGHT
// },
// zoomControlOptions: {
// style: google.maps.ZoomControlStyle.LARGE,
// position: google.maps.ControlPosition.TOP_LEFT
// }
};
console.log(options);
ymaps.ready(function () {
});
// map = new ymaps.Map(document.getElementById(mapid), options);
return map;
},
renderFeatureCollection: function (map, data) {
var self = this;
// var map = Drupal.YaMapsSimple.map_data[mapid].map;
var objectManager = new ymaps.ObjectManager({
// Чтобы метки начали кластеризоваться, выставляем опцию.
clusterize: true,
// ObjectManager принимает те же опции, что и кластеризатор.
gridSize: 32,
clusterDisableClickZoom: true
});
// console.log(data);
/// console.log(map);
objectManager.objects.options.set('preset', 'islands#greenDotIcon');
objectManager.clusters.options.set('preset', 'islands#greenClusterIcons');
// console.log(objectManager);
// console.log(map.geoObjects);
map.geoObjects.add(objectManager);
//map.geoObjects.add(objectManager);
objectManager.add(data);
},
// Define a Geographical point, from coordinates.
getLatLng: function (mapid, lat, lng) {
var self = this;
var latLng = {};
latLng = new ymaps.geometry.Point([lat, lng]);
console.log(latLng.getCoordinates());
return latLng;
},
// Init Geofield Map and its functions.
map_initialize: function (mapid, map_settings, data) {
var self = this;
$.noConflict();
/// todo add init data
console.log(map_settings);
console.log(data);
var center_lat = map_settings.map_center.lat,
center_lon = map_settings.map_center.lon;
// Define the Geofield Position.
var position = self.getLatLng(mapid, center_lat, center_lon);
self.map_data[mapid].position = position;
// console.log(position);
// Define the Geofield Map.
///var map = self.getGeofieldYaMaps(params.mapid);
// Map configure object
var mapState = {
center: position.getCoordinates(),
//center: [55.74954, 37.621587],
zoom: 10
};
// create Map object
var map = new ymaps.Map(document.getElementById(mapid), mapState);
var objectManager = new ymaps.ObjectManager({
// Чтобы метки начали кластеризоваться, выставляем опцию.
clusterize: true,
// ObjectManager принимает те же опции, что и кластеризатор.
gridSize: 32,
clusterDisableClickZoom: true
});
// console.log(data);
// console.log(map);
objectManager.objects.options.set('preset', 'islands#glyphIcon');
objectManager.objects.options.set('iconGlyph', 'home');
objectManager.objects.options.set('iconGlyphColor', 'blue');
objectManager.clusters.options.set('preset', 'islands#greenClusterIcons');
//console.log(objectManager);
// console.log(map.geoObjects);
map.geoObjects.add(objectManager);
//map.geoObjects.add(objectManager);
objectManager.add(data);
self.map_data[mapid].map = map;
self.map_data[mapid].features = data.features;
// console.log(map);
// self.renderFeatureCollection(map, data);
}
};
})(jQuery, Drupal, drupalSettings);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment