forked from dimik/ymaps
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmulti-geocoder.js
65 lines (58 loc) · 2.4 KB
/
multi-geocoder.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
/**
* @fileOverview
* Пример реализации функциональности множественного геокодирования.
* Аналогичная разработка для первой версии АПИ.
* @see http://api.yandex.ru/maps/doc/jsapi/1.x/examples/multiplygeocoding.html
* @example
var multiGeocoder = new MultiGeocoder({ boundedBy : map.getBounds() });
multiGeocoder
.geocode(['Москва, Льва Толстого 16', [55.7, 37.5], 'Санкт-Петербург'])
.then(
function (res) {
map.geoObjects.add(res.geoObjects);
},
function (err) {
console.log(err);
}
);
*/
/**
* Класс для геокодирования списка адресов или координат.
* @class
* @name MultiGeocoder
* @param {Object} [options={}] Дефолтные опции мультигеокодера.
*/
function MultiGeocoder(options) {
this._options = options || {};
}
/**
* Функция множественнеого геокодирования.
* @function
* @requires ymaps.util.extend
* @see http://api.yandex.ru/maps/doc/jsapi/2.x/ref/reference/util.extend.xml
* @requires ymaps.util.Promise
* @see http://api.yandex.ru/maps/doc/jsapi/2.x/ref/reference/util.Promise.xml
* @name MultiGeocoder.geocode
* @param {Array} requests Массив строк-имен топонимов и/или геометрий точек (обратное геокодирование)
* @returns {Object} Как и в обычном геокодере, вернем объект-обещание.
*/
MultiGeocoder.prototype.geocode = function (requests, options) {
var self = this,
size = requests.length,
promise = new ymaps.util.Promise(),
geoObjects = new ListCollection();
requests.forEach(function (request, index) {
ymaps.geocode(request, ymaps.util.extend({}, self._options, options))
.then(
function (response) {
var geoObject = response.geoObjects.get(0);
geoObject && geoObjects.add(geoObject, index);
--size || promise.resolve({ geoObjects : geoObjects });
},
function (err) {
promise.reject(err);
}
);
});
return promise;
};