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});