Skip to content

Commit

Permalink
Simplify marker options to a single constructor option
Browse files Browse the repository at this point in the history
  • Loading branch information
Scott Farley committed Mar 20, 2019
1 parent f454c4d commit e730470
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 23 deletions.
3 changes: 1 addition & 2 deletions API.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,7 @@ A geocoder component using Mapbox Geocoding API
- `options.localGeocoder` **[Function](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Statements/function)?** A function accepting the query string which performs local geocoding to supplement results from the Mapbox Geocoding API. Expected to return an Array of GeoJSON Features in the [Carmen GeoJSON](https://github.com/mapbox/carmen/blob/master/carmen-geojson.md) format.
- `options.reverseMode` **(`"distance"` \| `"score"`)** Set the factors that are used to sort nearby results. (optional, default `'distance'`)
- `options.reverseGeocode` **[boolean](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Boolean)?** Enable reverse geocoding. Defaults to false. Expects coordinates to be lat, lon.
- `options.addToMap` **[Boolean](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Boolean)** Indicates that the location of the user-selected result should be added to the map as a [mapbox-gl#Marker](https://docs.mapbox.com/mapbox-gl-js/api/#marker) (optional, default `true`)
- `options.markerOptions` **[Object](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object)?** An object specifying the properties to use when constructing the marker, if `addToMap` is true
- `options.marker` **([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 `true`, a [Marker](https://docs.mapbox.com/mapbox-gl-js/api/#marker) will be added to the map at the location of the user-selected result using a default set of Marker properties. If the value is an object, the marker will be constructed using these properties. If `false`, no marker will be added to the map. (optional, default `true`)

**Examples**

Expand Down
16 changes: 7 additions & 9 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,7 @@ var geocoderService;
* @param {Function} [options.localGeocoder] A function accepting the query string which performs local geocoding to supplement results from the Mapbox Geocoding API. Expected to return an Array of GeoJSON Features in the [Carmen GeoJSON](https://github.com/mapbox/carmen/blob/master/carmen-geojson.md) format.
* @param {'distance'|'score'} [options.reverseMode='distance'] - Set the factors that are used to sort nearby results.
* @param {boolean} [options.reverseGeocode] Enable reverse geocoding. Defaults to false. Expects coordinates to be lat, lon.
* @param {Boolean} [options.addToMap=true] Indicates that the location of the user-selected result should be added to the map as a [mapbox-gl#Marker](https://docs.mapbox.com/mapbox-gl-js/api/#marker)
* @param {Object} [options.markerOptions] An object specifying the properties to use when constructing the marker, if `addToMap` is true
* @param {Boolean|Object} [options.marker=true] If `true`, a [Marker](https://docs.mapbox.com/mapbox-gl-js/api/#marker) will be added to the map at the location of the user-selected result using a default set of Marker properties. If the value is an object, the marker will be constructed using these properties. If `false`, no marker will be added to the map.
* @example
* var geocoder = new MapboxGeocoder({ accessToken: mapboxgl.accessToken });
* map.addControl(geocoder);
Expand All @@ -69,11 +68,7 @@ MapboxGeocoder.prototype = {
reverseGeocode: false,
limit: 5,
origin: 'https://api.mapbox.com',
addToMap: true,
markerOptions: {
color: '#4668F2',
anchor: 'bottom'
}
marker: true
},

onAdd: function(map) {
Expand Down Expand Up @@ -206,7 +201,7 @@ MapboxGeocoder.prototype = {
this._map.flyTo(flyOptions);
}
}
if (this.options.addToMap){
if (this.options.marker){
this._handleMarker(selected);
}

Expand Down Expand Up @@ -454,7 +449,10 @@ MapboxGeocoder.prototype = {
_handleMarker: function(selected){
// clean up any old marker that might be present
this._removeMarker();
var markerOptions = extend({}, this.options.markerOptions)
var defaultMarkerOptions = {
color: '#4668F2'
}
var markerOptions = extend({}, defaultMarkerOptions, this.options.marker)
this.mapMarker = new mapboxgl.Marker(markerOptions);
this.mapMarker
.setLngLat(selected.center)
Expand Down
18 changes: 6 additions & 12 deletions test/test.geocoder.js
Original file line number Diff line number Diff line change
Expand Up @@ -512,11 +512,11 @@ test('geocoder', function(tt) {
t.end();
});

tt.test('options.addToMap', function(t) {
tt.test('options.marker [true]', function(t) {
t.plan(2);

setup({
addToMap: true
marker: true
});
var markerConstructorSpy = sinon.spy(mapboxgl, "Marker");

Expand All @@ -532,12 +532,11 @@ test('geocoder', function(tt) {
);
});

tt.test('options.markerOptions', function(t) {
tt.test('options.marker [constructor properties]', function(t) {
t.plan(4);

setup({
addToMap: true,
markerOptions: {
marker: {
color: "purple",
draggable: true,
anchor: 'top'
Expand All @@ -559,16 +558,11 @@ test('geocoder', function(tt) {
);
});

tt.test('options.addToMap [false]', function(t) {
tt.test('options.marker [false]', function(t) {
t.plan(1);

setup({
addToMap: false,
markerOptions: {
color: "purple",
draggable: true,
anchor: 'top'
}
marker: false
});
var markerConstructorSpy = sinon.spy(mapboxgl, "Marker");

Expand Down

0 comments on commit e730470

Please sign in to comment.