Skip to content

Commit ac39be0

Browse files
authored
Merge pull request #434 from mapbox/parameter-updates
Added autocomplete, fuzzyMatch, routing, worldview parameters
2 parents c09f27e + 5073dd3 commit ac39be0

File tree

7 files changed

+427
-130
lines changed

7 files changed

+427
-130
lines changed

API.md

Lines changed: 207 additions & 111 deletions
Large diffs are not rendered by default.

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
## master
22

3+
### Features / Improvements 🚀
4+
5+
- Adds support for `autocomplete`, `fuzzyMatch`, `routing`, and `worldview` parameters
6+
- Bumps `mapbox-sd-js` to v0.13.2 to support `fuzzyMatch` and `worldview` parameters
7+
38
## 4.7.3
49

510
### Dependency update

lib/events.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -164,10 +164,12 @@ MapboxEventManager.prototype = {
164164
bbox: this.bbox,
165165
types: this.types,
166166
endpoint: 'mapbox.places',
167-
// fuzzyMatch: search.fuzzy, //todo --> add to plugin
167+
autocomplete: geocoder.options.autocomplete,
168+
fuzzyMatch: geocoder.options.fuzzyMatch,
168169
proximity: proximity,
169170
limit: geocoder.options.limit,
170-
// routing: search.routing, //todo --> add to plugin
171+
routing: geocoder.options.routing,
172+
worldview: geocoder.options.worldview,
171173
mapZoom: zoom,
172174
keyboardLocale: this.locale
173175
}

lib/index.js

