UNPKG

react-map-gl-geocoder

Version:

React wrapper for mapbox-gl-geocoder for use with react-map-gl

3 lines (2 loc) 5.76 kB
!function(e,o){"object"==typeof exports&&"undefined"!=typeof module?module.exports=o(require("react"),require("prop-types"),require("@mapbox/mapbox-gl-geocoder"),require("react-map-gl"),require("viewport-mercator-project"),require("mapbox-gl")):"function"==typeof define&&define.amd?define(["react","prop-types","@mapbox/mapbox-gl-geocoder","react-map-gl","viewport-mercator-project","mapbox-gl"],o):(e=e||self).reactMapGlGeocoder=o(e.react,e.propTypes,e.MapboxGeocoder,e.reactMapGl,e.viewportMercatorProject,e.mapboxgl)}(this,function(e,o,n,r,t,l){o=o&&Object.prototype.hasOwnProperty.call(o,"default")?o.default:o,n=n&&Object.prototype.hasOwnProperty.call(n,"default")?n.default:n,t=t&&Object.prototype.hasOwnProperty.call(t,"default")?t.default:t,l=l&&Object.prototype.hasOwnProperty.call(l,"default")?l.default:l;var c=["top-left","top-right","bottom-left","bottom-right"],a=function(e){var o,a;function i(){for(var o,a=arguments.length,i=new Array(a),u=0;u<a;u++)i[u]=arguments[u];return(o=e.call.apply(e,[this].concat(i))||this).geocoder=null,o.cachedResult=null,o.initializeGeocoder=function(){var e=o.getMapboxMap(),r=o.getContainerNode(),t=o.props,a=t.inputValue,i=t.render,u=t.getItemValue,p=t.onInit,d=t.position,s={accessToken:t.mapboxApiAccessToken,origin:t.origin,zoom:t.zoom,flyTo:!1,placeholder:t.placeholder,proximity:t.proximity,trackProximity:t.trackProximity,collapsed:t.collapsed,clearAndBlurOnEsc:t.clearAndBlurOnEsc,clearOnBlur:t.clearOnBlur,bbox:t.bbox,types:t.types,countries:t.countries,minLength:t.minLength,limit:t.limit,language:t.language,filter:t.filter,localGeocoder:t.localGeocoder,localGeocoderOnly:t.localGeocoderOnly,reverseGeocode:t.reverseGeocode,enableEventLogging:t.enableEventLogging,marker:t.marker,mapboxgl:l};i&&"function"==typeof i&&(s.render=i),u&&"function"==typeof u&&(s.getItemValue=u),o.geocoder=new n(s),o.subscribeEvents(),r?r.appendChild(o.geocoder.onAdd(e)):e.addControl(o.geocoder,c.find(function(e){return d===e})),null!=a?o.geocoder.setInput(a):o.cachedResult&&o.geocoder.setInput(o.cachedResult.place_name),(o.cachedResult||null!=a)&&o.showClearIcon(),p(o.geocoder)},o.showClearIcon=function(){o.geocoder._clearEl.style.display="block"},o.getMapboxMap=function(){var e=o.props.mapRef;return e&&e.current&&e.current.getMap()||null},o.getContainerNode=function(){var e=o.props.containerRef;return e&&e.current||null},o.subscribeEvents=function(){o.geocoder.on("clear",o.handleClear),o.geocoder.on("loading",o.handleLoading),o.geocoder.on("results",o.handleResults),o.geocoder.on("result",o.handleResult),o.geocoder.on("error",o.handleError)},o.unsubscribeEvents=function(){o.geocoder.off("clear",o.handleClear),o.geocoder.off("loading",o.handleLoading),o.geocoder.off("results",o.handleResults),o.geocoder.off("result",o.handleResult),o.geocoder.off("error",o.handleError)},o.removeGeocoder=function(){var e=o.getMapboxMap();o.unsubscribeEvents(),e&&e.removeControl&&o.getMapboxMap().removeControl(o.geocoder),o.geocoder=null},o.handleClear=function(){o.cachedResult=null,o.props.onClear()},o.handleLoading=function(e){o.props.onLoading(e)},o.handleResults=function(e){o.props.onResults(e)},o.handleResult=function(e){var n=e.result,l=o.props,c=l.onViewportChange,a=l.onResult,i=n.bbox,u=n.center,p=n.properties,d=(void 0===p?{}:p).short_code,s=u[0],g=u[1],f={fr:{name:"France",bbox:[[-4.59235,41.380007],[9.560016,51.148506]]},us:{name:"United States",bbox:[[-171.791111,18.91619],[-66.96466,71.357764]]},ru:{name:"Russia",bbox:[[19.66064,41.151416],[190.10042,81.2504]]},ca:{name:"Canada",bbox:[[-140.99778,41.675105],[-52.648099,83.23324]]}},b=o.getMapboxMap().getContainer().getBoundingClientRect(),m=b.width,h=b.height,y=o.geocoder.options.zoom,x=function(e,o){return new t(o).fitBounds(e)};try{!f[d]&&i?y=x([[i[0],i[1]],[i[2],i[3]]],{width:m,height:h}).zoom:f[d]&&(y=x(f[d].bbox,{width:m,height:h}).zoom)}catch(e){console.warn("following result caused an error when trying to zoom to bbox: ",n),y=o.geocoder.options.zoom}c({longitude:s,latitude:g,zoom:y,transitionInterpolator:new r.FlyToInterpolator,transitionDuration:3e3}),a(e),o.cachedResult=n,o.geocoder._typeahead.selected=null,o.showClearIcon()},o.handleError=function(e){o.props.onError(e)},o}a=e,(o=i).prototype=Object.create(a.prototype),o.prototype.constructor=o,o.__proto__=a;var u=i.prototype;return u.componentDidMount=function(){this.initializeGeocoder()},u.componentWillUnmount=function(){this.removeGeocoder()},u.componentDidUpdate=function(){this.removeGeocoder(),this.initializeGeocoder()},u.getGeocoder=function(){return this.geocoder},u.render=function(){return null},i}(e.PureComponent);return a.propTypes={mapRef:o.object.isRequired,containerRef:o.object,onViewportChange:o.func,mapboxApiAccessToken:o.string.isRequired,inputValue:o.string,origin:o.string,zoom:o.number,placeholder:o.string,proximity:o.object,trackProximity:o.bool,collapsed:o.bool,clearAndBlurOnEsc:o.bool,clearOnBlur:o.bool,bbox:o.array,types:o.string,countries:o.string,minLength:o.number,limit:o.number,language:o.string,filter:o.func,localGeocoder:o.func,localGeocoderOnly:o.bool,reverseGeocode:o.bool,enableEventLogging:o.bool,marker:o.oneOfType([o.bool,o.object]),render:o.func,getItemValue:o.func,position:o.oneOf(c),onInit:o.func,onClear:o.func,onLoading:o.func,onResults:o.func,onResult:o.func,onError:o.func},a.defaultProps={onViewportChange:function(){},origin:"https://api.mapbox.com",zoom:16,placeholder:"Search",trackProximity:!1,collapsed:!1,clearAndBlurOnEsc:!1,clearOnBlur:!1,minLength:2,limit:5,reverseGeocode:!1,enableEventLogging:!0,marker:!0,position:"top-right",onInit:function(){},onClear:function(){},onLoading:function(){},onResults:function(){},onResult:function(){},onError:function(){}},a}); //# sourceMappingURL=index.umd.js.map