PageSourceSearch

https://alekol.ru/assets/multi-geocoder.js

js alekol.ru collected 2026-10-05 00:24:03 UTC 2,550 bytes, 66 lines download raw bytes

1/**
2 * @fileOverview
3 * Пример реализации функциональности множественного геокодирования.
4 * Аналогичная разработка для первой версии АПИ.
5 * @see http://api.yandex.ru/maps/doc/jsapi/1.x/examples/multiplygeocoding.html
6 * @example
7 * var multiGeocoder = new MultiGeocoder({ boundedBy : map.getBounds() });
8 * multiGeocoder
9 *     .geocode(['Москва, Льва Толстого 16', [55.7, 37.5], 'Санкт-Петербург'])
10 *         .then(
11 *             function (res) {
12 *                 map.geoObjects.add(res.geoObjects);
13 *             },
14 *             function (err) {
15 *                 console.log(err);
16 *             }
17 *         );
18 */
19
20/**
21 * Класс для геокодирования списка адресов или координат.
22 * @class
23 * @name MultiGeocoder
24 * @param {Object} [options={}] Дефолтные опции мультигеокодера.
25 */
26function MultiGeocoder(options) {
27    this._options = options || {};
28}
29
30/**
31 * Функция множественнеого геокодирования.
32 * @function
33 * @requires ymaps.util.extend
34 * @see http://api.yandex.ru/maps/doc/jsapi/2.x/ref/reference/util.extend.xml
35 * @requires ymaps.util.Promise
36 * @see http://api.yandex.ru/maps/doc/jsapi/2.x/ref/reference/util.Promise.xml
37 * @name MultiGeocoder.geocode
38 * @param {Array} requests Массив строк-имен топонимов и/или геометрий точек (обратное геокодирование)
39 * @returns {Object} Как и в обычном геокодере, вернем объект-обещание.
40 */
41MultiGeocoder.prototype.geocode = function (requests, options) {
42    var self = this,
43        opts = ymaps.util.extend({}, self._options, options),
44        size = requests.length,
45        promise = new ymaps.util.Promise(),
46        result = [],
47        geoObjects = new ymaps.GeoObjectArray();
48
49    requests.forEach(function (request, index) {
50        ymaps.geocode(request, opts)
51            .then(
52                function (response) {
53                    var geoObject = response.geoObjects.get(0);
54
55                    geoObject && (result[index] = geoObject);
56                    --size || (result.forEach(geoObjects.add, geoObjects), promise.resolve({ geoObjects : geoObjects }));
57                },
58                function (err) {
59                    promise.reject(err);
60                }
61            );
62    });
63
64    return promise;
65};
66

Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.