From fc87efa0ac3fd8020ca652aee777f01b44063fb6 Mon Sep 17 00:00:00 2001 From: Ruben Cid Date: Tue, 7 Apr 2020 11:37:01 +0200 Subject: [PATCH 1/3] custom marker --- lib/index.js | 11 +++++++++-- test/test.geocoder.js | 23 +++++++++++++++++++++++ 2 files changed, 32 insertions(+), 2 deletions(-) diff --git a/lib/index.js b/lib/index.js index 5040f6b5..7a1c8859 100644 --- a/lib/index.js +++ b/lib/index.js @@ -929,8 +929,14 @@ MapboxGeocoder.prototype = { var defaultMarkerOptions = { color: '#4668F2' } - var markerOptions = extend({}, defaultMarkerOptions, this.options.marker) - this.mapMarker = new this._mapboxgl.Marker(markerOptions); + var markerOptions = extend({}, defaultMarkerOptions, this.options.marker); + var isInstance = this.options.marker instanceof this._mapboxgl.Marker; + if (isInstance) { + this.mapMarker = this.options.marker; + } else { + this.mapMarker = new this._mapboxgl.Marker(markerOptions); + } + if (selected.center) { this.mapMarker .setLngLat(selected.center) @@ -940,6 +946,7 @@ MapboxGeocoder.prototype = { .setLngLat(selected.geometry.coordinates) .addTo(this._map); } + return this; }, diff --git a/test/test.geocoder.js b/test/test.geocoder.js index dfddcc36..5e90cbd2 100644 --- a/test/test.geocoder.js +++ b/test/test.geocoder.js @@ -718,6 +718,29 @@ test('geocoder', function(tt) { ); }); + tt.test('options.marker [marker instance]', function(t) { + t.plan(3); + + const marker = new mapboxgl.Marker(); + marker.id = 'my-custom-marker'; + marker.setLngLat([0,0]); + setup({ + marker: marker, + mapboxgl: mapboxgl + }); + + geocoder.query('high'); + geocoder.on( + 'result', + once(function(r) { + const coords = marker.getLngLat(); + t.equals(coords.lng, r.result.center[0], "sets the correct lng"); + t.equals(coords.lat, r.result.center[1], "sets the correct lat"); + t.equals(geocoder.mapMarker.id, 'my-custom-marker', "keep id"); + }) + ); + }); + tt.test('geocode#onRemove', function(t){ setup({marker: true}); From 166bec8f020acfdc13a3d24fca9cce3220e5e784 Mon Sep 17 00:00:00 2001 From: Ruben Cid Date: Tue, 7 Apr 2020 11:42:03 +0200 Subject: [PATCH 2/3] Doc --- API.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/API.md b/API.md index db2b37c4..24df8232 100644 --- a/API.md +++ b/API.md @@ -95,7 +95,7 @@ A geocoder component using the [Mapbox Geocoding API][55] - `options.reverseMode` **(distance | score)** Set the factors that are used to sort nearby results. (optional, default `distance`) - `options.reverseGeocode` **[boolean][61]** If `true`, enable reverse geocoding mode. In reverse geocoding, search input is expected to be coordinates in the form `lat, lon`, with suggestions being the reverse geocodes. (optional, default `false`) - `options.enableEventLogging` **[Boolean][61]** Allow Mapbox to collect anonymous usage statistics from the plugin. (optional, default `true`) - - `options.marker` **([Boolean][61] \| [Object][56])** If `true`, a [Marker][59] will be added to the map at the location of the user-selected result using a default set of Marker options. If the value is an object, the marker will be constructed using these options. If `false`, no marker will be added to the map. Requires that `options.mapboxgl` also be set. (optional, default `true`) + - `options.marker` **([Boolean][61] \| [Object][56])** If `true`, a [Marker][59] will be added to the map at the location of the user-selected result using a default set of Marker options. If the value is an object, the marker will be constructed using these options. If the value is an marker instance, the marker position will be updated. If `false`, no marker will be added to the map. Requires that `options.mapboxgl` also be set. (optional, default `true`) - `options.render` **[Function][66]?** A function that specifies how the results should be rendered in the dropdown menu. This function should accepts a single [Carmen GeoJSON][67] object as input and return a string. Any HTML in the returned string will be rendered. - `options.getItemValue` **[Function][66]?** A function that specifies how the selected result should be rendered in the search bar. This function should accept a single [Carmen GeoJSON][67] object as input and return a string. HTML tags in the output string will not be rendered. Defaults to `(item) => item.place_name`. - `options.mode` **[String][57]** A string specifying the geocoding [endpoint][68] to query. Options are `mapbox.places` and `mapbox.places-permanent`. The `mapbox.places-permanent` mode requires an enterprise license for permanent geocodes. (optional, default `mapbox.places`) From 748b8d55f066fc14d6f96825ad96500430e69ba0 Mon Sep 17 00:00:00 2001 From: Ruben Cid Date: Tue, 7 Apr 2020 11:42:45 +0200 Subject: [PATCH 3/3] Doc --- API.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/API.md b/API.md index 24df8232..db2b37c4 100644 --- a/API.md +++ b/API.md @@ -95,7 +95,7 @@ A geocoder component using the [Mapbox Geocoding API][55] - `options.reverseMode` **(distance | score)** Set the factors that are used to sort nearby results. (optional, default `distance`) - `options.reverseGeocode` **[boolean][61]** If `true`, enable reverse geocoding mode. In reverse geocoding, search input is expected to be coordinates in the form `lat, lon`, with suggestions being the reverse geocodes. (optional, default `false`) - `options.enableEventLogging` **[Boolean][61]** Allow Mapbox to collect anonymous usage statistics from the plugin. (optional, default `true`) - - `options.marker` **([Boolean][61] \| [Object][56])** If `true`, a [Marker][59] will be added to the map at the location of the user-selected result using a default set of Marker options. If the value is an object, the marker will be constructed using these options. If the value is an marker instance, the marker position will be updated. If `false`, no marker will be added to the map. Requires that `options.mapboxgl` also be set. (optional, default `true`) + - `options.marker` **([Boolean][61] \| [Object][56])** If `true`, a [Marker][59] will be added to the map at the location of the user-selected result using a default set of Marker options. If the value is an object, the marker will be constructed using these options. If `false`, no marker will be added to the map. Requires that `options.mapboxgl` also be set. (optional, default `true`) - `options.render` **[Function][66]?** A function that specifies how the results should be rendered in the dropdown menu. This function should accepts a single [Carmen GeoJSON][67] object as input and return a string. Any HTML in the returned string will be rendered. - `options.getItemValue` **[Function][66]?** A function that specifies how the selected result should be rendered in the search bar. This function should accept a single [Carmen GeoJSON][67] object as input and return a string. HTML tags in the output string will not be rendered. Defaults to `(item) => item.place_name`. - `options.mode` **[String][57]** A string specifying the geocoding [endpoint][68] to query. Options are `mapbox.places` and `mapbox.places-permanent`. The `mapbox.places-permanent` mode requires an enterprise license for permanent geocodes. (optional, default `mapbox.places`)