leaflet-control-geocoder
Version:
Extendable geocoder with builtin support for Nominatim, Bing, Google, Mapbox, Photon, What3Words, MapQuest, Mapzen
100 lines (83 loc) • 2.05 kB
JavaScript
var L = require('leaflet'),
Util = require('../util');
module.exports = {
class: L.Class.extend({
options: {
serviceUrl: '//photon.komoot.de/api/',
reverseUrl: '//photon.komoot.de/reverse/',
nameProperties: [
'name',
'street',
'suburb',
'hamlet',
'town',
'city',
'state',
'country'
]
},
initialize: function(options) {
L.setOptions(this, options);
},
geocode: function(query, cb, context) {
var params = L.extend({
q: query,
}, this.options.geocodingQueryParams);
Util.getJSON(this.options.serviceUrl, params, L.bind(function(data) {
cb.call(context, this._decodeFeatures(data));
}, this));
},
suggest: function(query, cb, context) {
return this.geocode(query, cb, context);
},
reverse: function(latLng, scale, cb, context) {
var params = L.extend({
lat: latLng.lat,
lon: latLng.lng
}, this.options.geocodingQueryParams);
Util.getJSON(this.options.reverseUrl, params, L.bind(function(data) {
cb.call(context, this._decodeFeatures(data));
}, this));
},
_decodeFeatures: function(data) {
var results = [],
i,
f,
c,
latLng,
extent,
bbox;
if (data && data.features) {
for (i = 0; i < data.features.length; i++) {
f = data.features[i];
c = f.geometry.coordinates;
latLng = L.latLng(c[1], c[0]);
extent = f.properties.extent;
if (extent) {
bbox = L.latLngBounds([extent[1], extent[0]], [extent[3], extent[2]]);
} else {
bbox = L.latLngBounds(latLng, latLng);
}
results.push({
name: this._deocodeFeatureName(f),
center: latLng,
bbox: bbox,
properties: f.properties
});
}
}
return results;
},
_deocodeFeatureName: function(f) {
var j,
name;
for (j = 0; !name && j < this.options.nameProperties.length; j++) {
name = f.properties[this.options.nameProperties[j]];
}
return name;
}
}),
factory: function(options) {
return new L.Control.Geocoder.Photon(options);
}
};