diff --git a/README.md b/README.md
index 49ce4d7..1f8e439 100644
--- a/README.md
+++ b/README.md
@@ -9,10 +9,11 @@ You can see [here a demo](http://rawgit.com/jonataswalker/ol3-geocoder/master/ex
## Providers
The plugin supports (for now) the following providers:
-* [OSM](http://www.openstreetmap.org/)/[Nominatim](http://wiki.openstreetmap.org/wiki/Nominatim)
-* [MapQuest Geocoding API](http://open.mapquestapi.com/nominatim/) - requires KEY.
-* [Google Maps Geocoding API](https://developers.google.com/maps/documentation/geocoding/intro) - requires KEY.
-* [Photon](http://photon.komoot.de/)
+* [OSM](http://www.openstreetmap.org/)/[Nominatim](http://wiki.openstreetmap.org/wiki/Nominatim) — `'osm'`.
+* [MapQuest Geocoding API](http://open.mapquestapi.com/nominatim/) - requires KEY — `'mapquest'`.
+* [Google Maps Geocoding API](https://developers.google.com/maps/documentation/geocoding/intro) - requires KEY — `'google'`.
+* [Photon](http://photon.komoot.de/) — `'photon'`.
+* [Mapzen Search/Pelias](https://mapzen.com/projects/search) - requires KEY — `'pelias'`.
## How to use it?
##### CDN Hosted - [jsDelivr](http://www.jsdelivr.com/projects/openlayers.geocoder)
@@ -60,7 +61,7 @@ geocoder.on('addresschosen', function(evt){
Maybe later we will have other types like `'reverse'`. So for now just pass `'nominatim'`.
###### `options` is an object with the following possible properties:
-* `provider` : `'osm'` (default), `'mapquest'`, `'google'`, `'photon'`; Your preferable provider;
+* `provider` : `'osm'` (default), `'mapquest'`, `'google'`, `'photon'`, `'pelias'`; Your preferable provider;
* `key` : ''; API Key if required;
* `placeholder` : 'Search for an address'; Placeholder for text input;
* `featureStyle`: `ol.style.Style`; Feature style;
diff --git a/build/.jshintrc b/build/.jshintrc
new file mode 100644
index 0000000..fefa45c
--- /dev/null
+++ b/build/.jshintrc
@@ -0,0 +1,4 @@
+{
+ "laxbreak": true,
+ "-W002": true
+}
diff --git a/build/ol3-geocoder-debug.js b/build/ol3-geocoder-debug.js
index e1e53d8..f5130dc 100644
--- a/build/ol3-geocoder-debug.js
+++ b/build/ol3-geocoder-debug.js
@@ -53,7 +53,6 @@
});
var defaults = {
provider: 'osm',
- key: '',
placeholder: 'Search for an address',
featureStyle: Geocoder.Nominatim.featureStyle,
lang: 'en-US',
@@ -65,16 +64,15 @@
this.options = utils.mergeOptions(defaults, opt_options);
this.options.provider = this.options.provider.toLowerCase();
this.constants = {
- class_container: 'ol-geocoder',
- expanded_class: 'ol-geocoder-search-expanded',
road: 'ol-geocoder-road',
city: 'ol-geocoder-city',
- country: 'ol-geocoder-country'
+ country: 'ol-geocoder-country',
+ class_container: 'ol-geocoder',
+ expanded_class: 'ol-geocoder-search-expanded'
};
- var container = this.createControl();
+ this.createControl();
this.els = Geocoder.Nominatim.elements;
-
this.setListeners();
return this;
};
@@ -104,8 +102,7 @@
var
this_ = this,
openSearch = function() {
- if (utils.hasClass(this_.els.control,
- this_.constants.expanded_class)) {
+ if (utils.hasClass(this_.els.control, this_.constants.expanded_class)) {
this_.collapse();
} else {
this_.expand();
@@ -125,7 +122,7 @@
utils.removeClass(this.els.input_search, 'ol-geocoder-loading');
utils.addClass(this.els.control, this.constants.expanded_class);
var input = this.els.input_search;
- window.setTimeout(function() {
+ win.setTimeout(function() {
input.focus();
}, 100);
},
@@ -173,23 +170,25 @@
switch (this_.options.provider) {
case providers_names.OSM:
case providers_names.MAPQUEST:
- response__ = (response.length > 0) ?
+ response__ = response.length > 0 ?
this_.mapquestResponse(response) : undefined;
break;
+ case providers_names.PELIAS:
+ response__ = response.features.length > 0 ?
+ this_.peliasResponse(response.features) : undefined;
+ break;
case providers_names.PHOTON:
- response__ = (response.features.length > 0) ?
+ response__ = response.features.length > 0 ?
this_.photonResponse(response.features) : undefined;
break;
case providers_names.GOOGLE:
- response__ = (response.results.length > 0) ?
+ response__ = response.results.length > 0 ?
this_.googleResponse(response.results) : undefined;
break;
}
if (response__) {
this_.createList(response__);
-
var canvas = this_.geocoder.getMap().getTargetElement();
-
//one-time fire click
canvas.addEventListener('click', {
handleEvent: function(evt) {
@@ -208,10 +207,8 @@
});
},
createList: function(response) {
- var
- this_ = this,
- ul = this.els.result_container;
-
+ var this_ = this;
+ var ul = this.els.result_container;
response.forEach(function(row) {
var
address_html = this_.addressTemplate(row),
@@ -225,38 +222,35 @@
ul.appendChild(li);
});
},
- addressTemplate: function(row) {
-
- var r = row.address,
+ addressTemplate: function(r) {
+ var row = r.address,
html = [];
-
- if (r.name) {
+ if (row.name) {
html.push(
'{name}'
);
}
- if (r.road || r.building) {
+ if (row.road || row.building || row.house_number) {
html.push(
'{building} {road} {house_number}'
);
}
- if (r.city || r.town || r.village) {
+ if (row.city || row.town || row.village) {
html.push(
'{postcode} {city} {town} {village}'
);
}
- if (r.state || r.country) {
+ if (row.state || row.country) {
html.push(
'{state} {country}'
);
}
- return utils.template(html.join('
'), r);
+ return utils.template(html.join('
'), row);
},
chosen: function(place, address_html, address_obj, address_original) {
-
if (this.options.keepOpen === false) {
this.clearResults(true);
}
@@ -316,6 +310,7 @@
address: {
name: result.address.neighbourhood || '',
road: result.address.road || '',
+ postcode: result.address.postcode,
city: result.address.city || result.address.town,
state: result.address.state,
country: result.address.country
@@ -335,6 +330,7 @@
lat: feature.geometry.coordinates[1],
address: {
name: feature.properties.name,
+ postcode: feature.properties.postcode,
city: feature.properties.city,
state: feature.properties.state,
country: feature.properties.country
@@ -347,6 +343,28 @@
});
return array;
},
+ peliasResponse: function(features) {
+ var array = features.map(function(feature) {
+ return {
+ lon: feature.geometry.coordinates[0],
+ lat: feature.geometry.coordinates[1],
+ address: {
+ name: feature.properties.name,
+ house_number: feature.properties.housenumber,
+ postcode: feature.properties.postalcode,
+ road: feature.properties.street,
+ city: feature.properties.city,
+ state: feature.properties.region,
+ country: feature.properties.country
+ },
+ original: {
+ formatted: feature.properties.label,
+ details: feature.properties
+ }
+ };
+ });
+ return array;
+ },
googleResponse: function(results) {
var
name = [
@@ -361,15 +379,10 @@
'sublocality_level_5',
'intersection'
],
- city = [
- 'locality'
- ],
- state = [
- 'administrative_area_level_1'
- ],
- country = [
- 'country'
- ];
+ postcode = ['postal_code'],
+ city = ['locality'],
+ state = ['administrative_area_level_1'],
+ country = ['country'];
/*
* @param {Array} details - address_components
@@ -378,6 +391,7 @@
var parts = {
name: '',
road: '',
+ postcode: '',
city: '',
state: '',
country: ''
@@ -387,6 +401,8 @@
parts.name = detail.long_name;
} else if (utils.anyMatchInArray(detail.types, road)) {
parts.road = detail.long_name;
+ } else if (utils.anyMatchInArray(detail.types, postcode)) {
+ parts.postcode = detail.long_name;
} else if (utils.anyMatchInArray(detail.types, city)) {
parts.city = detail.long_name;
} else if (utils.anyMatchInArray(detail.types, state)) {
@@ -407,6 +423,7 @@
lat: result.geometry.location.lat,
address: {
name: details.name,
+ postcode: details.postcode,
road: details.road,
city: details.city,
state: details.state,
@@ -425,58 +442,67 @@
return this.layer.getSource();
},
addLayer: function() {
- var
- this_ = this,
- map = this.geocoder.getMap(),
- found;
+ var this_ = this,
+ found = false;
+ var map = this.geocoder.getMap();
map.getLayers().forEach(function(layer) {
- found = (layer === this_.layer) ? true : false;
+ if (layer === this_.layer) found = true;
});
- if (found === false) {
+ if (!found) {
map.addLayer(this.layer);
}
},
getProvider: function(options) {
var
+ params,
provider = Geocoder.Nominatim.providers[options.provider],
providers_names = Geocoder.Nominatim.providers.names,
requires_key = [
providers_names.MAPQUEST,
+ providers_names.PELIAS,
providers_names.GOOGLE
],
langs_photon = ['de', 'it', 'fr', 'en'];
- if (options.provider == providers_names.MAPQUEST || options.provider == providers_names.OSM) {
-
- provider.params.q = options.query;
- provider.params.limit =
- options.limit || provider.params.limit;
- provider.params['accept-language'] =
- options.lang || provider.params['accept-language'];
-
- if (options.provider == providers_names.MAPQUEST) {
- provider.params.key = options.key;
- }
-
- } else if (options.provider == providers_names.PHOTON) {
-
- provider.params.q = options.query;
- provider.params.limit =
- options.limit || provider.params.limit;
-
- options.lang = options.lang.toLowerCase();
- provider.params.lang = (langs_photon.indexOf(options.lang) > -1) ? options.lang : provider.params.lang;
-
- } else if (options.provider == providers_names.GOOGLE) {
-
+ switch (options.provider) {
+ case providers_names.OSM:
+ case providers_names.MAPQUEST:
+ params = {
+ q: options.query,
+ limit: options.limit,
+ 'accept-language': options.lang
+ };
+ provider.params = utils.mergeOptions(provider.params, params);
+ break;
+ case providers_names.PHOTON:
+ options.lang = options.lang.toLowerCase();
+ params = {
+ q: options.query,
+ limit: options.limit || provider.params.limit,
+ lang: (langs_photon.indexOf(options.lang) > -1) ?
+ options.lang : provider.params.lang
+ };
+ provider.params = utils.mergeOptions(provider.params, params);
+ break;
+ case providers_names.GOOGLE:
+ params = {
+ address: options.query,
+ language: options.lang
+ };
+ provider.params = utils.mergeOptions(provider.params, params);
+ break;
+ case providers_names.PELIAS:
+ params = {
+ text: options.query,
+ size: options.limit
+ };
+ provider.params = utils.mergeOptions(provider.params, params);
+ break;
+ }
+ if (requires_key.indexOf(options.provider) > -1) {
provider.params.key = options.key;
- provider.params.address = options.query;
- provider.params.language =
- options.lang || provider.params.language;
-
}
-
return provider;
}
};
@@ -493,7 +519,8 @@
OSM: 'osm',
MAPQUEST: 'mapquest',
GOOGLE: 'google',
- PHOTON: 'photon'
+ PHOTON: 'photon',
+ PELIAS: 'pelias'
},
osm: {
url: 'http://nominatim.openstreetmap.org/search/',
@@ -524,6 +551,14 @@
language: 'en-US'
}
},
+ pelias: {
+ url: 'https://search.mapzen.com/v1/search',
+ params: {
+ key: '',
+ text: '',
+ size: 10
+ }
+ },
photon: {
url: 'http://photon.komoot.de/api/',
params: {
@@ -560,8 +595,7 @@
'',
'',
+ ' placeholder="Search">',
'',
'
'
].join('');
@@ -647,80 +681,114 @@
'EPSG:3857', 'EPSG:4326'
);
},
+ isNumeric: function(str) {
+ return /^\d+$/.test(str);
+ },
classRegex: function(classname) {
return new RegExp('(^|\\s+)' + classname + '(\\s+|$)');
},
- _addClass: function(el, c) {
- if (el.classList)
- el.classList.add(c);
- else
- el.className = (el.className + ' ' + c).trim();
- },
- addClass: function(el, classname) {
- if (Array.isArray(el)) {
- el.forEach(function(each) {
+ /**
+ * @param {Element|Array} element DOM node or array of nodes.
+ * @param {String|Array} classname Class or array of classes.
+ * For example: 'class1 class2' or ['class1', 'class2']
+ * @param {Number|undefined} timeout Timeout to remove a class.
+ */
+ addClass: function(element, classname, timeout) {
+ if (Array.isArray(element)) {
+ element.forEach(function(each) {
utils.addClass(each, classname);
});
return;
}
- //classname can be ['class1', 'class2'] or 'class1 class2'
var
- array = (Array.isArray(classname)) ?
- classname : classname.split(utils.whiteSpaceRegex),
+ array = (Array.isArray(classname)) ? classname : classname.split(/\s+/),
i = array.length;
while (i--) {
- if (!utils.hasClass(el, array[i])) {
- utils._addClass(el, array[i]);
+ if (!utils.hasClass(element, array[i])) {
+ utils._addClass(element, array[i], timeout);
}
}
},
- _removeClass: function(el, c) {
+ _addClass: function(el, c, timeout) {
+ // use native if available
if (el.classList) {
- el.classList.remove(c);
+ el.classList.add(c);
} else {
- el.className =
- (el.className.replace(utils.classReg(c), ' ')).trim();
+ el.className = (el.className + ' ' + c).trim();
+ }
+
+ if (timeout && utils.isNumeric(timeout)) {
+ win.setTimeout(function() {
+ utils._removeClass(el, c);
+ }, timeout);
}
},
- removeClass: function(el, classname) {
- if (Array.isArray(el)) {
- el.forEach(function(each) {
- utils.removeClass(each, classname);
+ /**
+ * @param {Element|Array} element DOM node or array of nodes.
+ * @param {String|Array} classname Class or array of classes.
+ * For example: 'class1 class2' or ['class1', 'class2']
+ * @param {Number|undefined} timeout Timeout to add a class.
+ */
+ removeClass: function(element, classname, timeout) {
+ if (Array.isArray(element)) {
+ element.forEach(function(each) {
+ utils.removeClass(each, classname, timeout);
});
return;
}
- //classname can be ['class1', 'class2'] or 'class1 class2'
var
- array = (Array.isArray(classname)) ?
- classname : classname.split(utils.whiteSpaceRegex),
+ array = (Array.isArray(classname)) ? classname : classname.split(/\s+/),
i = array.length;
while (i--) {
- if (utils.hasClass(el, array[i])) {
- utils._removeClass(el, array[i]);
+ if (utils.hasClass(element, array[i])) {
+ utils._removeClass(element, array[i], timeout);
}
}
},
- hasClass: function(el, c) {
- return (el.classList) ?
- el.classList.contains(c) : utils.classReg(c).test(el.className);
+ _removeClass: function(el, c, timeout) {
+ if (el.classList) {
+ el.classList.remove(c);
+ } else {
+ el.className = (el.className.replace(utils.classRegex(c), ' ')).trim();
+ }
+ if (timeout && utils.isNumeric(timeout)) {
+ win.setTimeout(function() {
+ utils._addClass(el, c);
+ }, timeout);
+ }
},
- toggleClass: function(el, c) {
- if (Array.isArray(el)) {
- el.forEach(function(each) {
- utils.toggleClass(each, c);
+ /**
+ * @param {Element} element DOM node.
+ * @param {String} classname Classname.
+ * @return {Boolean}
+ */
+ hasClass: function(element, c) {
+ // use native if available
+ return (element.classList) ?
+ element.classList.contains(c) : utils.classRegex(c).test(element.className);
+ },
+ /**
+ * @param {Element|Array} element DOM node or array of nodes.
+ * @param {String} classname Classe.
+ */
+ toggleClass: function(element, classname) {
+ if (Array.isArray(element)) {
+ element.forEach(function(each) {
+ utils.toggleClass(each, classname);
});
return;
}
- if (el.classList) {
- el.classList.toggle(c);
+ // use native if available
+ if (element.classList) {
+ element.classList.toggle(classname);
} else {
- if (utils.hasClass(el, c)) {
- utils._removeClass(el, c);
+ if (utils.hasClass(element, classname)) {
+ utils._removeClass(element, classname);
} else {
- utils._addClass(el, c);
+ utils._addClass(element, classname);
}
}
},
diff --git a/build/ol3-geocoder.js b/build/ol3-geocoder.js
index cb2413f..a725fd0 100644
--- a/build/ol3-geocoder.js
+++ b/build/ol3-geocoder.js
@@ -1 +1 @@
-(function(e,t){"use strict";this.Geocoder=function(){var a=function(e,t){r.assert("string"==typeof e,"@param `control_type` should be string type!"),r.assert("object"==typeof t||"undefined"==typeof t,"@param `opt_options` should be object|undefined type!"),e=e||"nominatim";var n=new a.Nominatim(this,t);this.layer=n.layer,ol.control.Control.call(this,{element:n.els.container})};return ol.inherits(a,ol.control.Control),a.prototype.getSource=function(){return this.layer.getSource()},a.prototype.getLayer=function(){return this.layer},function(e){e.Nominatim=function(t,a){this.geocoder=t,this.layer_name=r.randomId("geocoder-layer-"),this.layer=new ol.layer.Vector({name:this.layer_name,source:new ol.source.Vector});var n={provider:"osm",key:"",placeholder:"Search for an address",featureStyle:e.Nominatim.featureStyle,lang:"en-US",limit:5,keepOpen:!1,debug:!1};this.options=r.mergeOptions(n,a),this.options.provider=this.options.provider.toLowerCase(),this.constants={class_container:"ol-geocoder",expanded_class:"ol-geocoder-search-expanded",road:"ol-geocoder-road",city:"ol-geocoder-city",country:"ol-geocoder-country"};this.createControl();return this.els=e.Nominatim.elements,this.setListeners(),this},e.Nominatim.prototype={createControl:function(){var t=r.createElement(["div",{classname:this.constants.class_container}],e.Nominatim.html);return e.Nominatim.elements={container:t,control:t.querySelector(".ol-geocoder-search"),btn_search:t.querySelector(".ol-geocoder-btn-search"),input_search:t.querySelector(".ol-geocoder-input-search"),result_container:t.querySelector(".ol-geocoder-result")},e.Nominatim.elements.input_search.placeholder=this.options.placeholder,t},setListeners:function(){var e=this,t=function(){r.hasClass(e.els.control,e.constants.expanded_class)?e.collapse():e.expand()},a=function(t){if(13==t.keyCode){var a=r.htmlEscape(e.els.input_search.value);e.query(a)}};e.els.input_search.addEventListener("keydown",a,!1),e.els.btn_search.addEventListener("click",t,!1)},expand:function(){r.removeClass(this.els.input_search,"ol-geocoder-loading"),r.addClass(this.els.control,this.constants.expanded_class);var e=this.els.input_search;window.setTimeout(function(){e.focus()},100)},collapse:function(){this.els.input_search.value="",this.els.input_search.blur(),r.removeClass(this.els.control,this.constants.expanded_class),this.clearResults()},clearResults:function(e){e?this.collapse():r.removeAllChildren(this.els.result_container)},query:function(t){var a=this,n=this.options,o=this.els.input_search,s=e.Nominatim.providers.names,i=this.getProvider({provider:n.provider,key:n.key,query:t,lang:n.lang,limit:n.limit});this.clearResults(),r.addClass(o,"ol-geocoder-loading"),r.json(i.url,i.params).when({ready:function(e){n.debug&&console.info(e),r.removeClass(o,"ol-geocoder-loading");var t;switch(a.options.provider){case s.OSM:case s.MAPQUEST:t=e.length>0?a.mapquestResponse(e):void 0;break;case s.PHOTON:t=e.features.length>0?a.photonResponse(e.features):void 0;break;case s.GOOGLE:t=e.results.length>0?a.googleResponse(e.results):void 0}if(t){a.createList(t);var i=a.geocoder.getMap().getTargetElement();i.addEventListener("click",{handleEvent:function(e){a.clearResults(!0),i.removeEventListener(e.type,this,!1)}},!1)}},error:function(){r.removeClass(o,"ol-geocoder-loading");var e=r.createElement("li","Error! No internet connection?
");a.els.result_container.appendChild(e)}})},createList:function(e){var t=this,a=this.els.result_container;e.forEach(function(e){var n=t.addressTemplate(e),o=''+n+"",s=r.createElement("li",o);s.addEventListener("click",function(r){r.preventDefault(),t.chosen(e,n,e.address,e.original)},!1),a.appendChild(s)})},addressTemplate:function(e){var t=e.address,a=[];return t.name&&a.push('{name}'),(t.road||t.building)&&a.push('{building} {road} {house_number}'),(t.city||t.town||t.village)&&a.push('{postcode} {city} {town} {village}'),(t.state||t.country)&&a.push('{state} {country}'),r.template(a.join("
"),t)},chosen:function(e,t,a,n){this.options.keepOpen===!1&&this.clearResults(!0);var o=this.geocoder.getMap(),s=r.to3857([e.lon,e.lat]),i=2.388657133911758,l=500,c={coord:s,address_html:t,address_obj:a,address_original:n},d=ol.animation.pan({duration:l,source:o.getView().getCenter()}),u=ol.animation.zoom({duration:l,resolution:o.getView().getResolution()});o.beforeRender(d,u),o.getView().setCenter(s),o.getView().setResolution(i),this.createFeature(c)},createFeature:function(t){var a=new ol.Feature({address_html:t.address_html,address_obj:t.address_obj,address_original:t.address_original,geometry:new ol.geom.Point(t.coord)}),n=r.randomId("geocoder-ft-"),o=this.options.featureStyle||e.Nominatim.featureStyle;this.addLayer(),a.setStyle(o),a.setId(n),this.getSource().addFeature(a),this.geocoder.dispatchEvent({type:e.EventType.ADDRESSCHOSEN,feature:a,coordinate:t.coord})},mapquestResponse:function(e){var t=e.map(function(e){return{lon:e.lon,lat:e.lat,address:{name:e.address.neighbourhood||"",road:e.address.road||"",city:e.address.city||e.address.town,state:e.address.state,country:e.address.country},original:{formatted:e.display_name,details:e.address}}});return t},photonResponse:function(e){var t=e.map(function(e){return{lon:e.geometry.coordinates[0],lat:e.geometry.coordinates[1],address:{name:e.properties.name,city:e.properties.city,state:e.properties.state,country:e.properties.country},original:{formatted:e.properties.name,details:e.properties}}});return t},googleResponse:function(e){var t=["point_of_interest","establishment","natural_feature","airport"],a=["street_address","route","sublocality_level_5","intersection"],n=["locality"],o=["administrative_area_level_1"],s=["country"],i=function(e){var i={name:"",road:"",city:"",state:"",country:""};return e.forEach(function(e){r.anyMatchInArray(e.types,t)?i.name=e.long_name:r.anyMatchInArray(e.types,a)?i.road=e.long_name:r.anyMatchInArray(e.types,n)?i.city=e.long_name:r.anyMatchInArray(e.types,o)?i.state=e.long_name:r.anyMatchInArray(e.types,s)&&(i.country=e.long_name)}),i},l=[];return e.forEach(function(e){var t=i(e.address_components);r.anyItemHasValue(t)&&l.push({lon:e.geometry.location.lng,lat:e.geometry.location.lat,address:{name:t.name,road:t.road,city:t.city,state:t.state,country:t.country},original:{formatted:e.formatted_address,details:e.address_components}})}),l},getSource:function(){return this.layer.getSource()},addLayer:function(){var e,t=this,r=this.geocoder.getMap();r.getLayers().forEach(function(r){e=r===t.layer?!0:!1}),e===!1&&r.addLayer(this.layer)},getProvider:function(t){var r=e.Nominatim.providers[t.provider],a=e.Nominatim.providers.names,n=([a.MAPQUEST,a.GOOGLE],["de","it","fr","en"]);return t.provider==a.MAPQUEST||t.provider==a.OSM?(r.params.q=t.query,r.params.limit=t.limit||r.params.limit,r.params["accept-language"]=t.lang||r.params["accept-language"],t.provider==a.MAPQUEST&&(r.params.key=t.key)):t.provider==a.PHOTON?(r.params.q=t.query,r.params.limit=t.limit||r.params.limit,t.lang=t.lang.toLowerCase(),r.params.lang=n.indexOf(t.lang)>-1?t.lang:r.params.lang):t.provider==a.GOOGLE&&(r.params.key=t.key,r.params.address=t.query,r.params.language=t.lang||r.params.language),r}},e.EventType={ADDRESSCHOSEN:"addresschosen"},e.Nominatim.elements={},e.Nominatim.providers={names:{OSM:"osm",MAPQUEST:"mapquest",GOOGLE:"google",PHOTON:"photon"},osm:{url:"http://nominatim.openstreetmap.org/search/",params:{format:"json",q:"",addressdetails:1,limit:10,"accept-language":"en-US"}},mapquest:{url:"http://open.mapquestapi.com/nominatim/v1/search.php",params:{key:"",format:"json",q:"",addressdetails:1,limit:10,"accept-language":"en-US"}},google:{url:"https://maps.googleapis.com/maps/api/geocode/json",params:{key:"",address:"",language:"en-US"}},photon:{url:"http://photon.komoot.de/api/",params:{q:"",limit:10,lang:"en"}}},e.Nominatim.featureStyle=[new ol.style.Style({image:new ol.style.Icon({scale:.7,anchor:[.5,1],src:"//cdn.rawgit.com/jonataswalker/map-utils/master/images/marker.png"}),zIndex:5}),new ol.style.Style({image:new ol.style.Circle({fill:new ol.style.Fill({color:[235,235,235,1]}),stroke:new ol.style.Stroke({color:[0,0,0,1]}),radius:5}),zIndex:4})],e.Nominatim.html=['','','","
",''].join("")}(a),function(e,t){var n=function(){var t=!1;if(e.XMLHttpRequest)t=new XMLHttpRequest;else if(e.ActiveXObject)try{t=new ActiveXObject("Msxml2.XMLHTTP")}catch(e){try{t=new ActiveXObject("Microsoft.XMLHTTP")}catch(e){t=!1}}return t},o=function(e,t){if(t&&"object"==typeof t){var a=r.toQueryString(t);e+=(/\?/.test(e)?"&":"?")+a}return e};a.Utils={whiteSpaceRegex:/\s+/,toQueryString:function(e){return Object.keys(e).reduce(function(t,a){return t.push("object"==typeof e[a]?r.toQueryString(e[a]):encodeURIComponent(a)+"="+encodeURIComponent(e[a])),t},[]).join("&")},json:function(e,t){var r=n(),a={},s=function(){200===r.status&&a.ready.call(void 0,JSON.parse(r.response))},i=function(){console.info("Cannot XHR "+JSON.stringify(e))};return e=o(e,t),r.open("GET",e,!0),r.setRequestHeader("Accept","application/json"),r.onload=s,r.onerror=i,r.onprogress=onprogress,r.send(null),{when:function(e){a.ready=e.ready}}},randomId:function(e){var t=(new Date).getTime().toString(36);return e?e+t:t},to3857:function(e){return ol.proj.transform([parseFloat(e[0]),parseFloat(e[1])],"EPSG:4326","EPSG:3857")},to4326:function(e){return ol.proj.transform([parseFloat(e[0]),parseFloat(e[1])],"EPSG:3857","EPSG:4326")},classRegex:function(e){return new RegExp("(^|\\s+)"+e+"(\\s+|$)")},_addClass:function(e,t){e.classList?e.classList.add(t):e.className=(e.className+" "+t).trim()},addClass:function(e,t){if(Array.isArray(e))return void e.forEach(function(e){r.addClass(e,t)});for(var a=Array.isArray(t)?t:t.split(r.whiteSpaceRegex),n=a.length;n--;)r.hasClass(e,a[n])||r._addClass(e,a[n])},_removeClass:function(e,t){e.classList?e.classList.remove(t):e.className=e.className.replace(r.classReg(t)," ").trim()},removeClass:function(e,t){if(Array.isArray(e))return void e.forEach(function(e){r.removeClass(e,t)});for(var a=Array.isArray(t)?t:t.split(r.whiteSpaceRegex),n=a.length;n--;)r.hasClass(e,a[n])&&r._removeClass(e,a[n])},hasClass:function(e,t){return e.classList?e.classList.contains(t):r.classReg(t).test(e.className)},toggleClass:function(e,t){return Array.isArray(e)?void e.forEach(function(e){r.toggleClass(e,t)}):void(e.classList?e.classList.toggle(t):r.hasClass(e,t)?r._removeClass(e,t):r._addClass(e,t))},$:function(e){return e="#"===e[0]?e.substr(1,e.length):e,t.getElementById(e)},isElement:function(t){return"HTMLElement"in e?!!t&&t instanceof HTMLElement:!!t&&"object"==typeof t&&1===t.nodeType&&!!t.nodeName},getAllChildren:function(e,t){return[].slice.call(e.getElementsByTagName(t))},isEmpty:function(e){return!e||0===e.length},emptyArray:function(e){for(;e.length;)e.pop()},anyMatchInArray:function(e,t){return e.some(function(e){return t.indexOf(e)>=0})},everyMatchInArray:function(e,t){return t.every(function(t){return e.indexOf(t)>=0})},anyItemHasValue:function(e){var t=!1;for(var a in e)r.isEmpty(e[a])||(t=!0);return t},removeAllChildren:function(e){for(;e.firstChild;)e.removeChild(e.firstChild)},removeAll:function(e){for(var t;t=e[0];)t.parentNode.removeChild(t)},getChildren:function(e,t){return[].filter.call(e.childNodes,function(e){return t?1==e.nodeType&&e.tagName.toLowerCase()==t:1==e.nodeType})},template:function(e,t){var r=this;return e.replace(/\{ *([\w_-]+) *\}/g,function(e,a){var n=void 0===t[a]?"":t[a];return r.htmlEscape(n)})},htmlEscape:function(e){return String(e).replace(/&/g,"&").replace(//g,">").replace(/"/g,""").replace(/'/g,"'")},mergeOptions:function(e,t){var r={};for(var a in e)r[a]=e[a];for(var n in t)r[n]=t[n];return r},createElement:function(e,r){var a;if(Array.isArray(e)){if(a=t.createElement(e[0]),e[1].id&&(a.id=e[1].id),e[1].classname&&(a.className=e[1].classname),e[1].attr){var n=e[1].attr;if(Array.isArray(n))for(var o=-1;++o0?o.mapquestResponse(e):void 0;break;case s.PELIAS:t=e.features.length>0?o.peliasResponse(e.features):void 0;break;case s.PHOTON:t=e.features.length>0?o.photonResponse(e.features):void 0;break;case s.GOOGLE:t=e.results.length>0?o.googleResponse(e.results):void 0}if(t){o.createList(t);var i=o.geocoder.getMap().getTargetElement();i.addEventListener("click",{handleEvent:function(e){o.clearResults(!0),i.removeEventListener(e.type,this,!1)}},!1)}},error:function(){r.removeClass(a,"ol-geocoder-loading");var e=r.createElement("li","Error! No internet connection?
");o.els.result_container.appendChild(e)}})},createList:function(e){var t=this,o=this.els.result_container;e.forEach(function(e){var n=t.addressTemplate(e),a=''+n+"",s=r.createElement("li",a);s.addEventListener("click",function(r){r.preventDefault(),t.chosen(e,n,e.address,e.original)},!1),o.appendChild(s)})},addressTemplate:function(e){var t=e.address,o=[];return t.name&&o.push('{name}'),(t.road||t.building||t.house_number)&&o.push('{building} {road} {house_number}'),(t.city||t.town||t.village)&&o.push('{postcode} {city} {town} {village}'),(t.state||t.country)&&o.push('{state} {country}'),r.template(o.join("
"),t)},chosen:function(e,t,o,n){this.options.keepOpen===!1&&this.clearResults(!0);var a=this.geocoder.getMap(),s=r.to3857([e.lon,e.lat]),i=2.388657133911758,l=500,c={coord:s,address_html:t,address_obj:o,address_original:n},d=ol.animation.pan({duration:l,source:a.getView().getCenter()}),u=ol.animation.zoom({duration:l,resolution:a.getView().getResolution()});a.beforeRender(d,u),a.getView().setCenter(s),a.getView().setResolution(i),this.createFeature(c)},createFeature:function(e){var o=new ol.Feature({address_html:e.address_html,address_obj:e.address_obj,address_original:e.address_original,geometry:new ol.geom.Point(e.coord)}),n=r.randomId("geocoder-ft-"),a=this.options.featureStyle||t.Nominatim.featureStyle;this.addLayer(),o.setStyle(a),o.setId(n),this.getSource().addFeature(o),this.geocoder.dispatchEvent({type:t.EventType.ADDRESSCHOSEN,feature:o,coordinate:e.coord})},mapquestResponse:function(e){var t=e.map(function(e){return{lon:e.lon,lat:e.lat,address:{name:e.address.neighbourhood||"",road:e.address.road||"",postcode:e.address.postcode,city:e.address.city||e.address.town,state:e.address.state,country:e.address.country},original:{formatted:e.display_name,details:e.address}}});return t},photonResponse:function(e){var t=e.map(function(e){return{lon:e.geometry.coordinates[0],lat:e.geometry.coordinates[1],address:{name:e.properties.name,postcode:e.properties.postcode,city:e.properties.city,state:e.properties.state,country:e.properties.country},original:{formatted:e.properties.name,details:e.properties}}});return t},peliasResponse:function(e){var t=e.map(function(e){return{lon:e.geometry.coordinates[0],lat:e.geometry.coordinates[1],address:{name:e.properties.name,house_number:e.properties.housenumber,postcode:e.properties.postalcode,road:e.properties.street,city:e.properties.city,state:e.properties.region,country:e.properties.country},original:{formatted:e.properties.label,details:e.properties}}});return t},googleResponse:function(e){var t=["point_of_interest","establishment","natural_feature","airport"],o=["street_address","route","sublocality_level_5","intersection"],n=["postal_code"],a=["locality"],s=["administrative_area_level_1"],i=["country"],l=function(e){var l={name:"",road:"",postcode:"",city:"",state:"",country:""};return e.forEach(function(e){r.anyMatchInArray(e.types,t)?l.name=e.long_name:r.anyMatchInArray(e.types,o)?l.road=e.long_name:r.anyMatchInArray(e.types,n)?l.postcode=e.long_name:r.anyMatchInArray(e.types,a)?l.city=e.long_name:r.anyMatchInArray(e.types,s)?l.state=e.long_name:r.anyMatchInArray(e.types,i)&&(l.country=e.long_name)}),l},c=[];return e.forEach(function(e){var t=l(e.address_components);r.anyItemHasValue(t)&&c.push({lon:e.geometry.location.lng,lat:e.geometry.location.lat,address:{name:t.name,postcode:t.postcode,road:t.road,city:t.city,state:t.state,country:t.country},original:{formatted:e.formatted_address,details:e.address_components}})}),c},getSource:function(){return this.layer.getSource()},addLayer:function(){var e=this,t=!1,r=this.geocoder.getMap();r.getLayers().forEach(function(r){r===e.layer&&(t=!0)}),t||r.addLayer(this.layer)},getProvider:function(e){var o,n=t.Nominatim.providers[e.provider],a=t.Nominatim.providers.names,s=[a.MAPQUEST,a.PELIAS,a.GOOGLE],i=["de","it","fr","en"];switch(e.provider){case a.OSM:case a.MAPQUEST:o={q:e.query,limit:e.limit,"accept-language":e.lang},n.params=r.mergeOptions(n.params,o);break;case a.PHOTON:e.lang=e.lang.toLowerCase(),o={q:e.query,limit:e.limit||n.params.limit,lang:i.indexOf(e.lang)>-1?e.lang:n.params.lang},n.params=r.mergeOptions(n.params,o);break;case a.GOOGLE:o={address:e.query,language:e.lang},n.params=r.mergeOptions(n.params,o);break;case a.PELIAS:o={text:e.query,size:e.limit},n.params=r.mergeOptions(n.params,o)}return s.indexOf(e.provider)>-1&&(n.params.key=e.key),n}},t.EventType={ADDRESSCHOSEN:"addresschosen"},t.Nominatim.elements={},t.Nominatim.providers={names:{OSM:"osm",MAPQUEST:"mapquest",GOOGLE:"google",PHOTON:"photon",PELIAS:"pelias"},osm:{url:"http://nominatim.openstreetmap.org/search/",params:{format:"json",q:"",addressdetails:1,limit:10,"accept-language":"en-US"}},mapquest:{url:"http://open.mapquestapi.com/nominatim/v1/search.php",params:{key:"",format:"json",q:"",addressdetails:1,limit:10,"accept-language":"en-US"}},google:{url:"https://maps.googleapis.com/maps/api/geocode/json",params:{key:"",address:"",language:"en-US"}},pelias:{url:"https://search.mapzen.com/v1/search",params:{key:"",text:"",size:10}},photon:{url:"http://photon.komoot.de/api/",params:{q:"",limit:10,lang:"en"}}},t.Nominatim.featureStyle=[new ol.style.Style({image:new ol.style.Icon({scale:.7,anchor:[.5,1],src:"//cdn.rawgit.com/jonataswalker/map-utils/master/images/marker.png"}),zIndex:5}),new ol.style.Style({image:new ol.style.Circle({fill:new ol.style.Fill({color:[235,235,235,1]}),stroke:new ol.style.Stroke({color:[0,0,0,1]}),radius:5}),zIndex:4})],t.Nominatim.html=['','','',"
",''].join("")}(o),function(e,t){var n=function(){var t=!1;if(e.XMLHttpRequest)t=new XMLHttpRequest;else if(e.ActiveXObject)try{t=new ActiveXObject("Msxml2.XMLHTTP")}catch(e){try{t=new ActiveXObject("Microsoft.XMLHTTP")}catch(e){t=!1}}return t},a=function(e,t){if(t&&"object"==typeof t){var o=r.toQueryString(t);e+=(/\?/.test(e)?"&":"?")+o}return e};o.Utils={whiteSpaceRegex:/\s+/,toQueryString:function(e){return Object.keys(e).reduce(function(t,o){return t.push("object"==typeof e[o]?r.toQueryString(e[o]):encodeURIComponent(o)+"="+encodeURIComponent(e[o])),t},[]).join("&")},json:function(e,t){var r=n(),o={},s=function(){200===r.status&&o.ready.call(void 0,JSON.parse(r.response))},i=function(){console.info("Cannot XHR "+JSON.stringify(e))};return e=a(e,t),r.open("GET",e,!0),r.setRequestHeader("Accept","application/json"),r.onload=s,r.onerror=i,r.onprogress=onprogress,r.send(null),{when:function(e){o.ready=e.ready}}},randomId:function(e){var t=(new Date).getTime().toString(36);return e?e+t:t},to3857:function(e){return ol.proj.transform([parseFloat(e[0]),parseFloat(e[1])],"EPSG:4326","EPSG:3857")},to4326:function(e){return ol.proj.transform([parseFloat(e[0]),parseFloat(e[1])],"EPSG:3857","EPSG:4326")},isNumeric:function(e){return/^\d+$/.test(e)},classRegex:function(e){return new RegExp("(^|\\s+)"+e+"(\\s+|$)")},addClass:function(e,t,o){if(Array.isArray(e))return void e.forEach(function(e){r.addClass(e,t)});for(var n=Array.isArray(t)?t:t.split(/\s+/),a=n.length;a--;)r.hasClass(e,n[a])||r._addClass(e,n[a],o)},_addClass:function(t,o,n){t.classList?t.classList.add(o):t.className=(t.className+" "+o).trim(),n&&r.isNumeric(n)&&e.setTimeout(function(){r._removeClass(t,o)},n)},removeClass:function(e,t,o){if(Array.isArray(e))return void e.forEach(function(e){r.removeClass(e,t,o)});for(var n=Array.isArray(t)?t:t.split(/\s+/),a=n.length;a--;)r.hasClass(e,n[a])&&r._removeClass(e,n[a],o)},_removeClass:function(t,o,n){t.classList?t.classList.remove(o):t.className=t.className.replace(r.classRegex(o)," ").trim(),n&&r.isNumeric(n)&&e.setTimeout(function(){r._addClass(t,o)},n)},hasClass:function(e,t){return e.classList?e.classList.contains(t):r.classRegex(t).test(e.className)},toggleClass:function(e,t){return Array.isArray(e)?void e.forEach(function(e){r.toggleClass(e,t)}):void(e.classList?e.classList.toggle(t):r.hasClass(e,t)?r._removeClass(e,t):r._addClass(e,t))},$:function(e){return e="#"===e[0]?e.substr(1,e.length):e,t.getElementById(e)},isElement:function(t){return"HTMLElement"in e?!!t&&t instanceof HTMLElement:!!t&&"object"==typeof t&&1===t.nodeType&&!!t.nodeName},getAllChildren:function(e,t){return[].slice.call(e.getElementsByTagName(t))},isEmpty:function(e){return!e||0===e.length},emptyArray:function(e){for(;e.length;)e.pop()},anyMatchInArray:function(e,t){return e.some(function(e){return t.indexOf(e)>=0})},everyMatchInArray:function(e,t){return t.every(function(t){return e.indexOf(t)>=0})},anyItemHasValue:function(e){var t=!1;for(var o in e)r.isEmpty(e[o])||(t=!0);return t},removeAllChildren:function(e){for(;e.firstChild;)e.removeChild(e.firstChild)},removeAll:function(e){for(var t;t=e[0];)t.parentNode.removeChild(t)},getChildren:function(e,t){return[].filter.call(e.childNodes,function(e){return t?1==e.nodeType&&e.tagName.toLowerCase()==t:1==e.nodeType})},template:function(e,t){var r=this;return e.replace(/\{ *([\w_-]+) *\}/g,function(e,o){var n=void 0===t[o]?"":t[o];return r.htmlEscape(n)})},htmlEscape:function(e){return String(e).replace(/&/g,"&").replace(//g,">").replace(/"/g,""").replace(/'/g,"'")},mergeOptions:function(e,t){var r={};for(var o in e)r[o]=e[o];for(var n in t)r[n]=t[n];return r},createElement:function(e,r){var o;if(Array.isArray(e)){if(o=t.createElement(e[0]),e[1].id&&(o.id=e[1].id),e[1].classname&&(o.className=e[1].classname),e[1].attr){var n=e[1].attr;if(Array.isArray(n))for(var a=-1;++a
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/package.json b/package.json
index 9897c05..8c16308 100644
--- a/package.json
+++ b/package.json
@@ -1,6 +1,6 @@
{
"name": "ol3-geocoder",
- "version": "1.4.1",
+ "version": "1.5.0",
"description": "A geocoder extension for OpenLayers 3.",
"main": "ol3-geocoder.js",
"author": "Jonatas Walker",
diff --git a/src/nominatim.js b/src/nominatim.js
index ed36646..9cca5ab 100644
--- a/src/nominatim.js
+++ b/src/nominatim.js
@@ -9,7 +9,6 @@
});
var defaults = {
provider: 'osm',
- key: '',
placeholder: 'Search for an address',
featureStyle: Geocoder.Nominatim.featureStyle,
lang: 'en-US',
@@ -21,16 +20,15 @@
this.options = utils.mergeOptions(defaults, opt_options);
this.options.provider = this.options.provider.toLowerCase();
this.constants = {
- class_container: 'ol-geocoder',
- expanded_class: 'ol-geocoder-search-expanded',
road: 'ol-geocoder-road',
city: 'ol-geocoder-city',
- country: 'ol-geocoder-country'
+ country: 'ol-geocoder-country',
+ class_container: 'ol-geocoder',
+ expanded_class: 'ol-geocoder-search-expanded'
};
- var container = this.createControl();
+ this.createControl();
this.els = Geocoder.Nominatim.elements;
-
this.setListeners();
return this;
};
@@ -58,12 +56,11 @@
var
this_ = this,
openSearch = function() {
- if(utils.hasClass(this_.els.control,
- this_.constants.expanded_class)) {
- this_.collapse();
- } else {
- this_.expand();
- }
+ if(utils.hasClass(this_.els.control, this_.constants.expanded_class)){
+ this_.collapse();
+ } else {
+ this_.expand();
+ }
},
query = function(evt){
if (evt.keyCode == 13){ //enter key
@@ -80,7 +77,7 @@
utils.removeClass(this.els.input_search, 'ol-geocoder-loading');
utils.addClass(this.els.control, this.constants.expanded_class);
var input = this.els.input_search;
- window.setTimeout(function(){
+ win.setTimeout(function(){
input.focus();
}, 100);
},
@@ -111,7 +108,7 @@
limit: options.limit
})
;
-
+
this.clearResults();
utils.addClass(input, 'ol-geocoder-loading');
@@ -129,25 +126,25 @@
switch (this_.options.provider) {
case providers_names.OSM:
case providers_names.MAPQUEST:
- response__ = (response.length > 0) ?
+ response__ = response.length > 0 ?
this_.mapquestResponse(response) : undefined;
break;
+ case providers_names.PELIAS:
+ response__ = response.features.length > 0 ?
+ this_.peliasResponse(response.features) : undefined;
+ break;
case providers_names.PHOTON:
- response__ = (response.features.length > 0) ?
- this_.photonResponse(response.features)
- : undefined;
+ response__ = response.features.length > 0 ?
+ this_.photonResponse(response.features) : undefined;
break;
case providers_names.GOOGLE:
- response__ = (response.results.length > 0) ?
- this_.googleResponse(response.results)
- : undefined;
+ response__ = response.results.length > 0 ?
+ this_.googleResponse(response.results) : undefined;
break;
}
if(response__){
this_.createList(response__);
-
var canvas = this_.geocoder.getMap().getTargetElement();
-
//one-time fire click
canvas.addEventListener('click', {
handleEvent: function (evt) {
@@ -166,11 +163,8 @@
});
},
createList: function(response){
- var
- this_ = this,
- ul = this.els.result_container
- ;
-
+ var this_ = this;
+ var ul = this.els.result_container;
response.forEach(function(row) {
var
address_html = this_.addressTemplate(row),
@@ -185,37 +179,34 @@
ul.appendChild(li);
});
},
- addressTemplate: function(row){
-
- var r = row.address, html = [];
-
- if (r.name) {
+ addressTemplate: function(r){
+ var row = r.address, html = [];
+ if (row.name) {
html.push(
'{name}'
);
}
- if (r.road || r.building) {
+ if (row.road || row.building || row.house_number) {
html.push(
'{building} {road} {house_number}'
);
}
- if (r.city || r.town || r.village) {
+ if (row.city || row.town || row.village) {
html.push(
'{postcode} {city} {town} {village}'
);
}
- if (r.state || r.country) {
+ if (row.state || row.country) {
html.push(
'{state} {country}'
);
}
- return utils.template(html.join('
'), r);
+ return utils.template(html.join('
'), row);
},
chosen: function(place, address_html, address_obj, address_original){
-
if(this.options.keepOpen === false){
this.clearResults(true);
}
@@ -276,6 +267,7 @@
address: {
name: result.address.neighbourhood || '',
road: result.address.road || '',
+ postcode: result.address.postcode,
city: result.address.city || result.address.town,
state: result.address.state,
country: result.address.country
@@ -295,6 +287,7 @@
lat: feature.geometry.coordinates[1],
address: {
name: feature.properties.name,
+ postcode: feature.properties.postcode,
city: feature.properties.city,
state: feature.properties.state,
country: feature.properties.country
@@ -307,6 +300,28 @@
});
return array;
},
+ peliasResponse: function(features){
+ var array = features.map(function(feature){
+ return {
+ lon: feature.geometry.coordinates[0],
+ lat: feature.geometry.coordinates[1],
+ address: {
+ name: feature.properties.name,
+ house_number: feature.properties.housenumber,
+ postcode: feature.properties.postalcode,
+ road: feature.properties.street,
+ city: feature.properties.city,
+ state: feature.properties.region,
+ country: feature.properties.country
+ },
+ original: {
+ formatted: feature.properties.label,
+ details: feature.properties
+ }
+ };
+ });
+ return array;
+ },
googleResponse: function(results){
var
name = [
@@ -321,15 +336,10 @@
'sublocality_level_5',
'intersection'
],
- city = [
- 'locality'
- ],
- state = [
- 'administrative_area_level_1'
- ],
- country = [
- 'country'
- ]
+ postcode = [ 'postal_code' ],
+ city = [ 'locality' ],
+ state = [ 'administrative_area_level_1' ],
+ country = [ 'country' ]
;
/*
@@ -339,6 +349,7 @@
var parts = {
name: '',
road: '',
+ postcode: '',
city: '',
state: '',
country: ''
@@ -348,6 +359,8 @@
parts.name = detail.long_name;
} else if(utils.anyMatchInArray(detail.types, road)){
parts.road = detail.long_name;
+ } else if(utils.anyMatchInArray(detail.types, postcode)){
+ parts.postcode = detail.long_name;
} else if(utils.anyMatchInArray(detail.types, city)){
parts.city = detail.long_name;
} else if(utils.anyMatchInArray(detail.types, state)){
@@ -368,6 +381,7 @@
lat: result.geometry.location.lat,
address: {
name: details.name,
+ postcode: details.postcode,
road: details.road,
city: details.city,
state: details.state,
@@ -385,64 +399,68 @@
getSource: function() {
return this.layer.getSource();
},
- addLayer: function(){
- var
- this_ = this,
- map = this.geocoder.getMap(),
- found
- ;
+ addLayer: function() {
+ var this_ = this, found = false;
+ var map = this.geocoder.getMap();
map.getLayers().forEach(function(layer){
- found = (layer === this_.layer) ? true : false;
+ if (layer === this_.layer) found = true;
});
- if(found === false){
+ if (!found) {
map.addLayer(this.layer);
}
},
- getProvider: function(options){
+ getProvider: function(options) {
var
+ params,
provider = Geocoder.Nominatim.providers[options.provider],
providers_names = Geocoder.Nominatim.providers.names,
requires_key = [
providers_names.MAPQUEST,
+ providers_names.PELIAS,
providers_names.GOOGLE
],
langs_photon = ['de', 'it', 'fr', 'en']
;
- if(options.provider == providers_names.MAPQUEST
- || options.provider == providers_names.OSM){
-
- provider.params.q = options.query;
- provider.params.limit =
- options.limit || provider.params.limit;
- provider.params['accept-language'] =
- options.lang || provider.params['accept-language'];
-
- if(options.provider == providers_names.MAPQUEST){
- provider.params.key = options.key;
- }
-
- } else if(options.provider == providers_names.PHOTON){
-
- provider.params.q = options.query;
- provider.params.limit =
- options.limit || provider.params.limit;
-
- options.lang = options.lang.toLowerCase();
- provider.params.lang = (langs_photon.indexOf(options.lang) > -1)
- ? options.lang
- : provider.params.lang;
-
- } else if(options.provider == providers_names.GOOGLE){
-
+ switch(options.provider) {
+ case providers_names.OSM:
+ case providers_names.MAPQUEST:
+ params = {
+ q: options.query,
+ limit: options.limit,
+ 'accept-language': options.lang
+ };
+ provider.params = utils.mergeOptions(provider.params, params);
+ break;
+ case providers_names.PHOTON:
+ options.lang = options.lang.toLowerCase();
+ params = {
+ q: options.query,
+ limit: options.limit || provider.params.limit,
+ lang: (langs_photon.indexOf(options.lang) > -1) ?
+ options.lang : provider.params.lang
+ };
+ provider.params = utils.mergeOptions(provider.params, params);
+ break;
+ case providers_names.GOOGLE:
+ params = {
+ address: options.query,
+ language: options.lang
+ };
+ provider.params = utils.mergeOptions(provider.params, params);
+ break;
+ case providers_names.PELIAS:
+ params = {
+ text: options.query,
+ size: options.limit
+ };
+ provider.params = utils.mergeOptions(provider.params, params);
+ break;
+ }
+ if (requires_key.indexOf(options.provider) > -1) {
provider.params.key = options.key;
- provider.params.address = options.query;
- provider.params.language =
- options.lang || provider.params.language;
-
}
-
return provider;
}
};
@@ -459,7 +477,8 @@
OSM: 'osm',
MAPQUEST: 'mapquest',
GOOGLE: 'google',
- PHOTON: 'photon'
+ PHOTON: 'photon',
+ PELIAS: 'pelias'
},
osm: {
url: 'http://nominatim.openstreetmap.org/search/',
@@ -490,6 +509,14 @@
language: 'en-US'
}
},
+ pelias: {
+ url: 'https://search.mapzen.com/v1/search',
+ params: {
+ key: '',
+ text: '',
+ size: 10
+ }
+ },
photon: {
url: 'http://photon.komoot.de/api/',
params: {
@@ -523,8 +550,7 @@
'',
'',
+ ' placeholder="Search">',
'',
''
].join('');