@googlemaps/map-loader
Version:
Automatic initialization and loading of the Maps JS API base map into the DOM
3 lines (2 loc) • 4.96 kB
JavaScript
!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?e(exports):"function"==typeof define&&define.amd?define(["exports"],e):e(((t="undefined"!=typeof globalThis?globalThis:t||self).google=t.google||{},t.google.maps=t.google.maps||{},t.google.maps.plugins=t.google.maps.plugins||{},t.google.maps.plugins["map-loader"]={}))}(this,(function(t){"use strict";function e(t,e,i,s){return new(i||(i=Promise))((function(n,o){function r(t){try{l(s.next(t))}catch(t){o(t)}}function a(t){try{l(s.throw(t))}catch(t){o(t)}}function l(t){var e;t.done?n(t.value):(e=t.value,e instanceof i?e:new i((function(t){t(e)}))).then(r,a)}l((s=s.apply(t,e||[])).next())}))}class i{constructor({apiKey:t,channel:e,client:s,id:n="__googleMapsScriptId",libraries:o=[],language:r,region:a,version:l,mapIds:h,nonce:c,retries:p=3,url:d="https://maps.googleapis.com/maps/api/js"}){if(this.CALLBACK="__googleMapsCallback",this.callbacks=[],this.done=!1,this.loading=!1,this.errors=[],this.version=l,this.apiKey=t,this.channel=e,this.client=s,this.id=n||"__googleMapsScriptId",this.libraries=o,this.language=r,this.region=a,this.mapIds=h,this.nonce=c,this.retries=p,this.url=d,i.instance){if(!function t(e,i){if(e===i)return!0;if(e&&i&&"object"==typeof e&&"object"==typeof i){if(e.constructor!==i.constructor)return!1;var s,n,o;if(Array.isArray(e)){if((s=e.length)!=i.length)return!1;for(n=s;0!=n--;)if(!t(e[n],i[n]))return!1;return!0}if(e.constructor===RegExp)return e.source===i.source&&e.flags===i.flags;if(e.valueOf!==Object.prototype.valueOf)return e.valueOf()===i.valueOf();if(e.toString!==Object.prototype.toString)return e.toString()===i.toString();if((s=(o=Object.keys(e)).length)!==Object.keys(i).length)return!1;for(n=s;0!=n--;)if(!Object.prototype.hasOwnProperty.call(i,o[n]))return!1;for(n=s;0!=n--;){var r=o[n];if(!t(e[r],i[r]))return!1}return!0}return e!=e&&i!=i}(this.options,i.instance.options))throw new Error(`Loader must not be called again with different options. ${JSON.stringify(this.options)} !== ${JSON.stringify(i.instance.options)}`);return i.instance}i.instance=this}get options(){return{version:this.version,apiKey:this.apiKey,channel:this.channel,client:this.client,id:this.id,libraries:this.libraries,language:this.language,region:this.region,mapIds:this.mapIds,nonce:this.nonce,url:this.url}}createUrl(){let t=this.url;return t+="?callback="+this.CALLBACK,this.apiKey&&(t+="&key="+this.apiKey),this.channel&&(t+="&channel="+this.channel),this.client&&(t+="&client="+this.client),this.libraries.length>0&&(t+="&libraries="+this.libraries.join(",")),this.language&&(t+="&language="+this.language),this.region&&(t+="®ion="+this.region),this.version&&(t+="&v="+this.version),this.mapIds&&(t+="&map_ids="+this.mapIds.join(",")),t}load(){return this.loadPromise()}loadPromise(){return new Promise((t,e)=>{this.loadCallback(i=>{i?e(i):t()})})}loadCallback(t){this.callbacks.push(t),this.execute()}setScript(){if(document.getElementById(this.id))return void this.callback();const t=this.createUrl(),e=document.createElement("script");e.id=this.id,e.type="text/javascript",e.src=t,e.onerror=this.loadErrorCallback.bind(this),e.defer=!0,e.async=!0,this.nonce&&(e.nonce=this.nonce),document.head.appendChild(e)}deleteScript(){const t=document.getElementById(this.id);t&&t.remove()}resetIfRetryingFailed(){const t=this.retries+1;this.done&&!this.loading&&this.errors.length>=t&&(this.deleteScript(),this.done=!1,this.loading=!1,this.errors=[])}loadErrorCallback(t){if(this.errors.push(t),this.errors.length<=this.retries){const t=this.errors.length*Math.pow(2,this.errors.length);console.log(`Failed to load Google Maps script, retrying in ${t} ms.`),setTimeout(()=>{this.deleteScript(),this.setScript()},t)}else this.onerrorEvent=t,this.callback()}setCallback(){window.__googleMapsCallback=this.callback.bind(this)}callback(){this.done=!0,this.loading=!1,this.callbacks.forEach(t=>{t(this.onerrorEvent)}),this.callbacks=[]}execute(){window.google&&window.google.maps&&window.google.maps.version&&(console.warn("Aborted attempt to load Google Maps JS with @googlemaps/js-api-loader.This may result in undesirable behavior as script parameters may not match."),this.callback()),this.resetIfRetryingFailed(),this.done?this.callback():this.loading||(this.loading=!0,this.setCallback(),this.setScript())}}t.GoogleMap=class{initMap(t){return e(this,void 0,void 0,(function*(){yield this._loadJSAPI(t);const e=this._getMapDiv(t);return new google.maps.Map(e,t.mapOptions)}))}_getMapDiv(t){let e=document.getElementById(t.divId);return t.append&&(e=this._appendMapDiv(e)),e}_appendMapDiv(t){const e=document.createElement("div");return e.setAttribute("id","google_map_appended"),t.appendChild(e),e}_loadJSAPI(t){return e(this,void 0,void 0,(function*(){t.apiOptions||(t.apiOptions={});const e=Object.assign(t.apiOptions,{apiKey:t.apiKey});return new i(e).load()}))}},Object.defineProperty(t,"__esModule",{value:!0})}));
//# sourceMappingURL=map-loader.umd.js.map