Skip to content

Dynamically load map libraries #5

@indrimuska

Description

@indrimuska

Hi all,
This version of the map loader, like the previous one, does not provide the ability to incrementally and dynamically load libraries at different instance of time.

In my use case I have a single page app with forms which show an Autocomplete, a Map or a Map with drawing tools, depending on the selected route. I have modeled these control in reusable building blocks (e.g. React components, Angular directives, Web Elements, ...), but unfortunately each control requires a different subset of map library:

  • autocomplete: ["places"]
  • map: ["places"],
  • map with drawing tools: ["places", "drawing"]

If I am not wrong, this loader should be able to guarantee 1-time-loading of libraries, so if I require "places", then I cannot load "drawing" later anymore. Basically the loader dies as soon as you use it (actually it may work if I use it multiple times, but I am not sure if the previously provided callback will be fired again).

image

The loader should take the ownership of handling multiple loading requests, also avoiding to load
libraries which are already in place.

SBE:

const loader1 = new Loader({ apiKey, libraries: ["places"] });
await loader1.load(); // <-- loads "places" lib

const loader2 = new Loader({ apiKey, libraries: ["places", "drawing"] });
await loader2.load(); // <-- loads only "drawing" lib

The implementations is also quite easy, broadly like the following:

libraries: Libraries;
constructor(libraries: Libraries) {
   // filter out already loaded services
   const librariesToLoad = libraries.filter(library => this.libraries.indexOf(library) < 0);
   // everything is loaded already?
   if (librariesToLoad.length === 0) return;
   // save reference to new libs
   this.libraries.push(...librariesToLoad);
   // create tag and load `librariesToLoad` libs
   // ...
}

For what concern the callback to be provided in the URL, this can be dynamically provided with an incremental name:

window[`__googleMapsCallback_${++this.cbNo}`] = this.callback.bind(this);

url += `?callback=__googleMapsCallback_${this.cbNo}`;

Thanks,
Indri

Metadata

Metadata

Assignees

No one assigned

    Labels

    priority: p1Important issue which blocks shipping the next release. Will be fixed prior to next release.type: feature request‘Nice-to-have’ improvement, new feature or different behavior or design.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions