Skip to content

Commit

Permalink
Merge pull request #227 from mapbox/camera-options-on-fit-bounds
Browse files Browse the repository at this point in the history
Pass camera options to map.fitBounds
  • Loading branch information
Scott Farley authored Mar 22, 2019
2 parents d8418cc + 172809c commit 9e77770
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 9 deletions.
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 @@ -220,23 +220,27 @@ MapboxGeocoder.prototype = {
if (selected && selected.id !== this.lastSelected) {
this._clearEl.style.display = 'none';
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

0 comments on commit 9e77770

Please sign in to comment.