Skip to content

Commit

Permalink
Merge pull request #453 from mapbox/add-ip-proximity
Browse files Browse the repository at this point in the history
Enable proximity=ip and disabling of trackProximity
  • Loading branch information
mpothier authored Mar 4, 2022
2 parents 68e1e7e + 4cdddc8 commit ea51676
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 34 deletions.
10 changes: 4 additions & 6 deletions API.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,11 +84,8 @@ A geocoder component using the [Mapbox Geocoding API][69]
- `options.zoom` **[Number][74]** On geocoded result what zoom level should the map animate to when a `bbox` isn't found in the response. If a `bbox` is found the map will fit to the `bbox`. (optional, default `16`)
- `options.flyTo` **([Boolean][75] \| [Object][70])** If `false`, animating the map to a selected result is disabled. If `true`, animating the map will use the default animation parameters. If an object, it will be passed as `options` to the map [`flyTo`][76] or [`fitBounds`][77] method providing control over the animation of the transition. (optional, default `true`)
- `options.placeholder` **[String][71]** Override the default placeholder attribute value. (optional, default `Search`)
- `options.proximity` **[Object][70]?** a proximity argument: this is
a geographical point given as an object with `latitude` and `longitude`
properties. Search results closer to this point will be given
higher priority.
- `options.trackProximity` **[Boolean][75]** If `true`, the geocoder proximity will automatically update based on the map view. (optional, default `true`)
- `options.proximity` **([Object][70] \| `"ip"`)?** a geographical point given as an object with `latitude` and `longitude` properties, or the string 'ip' to use a user's IP address location. Search results closer to this point will be given higher priority.
- `options.trackProximity` **[Boolean][75]** If `true`, the geocoder proximity will dynamically update based on the current map view or user's IP location, depending on zoom level. (optional, default `true`)
- `options.collapsed` **[Boolean][75]** If `true`, the geocoder control will collapse until hovered or in focus. (optional, default `false`)
- `options.clearAndBlurOnEsc` **[Boolean][75]** If `true`, the geocoder control will clear it's contents and blur when user presses the escape key. (optional, default `false`)
- `options.clearOnBlur` **[Boolean][75]** If `true`, the geocoder control will clear its value when the input blurs. (optional, default `false`)
Expand Down Expand Up @@ -185,7 +182,8 @@ Set proximity

#### Parameters

- `proximity` **[Object][70]** The new `options.proximity` value. This is a geographical point given as an object with `latitude` and `longitude` properties.
- `proximity` **([Object][70] \| `"ip"`)** The new `options.proximity` value. This is a geographical point given as an object with `latitude` and `longitude` properties or the string 'ip'.
- `disableTrackProximity` **[Boolean][75]** If true, sets `trackProximity` to false. True by default to prevent `trackProximity` from unintentionally overriding an explicitly set proximity value. (optional, default `true`)

Returns **[MapboxGeocoder][83]** this

Expand Down
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
### Features / Improvements 🚀

