Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pass camera options to map.fitBounds #227

Merged
merged 3 commits into from
Mar 22, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion API.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ A geocoder component using Mapbox Geocoding API
- `options.accessToken` **[String][20]** Required.
- `options.origin` **[String][20]** Use to set a custom API origin. Defaults to [https://api.mapbox.com][21].
- `options.zoom` **[Number][22]** 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][23] \| [Object][19])?** 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, the object will be passed to the flyTo map method to specify a custom animation.
- `options.flyTo` **([Boolean][23] \| [Object][19])?** 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, the object will be passed to the map method to specify a custom animation when a result is selected.
- `options.placeholder` **[String][20]** Override the default placeholder attribute value. (optional, default `"Search"`)
- `options.proximity` **[Object][19]?** a proximity argument: this is
a geographical point given as an object with latitude and longitude
Expand Down
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
## Master
- Pass `flyTo` options to the map on result selection.
- Pass `flyTo` options to the map on result selection on both map#flyTo and map#fitBounds operations [#214](https://github.com/mapbox/mapbox-gl-geocoder/pull/214) and [#227](https://github.com/mapbox/mapbox-gl-geocoder/pull/227)
- Obtain language from user's browser settings [#195](https://github.com/mapbox/mapbox-gl-geocoder/issues/195)
- Localize placeholder based on language set in constructor options [#150](https://github.com/mapbox/mapbox-gl-geocoder/issues/150)
- `trackProximity` turned on by default [#195](https://github.com/mapbox/mapbox-gl-geocoder/issues/195)
Expand Down
14 changes: 9 additions & 5 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ var geocoderService;
* @param {String} options.accessToken Required.
* @param {String} options.origin Use to set a custom API origin. Defaults to https://api.mapbox.com.
* @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] 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, the object will be passed to the flyTo map method to specify a custom animation.
* @param {Boolean|Object} [options.flyTo] 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, the object will be passed to the map method to specify a custom animation when a result is selected.
* @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
Expand Down Expand Up @@ -193,23 +193,27 @@ MapboxGeocoder.prototype = {
var selected = this._typeahead.selected;
if (selected && selected.id !== this.lastSelected) {
if (this.options.flyTo) {
var flyOptions;
if (selected.properties && !exceptions[selected.properties.short_code] && selected.bbox) {
var bbox = selected.bbox;
this._map.fitBounds([[bbox[0], bbox[1]], [bbox[2], bbox[3]]]);
flyOptions = extend({}, this.options.flyTo);
this._map.fitBounds([[bbox[0], bbox[1]], [bbox[2], bbox[3]]], flyOptions);
} else if (selected.properties && exceptions[selected.properties.short_code]) {
// Certain geocoder search results return (and therefore zoom to fit)
// an unexpectedly large bounding box: for example, both Russia and the
// USA span both sides of -180/180, or France includes the island of
// Reunion in the Indian Ocean. An incomplete list of these exceptions
// at ./exceptions.json provides "reasonable" bounding boxes as a
// short-term solution; this may be amended as necessary.
this._map.fitBounds(exceptions[selected.properties.short_code].bbox);
flyOptions = extend({}, this.options.flyTo);
this._map.fitBounds(exceptions[selected.properties.short_code].bbox, flyOptions);
} else {
var defaultFlyOptions = {
center: selected.center,
zoom: this.options.zoom
}
var flyOptions = extend({}, this.options.flyTo, defaultFlyOptions);
flyOptions = extend({}, defaultFlyOptions, this.options.flyTo);
// ensure that center is not overriden by custom options
flyOptions.center = selected.center;
this._map.flyTo(flyOptions);
}
}
Expand Down
46 changes: 44 additions & 2 deletions test/test.geocoder.js
Original file line number Diff line number Diff line change
Expand Up @@ -581,11 +581,53 @@ test('geocoder', function(tt) {
t.ok(mapFlyMethod.calledOnce, "The map flyTo was called when the option was set to true");
var calledWithArgs = mapFlyMethod.args[0][0];
t.deepEqual(calledWithArgs.center, [ -122.47846, 37.819378 ], 'the selected result overrides the constructor center option');
t.deepEqual(calledWithArgs.zoom, 16, 'the selected result overrides the constructor zoom optiopn');
t.deepEqual(calledWithArgs.zoom, 4, 'the selected result overrides the constructor zoom option');
t.deepEqual(calledWithArgs.speed, 5, 'speed argument is passed to the flyTo method');
})
);
})
});


tt.test('options.flyTo object on feature with bounding box', function(t){
t.plan(2 )
setup({
flyTo: {
speed: 5
}
});

var mapFlyMethod = sinon.spy(map, "fitBounds");
geocoder.query('Brazil');
geocoder.on(
'result',
once(function() {
t.ok(mapFlyMethod.calledOnce, "The map flyTo was called when the option was set to true");
var calledWithArgs = mapFlyMethod.args[0][1];
t.deepEqual(calledWithArgs.speed, 5, 'speed argument is passed to the flyTo method');
})
);
});


tt.test('options.flyTo object on bounding box excepted feature', function(t){
t.plan(2)
setup({
flyTo: {
speed: 5
}
});

var mapFlyMethod = sinon.spy(map, "fitBounds");
geocoder.query('Canada');
geocoder.on(
'result',
once(function() {
t.ok(mapFlyMethod.calledOnce, "The map flyTo was called when the option was set to true");
var calledWithArgs = mapFlyMethod.args[0][1];
t.deepEqual(calledWithArgs.speed, 5, 'speed argument is passed to the flyTo method');
})
);
});

tt.test('placeholder localization', function(t){
var ensureLanguages = ['de', 'en', 'fr', 'it', 'nl', 'ca', 'cs', 'fr', 'he', 'hu', 'is', 'ja', 'ka', 'ko', 'lv', 'ka', 'ko', 'lv', 'nb', 'pl', 'pt', 'sk', 'sl', 'sr', 'th', 'zh'];
Expand Down