Skip to content

Commit a0151cc

Browse files
quinnchrperliedman
authored andcommitted
Adding ability to pass query options to the Mapbox API (#164)
* Adding ability to pass query options to the Mapbox API * Woops, better make sure to check for empty proximity option first * Moving proximity param conversion inside geocoding function and giving reverse its own set of options for Mapbox
1 parent 9501e6d commit a0151cc

File tree

1 file changed

+16
-10
lines changed

1 file changed

+16
-10
lines changed

src/geocoders/mapbox.js

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,26 @@ var L = require('leaflet'),
44
module.exports = {
55
class: L.Class.extend({
66
options: {
7-
serviceUrl: 'https://api.tiles.mapbox.com/v4/geocode/mapbox.places-v1/'
7+
serviceUrl: 'https://api.tiles.mapbox.com/v4/geocode/mapbox.places-v1/',
8+
geocodingQueryParams: {},
9+
reverseQueryParams: {}
810
},
911

1012
initialize: function(accessToken, options) {
1113
L.setOptions(this, options);
12-
this._accessToken = accessToken;
14+
this.options.geocodingQueryParams.access_token = accessToken;
15+
this.options.reverseQueryParams.access_token = accessToken;
1316
},
1417

1518
geocode: function(query, cb, context) {
16-
Util.getJSON(this.options.serviceUrl + encodeURIComponent(query) + '.json', {
17-
access_token: this._accessToken,
18-
}, function(data) {
19+
var params = this.options.geocodingQueryParams;
20+
if (typeof params.proximity !== 'undefined'
21+
&& params.proximity.hasOwnProperty('lat')
22+
&& params.proximity.hasOwnProperty('lng'))
23+
{
24+
params.proximity = params.proximity.lng + ',' + params.proximity.lat;
25+
}
26+
Util.getJSON(this.options.serviceUrl + encodeURIComponent(query) + '.json', params, function(data) {
1927
var results = [],
2028
loc,
2129
latLng,
@@ -24,7 +32,7 @@ module.exports = {
2432
for (var i = 0; i <= data.features.length - 1; i++) {
2533
loc = data.features[i];
2634
latLng = L.latLng(loc.center.reverse());
27-
if(loc.hasOwnProperty('bbox'))
35+
if (loc.hasOwnProperty('bbox'))
2836
{
2937
latLngBounds = L.latLngBounds(L.latLng(loc.bbox.slice(0, 2).reverse()), L.latLng(loc.bbox.slice(2, 4).reverse()));
3038
}
@@ -49,9 +57,7 @@ module.exports = {
4957
},
5058

5159
reverse: function(location, scale, cb, context) {
52-
Util.getJSON(this.options.serviceUrl + encodeURIComponent(location.lng) + ',' + encodeURIComponent(location.lat) + '.json', {
53-
access_token: this._accessToken,
54-
}, function(data) {
60+
Util.getJSON(this.options.serviceUrl + encodeURIComponent(location.lng) + ',' + encodeURIComponent(location.lat) + '.json', this.options.reverseQueryParams, function(data) {
5561
var results = [],
5662
loc,
5763
latLng,
@@ -60,7 +66,7 @@ module.exports = {
6066
for (var i = 0; i <= data.features.length - 1; i++) {
6167
loc = data.features[i];
6268
latLng = L.latLng(loc.center.reverse());
63-
if(loc.hasOwnProperty('bbox'))
69+
if (loc.hasOwnProperty('bbox'))
6470
{
6571
latLngBounds = L.latLngBounds(L.latLng(loc.bbox.slice(0, 2).reverse()), L.latLng(loc.bbox.slice(2, 4).reverse()));
6672
}

0 commit comments

Comments
 (0)