// google-maps.service.ts import { Injectable } from "@angular/core"; @Injectable({ providedIn: "root", }) export class GoogleMapsService { private scriptLoaded = false; constructor() {} public mapOnload(){ console.log(" Google map Onload "); } public loadScript( apiKey: string, libraries: string = "places,geometry,drawing,visualization", language: string = "en" ): Promise { return new Promise((resolve, reject) => { if (this.scriptLoaded) { resolve(); return; } const script = document.createElement("script"); script.src = `https://maps.googleapis.com/maps/api/js?key=${apiKey}&libraries=${libraries}&language=${language}&callback=${this.mapOnload}`; script.async = true; script.defer = true; script.onload = () => { this.scriptLoaded = true; resolve(); }; script.onerror = (error) => { reject(error); }; document.head.appendChild(script); }); } }