diff --git a/.gitignore b/.gitignore new file mode 100644 index 00000000..1e9f2471 --- /dev/null +++ b/.gitignore @@ -0,0 +1,35 @@ +# Created by .gitignore support plugin (hsz.mobi) +### JetBrains template +# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm + +## Directory-based project format +.idea/ +/*.iml +# if you remove the above rule, at least ignore user-specific stuff: +# .idea/workspace.xml +# .idea/tasks.xml +# .idea/dictionaries +# and these sensitive or high-churn files: +# .idea/dataSources.ids +# .idea/dataSources.xml +# .idea/sqlDataSources.xml +# .idea/dynamic.xml +# and, if using gradle:: +# .idea/gradle.xml +# .idea/libraries + +## File-based project format +*.ipr +*.iws + +## Additional for IntelliJ +out/ + +# generated by mpeltonen/sbt-idea plugin +.idea_modules/ + +# generated by JIRA plugin +atlassian-ide-plugin.xml + +# generated by Crashlytics plugin (for Android Studio and Intellij) +com_crashlytics_export_strings.xml diff --git a/examples/2.1/multi-geocoder/index.html b/examples/2.1/multi-geocoder/index.html new file mode 100644 index 00000000..ca203f3d --- /dev/null +++ b/examples/2.1/multi-geocoder/index.html @@ -0,0 +1,47 @@ + + + + + Пример множественного геокодирования для API 2.1 + + + + + + + + + + + +
+ + + diff --git a/examples/2.1/multi-geocoder/list-collection.js b/examples/2.1/multi-geocoder/list-collection.js new file mode 100644 index 00000000..5dadb741 --- /dev/null +++ b/examples/2.1/multi-geocoder/list-collection.js @@ -0,0 +1,63 @@ +function ListCollection() { + ListCollection.superclass.constructor.apply(this, arguments); + this._list = []; +} + +ymaps.ready(function () { + ymaps.util.augment(ListCollection, ymaps.GeoObjectCollection, { + get: function (index) { + return this._list[index]; + }, + add: function (child, index) { + this.constructor.superclass.add.call(this, child); + + index = index == null ? this._list.length : index; + this._list[index] = child; + + return this; + }, + indexOf: function (o) { + for (var i = 0, len = this._list.length; i < len; i++) { + if (this._list[i] === o) { + return i; + } + } + + return -1; + }, + splice: function (index, number) { + var added = Array.prototype.slice.call(arguments, 2), + removed = this._list.splice.apply(this._list, arguments); + + for (var i = 0, len = added.length; i < len; i++) { + this.add(added[i]); + } + + for (var i = 0, len = removed.length; i < len; i++) { + this.remove(removed[i]); + } + + return removed; + }, + remove: function (child) { + this.constructor.superclass.remove.call(this, child); + + // this._list.splice(this.indexOf(child), 1); + delete this._list[this.indexOf(child)]; + + return this; + }, + removeAll: function () { + this.constructor.superclass.removeAll.call(this); + + this._list = []; + + return this; + }, + each: function (callback, context) { + for (var i = 0, len = this._list.length; i < len; i++) { + callback.call(context, this._list[i]); + } + } + }); +}); diff --git a/examples/2.1/multi-geocoder/multi-geocoder.js b/examples/2.1/multi-geocoder/multi-geocoder.js new file mode 100644 index 00000000..e8f9d2e1 --- /dev/null +++ b/examples/2.1/multi-geocoder/multi-geocoder.js @@ -0,0 +1,58 @@ +/** + * @fileOverview + * Реализация множественного геокодирования для версии АПИ 2.1. + * + * @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 + * @name MultiGeocoder.geocode + * @param {Array} requests Массив строк-имен топонимов и/или геометрий точек (обратное геокодирование) + * @returns {Object} Как и в обычном геокодере, вернем объект-обещание. + */ + +MultiGeocoder.prototype.geocode = function (requests, options) { + var self = this, + size = requests.length, + def = new ymaps.vow.Deferred(), + 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 || def.resolve({ geoObjects: geoObjects }); + }, + function (err) { + def.reject(err); + } + ); + }); + return def.promise(); +};