Skip to content
New issue

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

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

Already on GitHub? # to your account

Pass flyTo options to map upon selection #214

Merged
merged 6 commits into from
Mar 19, 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 @@ -21,7 +21,7 @@ A geocoder component using Mapbox Geocoding API
- `options.accessToken` **[String](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)** Required.
- `options.origin` **[String](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)** Use to set a custom API origin. Defaults to <https://api.mapbox.com>.
- `options.zoom` **[Number](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number)** 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](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Boolean)** If false, animating the map to a selected result is disabled. (optional, default `true`)
- `options.flyTo` **([Boolean](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Boolean) \| [Object](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object))?** 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.placeholder` **[String](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)** Override the default placeholder attribute value. (optional, default `"Search"`)
- `options.proximity` **[Object](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object)?** a proximity argument: this is
a geographical point given as an object with latitude and longitude
Expand Down
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
## Unreleased
## Master
- Pass `flyTo` options to the map on result selection.
- 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)
- Bump suggestions to v1.3.4
- Remove hardcoded IDs in bounding box exception list
- Fix duplicate event bug


## v3.1.4

- Emit a `clear` event when the user backspaces into an empty search bar or selects all existing text and deletes it.
Expand Down
8 changes: 5 additions & 3 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} [options.flyTo=true] If false, animating the map to a selected result is disabled.
* @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 {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 @@ -187,10 +187,12 @@ MapboxGeocoder.prototype = {
// short-term solution; this may be amended as necessary.
this._map.fitBounds(exceptions[selected.properties.short_code].bbox);
} else {
this._map.flyTo({
var defaultFlyOptions = {
center: selected.center,
zoom: this.options.zoom
});
}
var flyOptions = extend({}, this.options.flyTo, defaultFlyOptions);
this._map.flyTo(flyOptions);
}
}
if (selected.id !== this.lastSelected){
Expand Down
64 changes: 64 additions & 0 deletions test/test.geocoder.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ var mapboxEvents = require('./../lib/events');
var sinon = require('sinon');
var localization = require('./../lib/localization');


mapboxgl.accessToken = process.env.MapboxAccessToken;

test('geocoder', function(tt) {
Expand Down Expand Up @@ -446,6 +447,69 @@ test('geocoder', function(tt) {
);
});

tt.test('options.flyTo [false]', function(t){
t.plan(1)
setup({
flyTo: false
});

var mapFlyMethod = sinon.spy(map, "flyTo");
geocoder.query('Golden Gate Bridge');
geocoder.on(
'result',
once(function(e) {
// console.log(geocoder._typeahead)
t.ok(mapFlyMethod.notCalled, "The map flyTo was not called when the option was set to false")
})
);
});


tt.test('options.flyTo [true]', function(t){
t.plan(3)
setup({
flyTo: true
});

var mapFlyMethod = sinon.spy(map, "flyTo");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice!!

geocoder.query('Golden Gate Bridge');
geocoder.on(
'result',
once(function(e) {
// console.log(geocoder._typeahead)
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 map is directed to fly to the right place');
t.deepEqual(calledWithArgs.zoom, 16, 'the map is directed to fly to the right zoom');
})
);
});

tt.test('options.flyTo [object]', function(t){
t.plan(4)
setup({
flyTo: {
speed: 5,
zoom: 4,
center: [0, 0]
}
});

var mapFlyMethod = sinon.spy(map, "flyTo");
geocoder.query('Golden Gate Bridge');
geocoder.on(
'result',
once(function(e) {
// console.log(geocoder._typeahead)
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.speed, 5, 'speed argument is passed to the flyTo method');
})
);
})

tt.test('placeholder localization', (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'];
ensureLanguages.forEach(function(languageTag){
Expand Down