-
Notifications
You must be signed in to change notification settings - Fork 181
No Map #295
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
No Map #295
Changes from 1 commit
c15ddb1
7f53ecf
8173efd
a6d0e70
5830a8b
1a33613
533e088
7ffa0ee
0eb94ba
dc7ab50
12adb63
e637f10
644ccfd
bb5304b
bc89d5e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -90,8 +90,32 @@ MapboxGeocoder.prototype = { | |
| } | ||
| }, | ||
|
|
||
| addTo(container){ | ||
| // if the container is a map, add the control like normal | ||
| if (container._controlContainer){ | ||
| // it's a mapbox-gl map, add like normal | ||
| container.addControl(this) | ||
| } | ||
| // if the container is not a map, but an html element, then add the control to that element | ||
| else if (typeof container == 'string' && (container.startsWith('#') || container.startsWith('.'))){ | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Suggestion: Allow passing in an HTMLElement
|
||
| const parent = document.querySelectorAll(container); | ||
| if (parent.length == 0){ | ||
| throw new Error(`Element ${container} not found.`) | ||
| } | ||
| parent.forEach((parentEl)=>{ | ||
| const el = this.onAdd(); //no map | ||
| parentEl.appendChild(el); | ||
| }) | ||
|
|
||
| }else{ | ||
| throw new Error("Error: addTo Container must be a mapbox-gl-js map or a html element reference") | ||
| } | ||
| }, | ||
|
|
||
| onAdd: function(map) { | ||
| this._map = map; | ||
| if (map && typeof map != 'string'){ | ||
| this._map = map; | ||
| } | ||
|
|
||
| this.setLanguage(); | ||
|
|
||
|
|
@@ -182,20 +206,20 @@ MapboxGeocoder.prototype = { | |
| this.setRenderFunction(this.options.render); | ||
| this._typeahead.getItemValue = this.options.getItemValue; | ||
|
|
||
| if (this.options.trackProximity) { | ||
| if (this.options.trackProximity && this._map) { | ||
| this._updateProximity(); | ||
| this._map.on('moveend', this._updateProximity); | ||
| } | ||
|
|
||
| this.mapMarker = null; | ||
| this._handleMarker = this._handleMarker.bind(this); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This would never be called if there's no map right? I'd move this stuff to the if (this._map) block below for simplicity. Also means you don't need to check for map in the handler |
||
|
|
||
| this._mapboxgl = this.options.mapboxgl; | ||
| if (!this._mapboxgl && this.options.marker) { | ||
| console.error("No mapboxgl detected in options. Map markers are disabled. Please set options.mapboxgl."); | ||
| this.options.marker = false; | ||
| if (this._map){ | ||
| this._mapboxgl = this.options.mapboxgl; | ||
| if (!this._mapboxgl && this.options.marker) { | ||
scottsfarley93 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| console.error("No mapboxgl detected in options. Map markers are disabled. Please set options.mapboxgl."); | ||
| this.options.marker = false; | ||
| } | ||
| } | ||
|
|
||
| return el; | ||
| }, | ||
|
|
||
|
|
@@ -213,7 +237,7 @@ MapboxGeocoder.prototype = { | |
| onRemove: function() { | ||
| this.container.parentNode.removeChild(this.container); | ||
|
|
||
| if (this.options.trackProximity) { | ||
| if (this.options.trackProximity && this._map) { | ||
| this._map.off('moveend', this._updateProximity); | ||
| } | ||
|
|
||
|
|
@@ -279,7 +303,9 @@ MapboxGeocoder.prototype = { | |
| if (selected.properties && !exceptions[selected.properties.short_code] && selected.bbox) { | ||
| var bbox = selected.bbox; | ||
| flyOptions = extend({}, this.options.flyTo); | ||
| this._map.fitBounds([[bbox[0], bbox[1]], [bbox[2], bbox[3]]], flyOptions); | ||
| if (this._map){ | ||
| this._map.fitBounds([[bbox[0], bbox[1]], [bbox[2], bbox[3]]], flyOptions); | ||
| } | ||
| } else if (selected.properties && exceptions[selected.properties.short_code]) { | ||
| // Certain geocoder search results return (and therefore zoom to fit) | ||
| // an unexpectedly large bounding box: for example, both Russia and the | ||
|
|
@@ -288,15 +314,19 @@ MapboxGeocoder.prototype = { | |
| // at ./exceptions.json provides "reasonable" bounding boxes as a | ||
| // short-term solution; this may be amended as necessary. | ||
| flyOptions = extend({}, this.options.flyTo); | ||
| this._map.fitBounds(exceptions[selected.properties.short_code].bbox, flyOptions); | ||
| if (this._map){ | ||
| this._map.fitBounds(exceptions[selected.properties.short_code].bbox, flyOptions); | ||
| } | ||
| } else { | ||
| var defaultFlyOptions = { | ||
| zoom: this.options.zoom | ||
| } | ||
| flyOptions = extend({}, defaultFlyOptions, this.options.flyTo); | ||
| // ensure that center is not overriden by custom options | ||
| flyOptions.center = selected.center; | ||
| this._map.flyTo(flyOptions); | ||
| if (this._map){ | ||
| this._map.flyTo(flyOptions); | ||
| } | ||
| } | ||
| } | ||
| if (this.options.marker && this._mapboxgl){ | ||
|
|
@@ -513,6 +543,9 @@ MapboxGeocoder.prototype = { | |
| _updateProximity: function() { | ||
| // proximity is designed for local scale, if the user is looking at the whole world, | ||
| // it doesn't make sense to factor in the arbitrary centre of the map | ||
| if (!this._map){ | ||
| return; | ||
| } | ||
| if (this._map.getZoom() > 9) { | ||
| var center = this._map.getCenter().wrap(); | ||
| this.setProximity({ longitude: center.lng, latitude: center.lat }); | ||
|
|
@@ -848,6 +881,9 @@ MapboxGeocoder.prototype = { | |
| */ | ||
| _handleMarker: function(selected){ | ||
| // clean up any old marker that might be present | ||
| if (!this._map){ | ||
| return; | ||
| } | ||
| this._removeMarker(); | ||
| var defaultMarkerOptions = { | ||
| color: '#4668F2' | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.