- Adds `setAccessToken` method to update the accessToken after the Geocoder has been initialized [#449](https://github.com/mapbox/mapbox-gl-geocoder/pull/449)
- Enables use of the value `'ip'` for `proximity` to bias around a user's location [#453](https://github.com/mapbox/mapbox-gl-geocoder/pull/453)

### Dependency update

Expand Down
21 changes: 11 additions & 10 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,8 @@ const GEOCODE_REQUEST_TYPE = {
* @param {Number} [options.zoom=16] On geocoded result what zoom level should the map animate to when a `bbox` isn't found in the response. If a `bbox` is found the map will fit to the `bbox`.
* @param {Boolean|Object} [options.flyTo=true] If `false`, animating the map to a selected result is disabled. If `true`, animating the map will use the default animation parameters. If an object, it will be passed as `options` to the map [`flyTo`](https://docs.mapbox.com/mapbox-gl-js/api/#map#flyto) or [`fitBounds`](https://docs.mapbox.com/mapbox-gl-js/api/#map#fitbounds) method providing control over the animation of the transition.
* @param {String} [options.placeholder=Search] Override the default placeholder attribute value.
* @param {Object} [options.proximity] a proximity argument: this is
* a geographical point given as an object with `latitude` and `longitude`
* properties. Search results closer to this point will be given
* higher priority.
* @param {Boolean} [options.trackProximity=true] If `true`, the geocoder proximity will automatically update based on the map view.
* @param {Object|'ip'} [options.proximity] a geographical point given as an object with `latitude` and `longitude` properties, or the string 'ip' to use a user's IP address location. Search results closer to this point will be given higher priority.
* @param {Boolean} [options.trackProximity=true] If `true`, the geocoder proximity will dynamically update based on the current map view or user's IP location, depending on zoom level.
* @param {Boolean} [options.collapsed=false] If `true`, the geocoder control will collapse until hovered or in focus.
* @param {Boolean} [options.clearAndBlurOnEsc=false] If `true`, the geocoder control will clear it's contents and blur when user presses the escape key.
* @param {Boolean} [options.clearOnBlur=false] If `true`, the geocoder control will clear its value when the input blurs.
Expand Down Expand Up @@ -675,14 +672,14 @@ MapboxGeocoder.prototype = {
_updateProximity: function() {
// proximity is designed for local scale, if the user is looking at the whole world,
// it doesn't make sense to factor in the arbitrary centre of the map
if (!this._map){
if (!this._map || !this.options.trackProximity){
return;
}
if (this._map.getZoom() > 9) {
var center = this._map.getCenter().wrap();
this.setProximity({ longitude: center.lng, latitude: center.lat });
this.setProximity({ longitude: center.lng, latitude: center.lat }, false);
} else {
this.setProximity(null);
this.setProximity(null, false);
}
},

Expand Down Expand Up @@ -761,11 +758,15 @@ MapboxGeocoder.prototype = {

/**
* Set proximity
* @param {Object} proximity The new `options.proximity` value. This is a geographical point given as an object with `latitude` and `longitude` properties.
* @param {Object|'ip'} proximity The new `options.proximity` value. This is a geographical point given as an object with `latitude` and `longitude` properties or the string 'ip'.
* @param {Boolean} disableTrackProximity If true, sets `trackProximity` to false. True by default to prevent `trackProximity` from unintentionally overriding an explicitly set proximity value.
* @returns {MapboxGeocoder} this
*/
setProximity: function(proximity) {
setProximity: function(proximity, disableTrackProximity = true) {
this.options.proximity = proximity;
if (disableTrackProximity) {
this.options.trackProximity = false;
}
return this;
},

Expand Down
30 changes: 15 additions & 15 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@
"mapbox-gl": ">= 0.47.0 < 3.0.0"
},
"dependencies": {
"@mapbox/mapbox-sdk": "^0.13.2",
"@mapbox/mapbox-sdk": "^0.13.3",
"lodash.debounce": "^4.0.6",
"nanoid": "^3.1.31",
"subtag": "^0.5.0",
Expand Down
40 changes: 38 additions & 2 deletions test/test.geocoder.js
Original file line number Diff line number Diff line change
Expand Up @@ -474,7 +474,7 @@ test('geocoder', function(tt) {
});

tt.test('options.trackProximity', function(t) {
t.plan(2);
t.plan(3);

setup({
trackProximity: true
Expand All @@ -490,6 +490,7 @@ test('geocoder', function(tt) {

map.setZoom(9);
t.notOk(geocoder.getProximity(), 'proximity unset after zooming out');
t.true(geocoder.options.trackProximity, 'trackProximity remains enabled after it automatically updates proximity')
});

tt.test('options.trackProximity=false', function(t) {
Expand All @@ -503,13 +504,15 @@ test('geocoder', function(tt) {
});

tt.test('options.setProximity', function(t) {
t.plan(1);
t.plan(4);

setup({});

t.true(geocoder.options.trackProximity, 'trackProximity is set to true by default')
map.setZoom(13);
map.setCenter([-79.4512, 43.6568]);
geocoder.setProximity({ longitude: -79.4512, latitude: 43.6568});
t.false(geocoder.options.trackProximity, 'trackProximity is set to false after explicitly setting proximity')

geocoder.query('high');
geocoder.on(
Expand All @@ -519,6 +522,19 @@ test('geocoder', function(tt) {
e.features[0].place_name.indexOf('Toronto') !== -1,
'proximity applied in geocoding request'
);
// Move map and make sure _updateProximity not still changing proximity
map.setCenter([0,0]);
map.setZoom(5);
geocoder.query('high');
geocoder.on(
'results',
once(function(e) {
t.ok(
e.features[0].place_name.indexOf('Toronto') !== -1,
'explicitly-set proximity remains intact after moving map'
)
})
)
})
);
});
Expand Down Expand Up @@ -564,6 +580,26 @@ test('geocoder', function(tt) {
);
});

tt.test('proximity can be set to a value of "ip"', function(t) {
t.plan(1)

setup({trackProximity: false});

geocoder.setProximity('ip');
geocoder.query('par');

geocoder.on(
'results',
once(function(e) {
t.ok(e.features.length > 0,
'proximity=ip successfully returned results'
);
})
);


})

tt.test('options.render', function(t){
t.plan(3);
setup({
Expand Down

0 comments on commit ea51676

Please sign in to comment.