Lines changed: 90 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,10 @@ const GEOCODE_REQUEST_TYPE = {
5959
* @param {Function} [options.getItemValue] A function that specifies how the selected result should be rendered in the search bar. This function should accept a single [Carmen GeoJSON](https://github.com/mapbox/carmen/blob/master/carmen-geojson.md) object as input and return a string. HTML tags in the output string will not be rendered. Defaults to `(item) => item.place_name`.
6060
* @param {String} [options.mode=mapbox.places] A string specifying the geocoding [endpoint](https://docs.mapbox.com/api/search/#endpoints) to query. Options are `mapbox.places` and `mapbox.places-permanent`. The `mapbox.places-permanent` mode requires an enterprise license for permanent geocodes.
6161
* @param {Boolean} [options.localGeocoderOnly=false] If `true`, indicates that the `localGeocoder` results should be the only ones returned to the user. If `false`, indicates that the `localGeocoder` results should be combined with those from the Mapbox API with the `localGeocoder` results ranked higher.
62+
* @param {Boolean} [options.autocomplete=true] Specify whether to return autocomplete results or not. When autocomplete is enabled, results will be included that start with the requested string, rather than just responses that match it exactly.
63+
* @param {Boolean} [options.fuzzyMatch=true] Specify whether the Geocoding API should attempt approximate, as well as exact, matching when performing searches, or whether it should opt out of this behavior and only attempt exact matching.
64+
* @param {Boolean} [options.routing=false] Specify whether to request additional metadata about the recommended navigation destination corresponding to the feature or not. Only applicable for address features.
65+
* @param {String} [options.worldview="us"] Filter results to geographic features whose characteristics are defined differently by audiences belonging to various regional, cultural, or political groups.
6266
* @example
6367
* var geocoder = new MapboxGeocoder({ accessToken: mapboxgl.accessToken });
6468
* map.addControl(geocoder);
@@ -431,13 +435,18 @@ MapboxGeocoder.prototype = {
431435
'types',
432436
'language',
433437
'reverseMode',
434-
'mode'
438+
'mode',
439+
'autocomplete',
440+
'fuzzyMatch',
441+
'routing',
442+
'worldview'
435443
];
436444
const spacesOrCommaRgx = /[\s,]+/;
437445

438446
var self = this;
439447
var config = keys.reduce(function(config, key) {
440-
if (!self.options[key]) {
448+
// don't include undefined/null params, but allow boolean, among other, values
449+
if (self.options[key] === undefined || self.options[key] === null) {
441450
return config;
442451
}
443452

@@ -472,11 +481,13 @@ MapboxGeocoder.prototype = {
472481
config.types ? [config.types[0]] : ["poi"];
473482
config = extend(config, { query: coords, limit: 1 });
474483

475-
// drop proximity which may have been set by trackProximity
476-
// since it's not supported by the reverseGeocoder
477-
if ('proximity' in config) {
478-
delete config.proximity;
479-
}
484+
// Remove config options not supported by the reverseGeocoder
485+
486+
['proximity', 'autocomplete', 'fuzzyMatch', 'bbox'].forEach(function(key) {
487+
if (key in config) {
488+
delete config[key]
489+
}
490+
});
480491
} break;
481492
case GEOCODE_REQUEST_TYPE.FORWARD: {
482493
// Ensure that any reverse geocoding looking request is cleaned up
@@ -1006,6 +1017,78 @@ MapboxGeocoder.prototype = {
10061017
return this.options.origin;
10071018
},
10081019

1020+
/**
1021+
* Set the autocomplete option used for geocoding requests
1022+
* @param {Boolean} value The boolean value to set autocomplete to
1023+
* @returns
1024+
*/
1025+
setAutocomplete: function(value){
1026+
this.options.autocomplete = value;
1027+
return this;
1028+
},
1029+
1030+
/**
1031+
* Get the current autocomplete parameter value used for requests
1032+
* @returns {Boolean} The autocomplete parameter value
1033+
*/
1034+
getAutocomplete: function(){
1035+
return this.options.autocomplete
1036+
},
1037+
1038+
/**
1039+
* Set the fuzzyMatch option used for approximate matching in geocoding requests
1040+
* @param {Boolean} value The boolean value to set fuzzyMatch to
1041+
* @returns
1042+
*/
1043+
setFuzzyMatch: function(value){
1044+
this.options.fuzzyMatch = value;
1045+
return this;
1046+
},
1047+
1048+
/**
1049+
* Get the current fuzzyMatch parameter value used for requests
1050+
* @returns {Boolean} The fuzzyMatch parameter value
1051+
*/
1052+
getFuzzyMatch: function(){
1053+
return this.options.fuzzyMatch
1054+
},
1055+
1056+
/**
1057+
* Set the routing parameter used to ask for routable point metadata in geocoding requests
1058+
* @param {Boolean} value The boolean value to set routing to
1059+
* @returns
1060+
*/
1061+
setRouting: function(value){
1062+
this.options.routing = value;
1063+
return this;
1064+
},
1065+
1066+
/**
1067+
* Get the current routing parameter value used for requests
1068+
* @returns {Boolean} The routing parameter value
1069+
*/
1070+
getRouting: function(){
1071+
return this.options.routing
1072+
},
1073+
1074+
/**
1075+
* Set the worldview parameter
1076+
* @param {String} code The country code representing the worldview (e.g. "us" | "cn" | "jp", "in")
1077+
* @returns
1078+
*/
1079+
setWorldview: function(code){
1080+
this.options.worldview = code;
1081+
return this;
1082+
},
1083+
1084+
/**
1085+
* Get the current worldview parameter value used for requests
1086+
* @returns {String} The worldview parameter value
1087+
*/
1088+
getWorldview: function(){
1089+
return this.options.worldview
1090+
},
1091+
10091092
/**
10101093
* Handle the placement of a result marking the selected result
10111094
* @private

package-lock.json

Lines changed: 9 additions & 9 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@
5959
"mapbox-gl": ">= 0.47.0 < 3.0.0"
6060
},
6161
"dependencies": {
62-
"@mapbox/mapbox-sdk": "^0.13.1",
62+
"@mapbox/mapbox-sdk": "^0.13.2",
6363
"lodash.debounce": "^4.0.6",
6464
"nanoid": "^2.0.1",
6565
"subtag": "^0.5.0",

test/test.geocoder.js

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ test('geocoder', function(tt) {
1818
function setup(opts) {
1919
opts = opts || {};
2020
opts.accessToken = mapboxgl.accessToken;
21+
opts.mapboxgl = opts.mapboxgl || mapboxgl; // set default to prevent warnings littering the test logs
2122
opts.enableEventLogging = false;
2223
container = document.createElement('div');
2324
map = new mapboxgl.Map({ container: container });
@@ -970,6 +971,116 @@ test('geocoder', function(tt) {
970971
t.end();
971972
});
972973

974+
tt.test('geocoder#autocomplete default results', function(t) {
975+
t.plan(1)
976+
setup();
977+
geocoder.query('India');
978+
geocoder.on('results', once(function(e) {
979+
t.ok(e.features.some(feature => feature.place_name.indexOf('Indiana') !== -1 ), 'autocomplete is enabled in default responses');
980+
}));
981+
});
982+
983+
tt.test('geocoder#getAutocomplete', function(t) {
984+
setup({autocomplete: false});
985+
t.equals(geocoder.getAutocomplete(), false, 'getAutocomplete returns the correct autocomplete value');
986+
t.end();
987+
});
988+
989+
tt.test('geocoder#setAutocomplete', function(t){
990+
t.plan(2);
991+
992+
setup({autocomplete: false});
993+
geocoder.setAutocomplete(true);
994+
t.equals(geocoder.options.autocomplete, true, 'the setAutocomplete changes the autocomplete value in the geocoder options');
995+
geocoder.setAutocomplete(false);
996+
geocoder.query('India');
997+
geocoder.on('results', once(function(e) {
998+
t.ok(e.features.every(feature => feature.place_name.indexOf('Indiana') === -1 ), 'disabling autocomplete correctly affects geocoding results');
999+
}));
1000+
});
1001+
1002+
tt.test('geocoder#fuzzyMatch default results', function(t) {
1003+
t.plan(1)
1004+
setup();
1005+
geocoder.query('wahsingtno');
1006+
geocoder.on('results', once(function(e) {
1007+
t.ok(e.features.some(feature => feature.place_name.indexOf('Washington') !== -1 ), 'fuzzyMatch is enabled in default responses');
1008+
}));
1009+
});
1010+
1011+
tt.test('geocoder#getFuzzyMatch', function(t) {
1012+
setup({fuzzyMatch: false});
1013+
t.equals(geocoder.getFuzzyMatch(), false, 'getFuzzyMatch returns the correct fuzzyMatch value');
1014+
t.end();
1015+
});
1016+
1017+
tt.test('geocoder#setFuzzyMatch', function(t){
1018+
t.plan(2);
1019+
1020+
setup({fuzzyMatch: false});
1021+
geocoder.setFuzzyMatch(true);
1022+
t.equals(geocoder.options.fuzzyMatch, true, 'setFuzzyMatch changes the fuzzyMatch value in the geocoder options');
1023+
geocoder.setFuzzyMatch(false);
1024+
geocoder.query('wahsingtno');
1025+
geocoder.on('results', once(function(e) {
1026+
t.equals(e.features.length, 0, 'disabling fuzzyMatch correctly affects geocoding results');
1027+
}));
1028+
});
1029+
1030+
tt.test('geocoder#routing default results', function(t) {
1031+
t.plan(1)
1032+
setup();
1033+
geocoder.query('The White House');
1034+
geocoder.on('results', once(function(e) {
1035+
t.ok(e.features[0].routable_points === undefined, 'routing (returning routable_points) is disabled in default responses');
1036+
}));
1037+
});
1038+
1039+
tt.test('geocoder#getRouting', function(t) {
1040+
setup({routing: true});
1041+
t.equals(geocoder.getRouting(), true, 'getRouting returns the correct routing value');
1042+
t.end();
1043+
});
1044+
1045+
tt.test('geocoder#setRouting', function(t){
1046+
t.plan(2);
1047+
1048+
setup({routing: false});
1049+
geocoder.setRouting(true);
1050+
t.equals(geocoder.options.routing, true, 'setRouting changes the routing value in the geocoder options');
1051+
geocoder.query('The White House');
1052+
geocoder.on('results', once(function(e) {
1053+
t.ok(e.features[0].routable_points !== undefined, 'enabling routing returns routable_points in geocoding results');
1054+
}));
1055+
});
1056+
1057+
tt.test('geocoder#worldview default results', function(t) {
1058+
t.plan(1)
1059+
setup();
1060+
geocoder.query('Taipei');
1061+
geocoder.on('results', once(function(e) {
1062+
t.ok(e.features[0].place_name.indexOf('Taiwan') !== -1, 'worldview defaults to US in responses');
1063+
}));
1064+
});
1065+
1066+
tt.test('geocoder#getWorldview', function(t) {
1067+
setup({worldview: 'cn'});
1068+
t.equals(geocoder.getWorldview(), 'cn', 'getWorldview returns the correct worldview value');
1069+
t.end();
1070+
});
1071+
1072+
tt.test('geocoder#setWorldview', function(t){
1073+
t.plan(2);
1074+
1075+
setup({worldview: 'us'});
1076+
geocoder.setWorldview('cn');
1077+
t.equals(geocoder.options.worldview, 'cn', 'setWorldview changes the worldview value in the geocoder options');
1078+
geocoder.query('Taipei');
1079+
geocoder.on('results', once(function(e) {
1080+
t.ok(e.features[0].place_name.indexOf('China') !== -1, 'setting worldview correctly affects geocoding results');
1081+
}));
1082+
});
1083+
9731084
tt.test('geocoder#_renderMessage', function(t){
9741085
setup({});
9751086
var typeaheadRenderErrorSpy = sinon.spy(geocoder._typeahead, 'renderError');

0 commit comments

Comments
 (0)