Skip to content

Commit

Permalink
Added support for what3words
Browse files Browse the repository at this point in the history
Added new geocoder provider for what3words
  • Loading branch information
davidpiesse authored and perliedman committed Jun 17, 2015
1 parent 86cc5d7 commit 728d2db
Showing 1 changed file with 61 additions and 0 deletions.
61 changes: 61 additions & 0 deletions Control.Geocoder.js
Original file line number Diff line number Diff line change
Expand Up @@ -666,6 +666,67 @@
L.Control.Geocoder.mapbox = function(access_token) {
return new L.Control.Geocoder.Mapbox(access_token);
};


L.Control.Geocoder.What3Words = L.Class.extend({
options: {
serviceUrl: 'http://api.what3words.com/'
},

initialize: function(accessToken) {
this._accessToken = accessToken;
},

geocode: function(query, cb, context) {
//get three words and make a dot based string
L.Control.Geocoder.getJSON(this.options.serviceUrl +'w3w', {
key: this._accessToken,
string: query.split(/\s+/).join('.'),
}, function(data) {
var results = [], loc, latLng, latLngBounds;
if (data.position && data.position.length) {
loc = data.words;
latLng = L.latLng(data.position[0],data.position[1]);
latLngBounds = L.latLngBounds(latLng, latLng);
results[0] = {
name: loc.join('.'),
bbox: latLngBounds,
center: latLng
};
}

cb.call(context, results);
});
},

suggest: function(query, cb, context) {
return this.geocode(query, cb, context);
},

reverse: function(location, scale, cb, context) {
L.Control.Geocoder.getJSON(this.options.serviceUrl +'position', {
key: this._accessToken,
position: [location.lat,location.lng].join(',')
}, function(data) {
var results = [],loc,latLng,latLngBounds;
if (data.position && data.position.length) {
loc = data.words;
latLng = L.latLng(data.position[0],data.position[1]);
latLngBounds = L.latLngBounds(latLng, latLng);
results[0] = {
name: loc.join('.'),
bbox: latLngBounds,
center: latLng
};
}
cb.call(context, results);
});
}
});

L.Control.Geocoder.what3words = function(access_token) {
return new L.Control.Geocoder.What3Words(access_token);
};

L.Control.Geocoder.Google = L.Class.extend({
options: {
Expand Down

0 comments on commit 728d2db

Please sign in to comment.