Skip to content

Instantly share code, notes, and snippets.

@sevkin
Last active February 11, 2024 22:20
Show Gist options
  • Save sevkin/10446710 to your computer and use it in GitHub Desktop.
Save sevkin/10446710 to your computer and use it in GitHub Desktop.
sample yandex.maps AMD module
/*
requirejs.config({
paths: {
'ymaps': ['//api-maps.yandex.ru/2.0-stable/?load=package.standard&lang=ru-RU']
},
shim: {
'ymaps': {exports: 'ymaps'}
}
});
*/
define(['jquery', 'ymaps'], function ($, ymaps) {
function geocode(address) {
var d = $.Deferred();
ymaps.geocode(address).then(
function (res) {
if (res.geoObjects.getLength()) {
var point = res.geoObjects.get(0).geometry.getCoordinates();
d.resolve(point);
} else {
d.reject();
}
}, function (err) {
d.reject();
});
return d.promise();
}
/*
function geocodes(addresses) {
var d = $.Deferred();
var promises = $.map(addresses, geocode);
$.when.apply($, promises).done(function() {
d.resolve(arguments);
}).fail(function() {
d.reject();
});
return d;
}
*/
function ymap(id, center) {
var map = new ymaps.Map(id, {
center: center,
zoom: 16,
behaviors: ['default', 'scrollZoom']
});
map.controls.add("zoomControl");
map.placemark = function(point, title, balloon, preset) {
var placemark = new ymaps.Placemark(point, {
// balloonContent: (typeof(balloon)==='undefined' && balloon),
iconContent: title
}, {
preset: (typeof(preset)==='undefined' && "twirl#blueStretchyIcon" || preset)
});
this.geoObjects.add(placemark);
};
return map;
}
// TODO addresses array -> map {point, title, balloon, preset}
function map (id, addresses, title) {
// var addresses = $('.content address').map(function(i,e) { return $(e).html(); });
// geocodes(addresses).done(function(points) {
geocode(addresses[0]).done(function(point) {
// TODO center for many points
// var center = [
// _.reduce(_.map(points, function(p) { return p[0]; } ), function(s, n) { return s+n; }) / points.length,
// _.reduce(_.map(points, function(p) { return p[1]; } ), function(s, n) { return s+n; }) / points.length
// ];
var map = ymap(id, point /*center*/);
map.placemark(point, title);
});
}
var d = $.Deferred();
ymaps.ready(function () {
d.resolve(map);
});
return d.promise();
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment