From 67b7949105151fe785adbfb278c60fb145a3874f Mon Sep 17 00:00:00 2001 From: Scott Farley Date: Mon, 18 Mar 2019 09:50:05 -0700 Subject: [PATCH 1/5] pass flyTo options to map upon selection --- lib/index.js | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/lib/index.js b/lib/index.js index 3c62ebd1..449d2136 100644 --- a/lib/index.js +++ b/lib/index.js @@ -183,10 +183,13 @@ MapboxGeocoder.prototype = { // short-term solution; this may be amended as necessary. this._map.fitBounds(exceptions[selected.id].bbox); } else { - this._map.flyTo({ + var defaultFlyOptions = { center: selected.center, zoom: this.options.zoom - }); + } + var flyOptions = extend({}, defaultFlyOptions, this.options.flyTo); + console.log(flyOptions) + this._map.flyTo(flyOptions); } } this._eventEmitter.emit('result', { result: selected }); From bf58172fe0aa807e95b06c1f422bec94dafe66cd Mon Sep 17 00:00:00 2001 From: Scott Farley Date: Mon, 18 Mar 2019 09:53:59 -0700 Subject: [PATCH 2/5] update jsdoc and api docs --- API.md | 2 +- lib/index.js | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/API.md b/API.md index 85ddffef..50aaf012 100644 --- a/API.md +++ b/API.md @@ -20,7 +20,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 . - `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 with 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 diff --git a/lib/index.js b/lib/index.js index 449d2136..e54bfe92 100644 --- a/lib/index.js +++ b/lib/index.js @@ -17,7 +17,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 with 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 From 6e9f1ef069c52ea7169990b1ac11521d014ed0d7 Mon Sep 17 00:00:00 2001 From: Scott Farley Date: Mon, 18 Mar 2019 09:54:42 -0700 Subject: [PATCH 3/5] Update changelog --- CHANGELOG.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 4a170145..2ec70dbb 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,6 @@ +## Master +- Pass `flyTo` options to the map on result selection. + ## v3.1.4 - Emit a `clear` event when the user backspaces into an empty search bar or selects all existing text and deletes it. From 2cc5ebed84fd4742a37fd2d81d76d7670160fd75 Mon Sep 17 00:00:00 2001 From: Scott Farley Date: Mon, 18 Mar 2019 12:24:25 -0700 Subject: [PATCH 4/5] tests for options.flyTo --- lib/index.js | 3 +- test/test.geocoder.js | 64 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 65 insertions(+), 2 deletions(-) diff --git a/lib/index.js b/lib/index.js index e54bfe92..90de673e 100644 --- a/lib/index.js +++ b/lib/index.js @@ -187,8 +187,7 @@ MapboxGeocoder.prototype = { center: selected.center, zoom: this.options.zoom } - var flyOptions = extend({}, defaultFlyOptions, this.options.flyTo); - console.log(flyOptions) + var flyOptions = extend({}, this.options.flyTo, defaultFlyOptions); this._map.flyTo(flyOptions); } } diff --git a/test/test.geocoder.js b/test/test.geocoder.js index 19c885b1..27681892 100644 --- a/test/test.geocoder.js +++ b/test/test.geocoder.js @@ -7,6 +7,7 @@ var once = require('lodash.once'); var mapboxEvents = require('./../lib/events'); var sinon = require('sinon'); + mapboxgl.accessToken = process.env.MapboxAccessToken; test('geocoder', function(tt) { @@ -431,5 +432,68 @@ 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"); + 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.end(); }); From a42749287dafc01c9ff3aa6ffc37dcba2d5c2612 Mon Sep 17 00:00:00 2001 From: Scott Farley Date: Tue, 19 Mar 2019 07:40:58 -0700 Subject: [PATCH 5/5] Fix typo in documentation --- API.md | 2 +- lib/index.js | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/API.md b/API.md index 50aaf012..27274330 100644 --- a/API.md +++ b/API.md @@ -20,7 +20,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 . - `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) \| [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 with 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](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 diff --git a/lib/index.js b/lib/index.js index 90de673e..3a22c688 100644 --- a/lib/index.js +++ b/lib/index.js @@ -17,7 +17,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 with 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 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