diff --git a/package.json b/package.json index e4e3c4dd..eba96d86 100644 --- a/package.json +++ b/package.json @@ -10,7 +10,7 @@ }, "repository": { "type": "git", - "url": "git://github.com/perliedman/leaflet-control-geocoder.git" + "url": "git://github.com/sputnik-maps/leaflet-control-geocoder.git" }, "keywords": [ "leaflet", diff --git a/src/all.js b/src/all.js new file mode 100755 index 00000000..24fb5ede --- /dev/null +++ b/src/all.js @@ -0,0 +1,43 @@ +var L = require('leaflet'), + Control = require('./control'), + Sputnik = require('./geocoders/sputnik'), + Nominatim = require('./geocoders/nominatim'), + Bing = require('./geocoders/bing'), + MapQuest = require('./geocoders/mapquest'), + Mapbox = require('./geocoders/mapbox'), + What3Words = require('./geocoders/what3words'), + Google = require('./geocoders/google'), + Photon = require('./geocoders/photon'), + Mapzen = require('./geocoders/mapzen'), + ArcGis = require('./geocoders/arcgis'), + HERE = require('./geocoders/here'); + +module.exports = L.Util.extend(Control.class, { + Sputnik: Sputnik.class, + sputnik: Sputnik.factory, + Nominatim: Nominatim.class, + nominatim: Nominatim.factory, + Bing: Bing.class, + bing: Bing.factory, + MapQuest: MapQuest.class, + mapQuest: MapQuest.factory, + Mapbox: Mapbox.class, + mapbox: Mapbox.factory, + What3Words: What3Words.class, + what3words: What3Words.factory, + Google: Google.class, + google: Google.factory, + Photon: Photon.class, + photon: Photon.factory, + Mapzen: Mapzen.class, + mapzen: Mapzen.factory, + ArcGis: ArcGis.class, + arcgis: ArcGis.factory, + HERE: HERE.class, + here: HERE.factory +}); + +L.Util.extend(L.Control, { + Geocoder: module.exports, + geocoder: Control.factory +}); diff --git a/src/control.js b/src/control.js index 833f6277..57b2c3be 100644 --- a/src/control.js +++ b/src/control.js @@ -1,5 +1,5 @@ var L = require('leaflet'), - Nominatim = require('./geocoders/nominatim').class; + Sputnik = require('./geocoders/sputnik').class; module.exports = { class: L.Control.extend({ @@ -8,8 +8,8 @@ module.exports = { collapsed: true, expand: 'click', position: 'topright', - placeholder: 'Search...', - errorMessage: 'Nothing found.', + placeholder: 'Поиск...', + errorMessage: 'Не найдено', suggestMinLength: 3, suggestTimeout: 250, defaultMarkGeocode: true @@ -20,7 +20,7 @@ module.exports = { initialize: function (options) { L.Util.setOptions(this, options); if (!this.options.geocoder) { - this.options.geocoder = new Nominatim(); + this.options.geocoder = new Sputnik(); } this._requestCount = 0; @@ -120,7 +120,7 @@ module.exports = { this._map.removeLayer(this._geocodeMarker); } - this._geocodeMarker = new L.Marker(result.center) + this._geocodeMarker = new (L.sm.Marker || L.Marker)(result.center) .bindPopup(result.html || result.name) .addTo(this._map) .openPopup(); diff --git a/src/geocoders/sputnik.js b/src/geocoders/sputnik.js new file mode 100644 index 00000000..17769e0b --- /dev/null +++ b/src/geocoders/sputnik.js @@ -0,0 +1,92 @@ +var L = require('leaflet'), + Util = require('../util'); + +module.exports = { + class: L.Class.extend({ + options: { + markerClass: Util.extract(L, 'sm.Marker', L.Marker), + geocoderUrl: 'http://search.maps.sputnik.ru/search/addr', + reverseGeocoderUrl: 'http://whatsthere.maps.sputnik.ru/point' + }, + + _bboxDeltas: { + "country": { + "latD": 29.4912965, + "lngD": 122.871094 + }, + "region": { + "latD": 2.2116335, + "lngD": 7.6794435 + }, + "district": { + "latD": 0.2752005, + "lngD": 0.9599305 + }, + "place": { + "latD": 0.1496885, + "lngD": 0.4837415 + }, + "street": { + "latD": 0.004543, + "lngD": 0.015117 + }, + "house": { + "latD": 0.0011355, + "lngD": 0.0037795 + }, + "default": { + "latD": 0.1, + "lngD": 0.1 + } + }, + + getBBox: function ( center, type ) { + type = type in this._bboxDeltas ? type : 'default'; + var deltas = this._bboxDeltas[type]; + return L.latLngBounds( + L.latLng(center.lat - deltas.latD, center.lng - deltas.lngD ), + L.latLng(center.lat + deltas.latD, center.lng + deltas.lngD ) + ) + }, + + geocode: function (query, cb, context) { + Util.jsonp(this.options.geocoderUrl, { + q: query + }, function(data) { + cb.call(context, this._prepareResult(data)); + }, this, 'callback'); + }, + + reverse: function(location, scale, cb, context) { + Util.jsonp(this.options.reverseGeocoderUrl , { + lat: location.lat, + lon: location.lng, + houses : true + }, function(data) { + cb.call(context, this._prepareResult(data)); + }, this, 'callback'); + }, + _prepareResult: function (data) { + var _this = this; + var features = [].concat(Util.extract(data, 'result.address.0.features', []), Util.extract(data, 'result.address.1.features', [])); + return features.map(function(feature){ + var feats = L.geoJson(feature); + var bounds = feats && feats.getBounds(), + center = bounds.getCenter(); + var bbox = _this.getBBox(center, feature.properties.type); + var name = [feature.properties.title, feature.properties.description] + .filter(function(value){ return !! value; }) + .join(', '); + return { + name: name, + bbox: bbox, + center: bounds.getCenter() + } + }); + } + }), + + factory: function(options) { + return new L.Control.Geocoder.Sputnik(options); + } +}; diff --git a/src/index.js b/src/index.js index e74896ca..2311097b 100755 --- a/src/index.js +++ b/src/index.js @@ -1,37 +1,10 @@ var L = require('leaflet'), Control = require('./control'), - Nominatim = require('./geocoders/nominatim'), - Bing = require('./geocoders/bing'), - MapQuest = require('./geocoders/mapquest'), - Mapbox = require('./geocoders/mapbox'), - What3Words = require('./geocoders/what3words'), - Google = require('./geocoders/google'), - Photon = require('./geocoders/photon'), - Mapzen = require('./geocoders/mapzen'), - ArcGis = require('./geocoders/arcgis'), - HERE = require('./geocoders/here'); + Sputnik = require('./geocoders/sputnik'); module.exports = L.Util.extend(Control.class, { - Nominatim: Nominatim.class, - nominatim: Nominatim.factory, - Bing: Bing.class, - bing: Bing.factory, - MapQuest: MapQuest.class, - mapQuest: MapQuest.factory, - Mapbox: Mapbox.class, - mapbox: Mapbox.factory, - What3Words: What3Words.class, - what3words: What3Words.factory, - Google: Google.class, - google: Google.factory, - Photon: Photon.class, - photon: Photon.factory, - Mapzen: Mapzen.class, - mapzen: Mapzen.factory, - ArcGis: ArcGis.class, - arcgis: ArcGis.factory, - HERE: HERE.class, - here: HERE.factory + Sputnik: Sputnik.class, + sputnik: Sputnik.factory }); L.Util.extend(L.Control, { diff --git a/src/util.js b/src/util.js index f08aa0b3..a6fa65a3 100644 --- a/src/util.js +++ b/src/util.js @@ -38,6 +38,16 @@ var L = require('leaflet'), })(); module.exports = { + extract: function extract(root, path, def){ + var key, + val = !!root ? root : this, + arr = String(path).replace(/'|"|\]/g,'').replace(/\[/g,'.').split('.'); + while ((key = arr.shift()) && 'object' == typeof val && val) { + val = 'undefined' == typeof val[key]? ('undefined' == typeof def? false : def) : val[key]; + } + return val; + }, + jsonp: function(url, params, callback, context, jsonpParam) { var callbackId = '_l_geocoder_' + (lastCallbackId++); params[jsonpParam || 'callback'] = callbackId; diff --git a/test/browserify.html b/test/browserify.html index b3141dd9..63805c08 100644 --- a/test/browserify.html +++ b/test/browserify.html @@ -3,7 +3,7 @@ Leaflet Control Geocoder - + diff --git a/test/browserify.js b/test/browserify.js index 39a862fa..90851ae1 100644 --- a/test/browserify.js +++ b/test/browserify.js @@ -2,7 +2,7 @@ var L = require('leaflet'); require('../dist/Control.Geocoder'); var map = L.map('map').setView([0, 0], 2), - geocoder = L.Control.Geocoder.mapzen('search-DopSHJw'), + geocoder = L.Control.Geocoder.sputnik(), control = L.Control.geocoder({ geocoder: geocoder }).addTo(map), diff --git a/test/position.html b/test/position.html index 8e49fb97..75f55d14 100644 --- a/test/position.html +++ b/test/position.html @@ -3,10 +3,10 @@ Leaflet Control Geocoder - + - +