Map Loader for Google Maps Platform JavaScript API v3
Description
Automatically loads a Google map into your website with a few lines of JavaScript or TypeScript. You provide the ID of the div where you want the map initialized or appended to, your settings for what the map looks like, and optionally your settings for loading the Maps JS API, and this module handles the rest.
Loading of the Google Maps Platform JavaScript API is taken care of for you asynchronously courteousy of @googlemaps/js-api-loader.
NPM
Available via NPM as the package @googlemaps/map-loader
Example
Set map loader options
import { GoogleMap } from 'map-loader';
const googleMapsAPIKey = "YOUR API KEY";
const mapOptions = {
center: {
lat: 47.649196,
lng: -122.350384
},
zoom: 12
}
const apiOptions = {
version: 'weekly',
libraries: ['places']
}
const mapLoaderOptions = {
apiKey: googleMapsAPIKey,
divId: 'google_map',
append: true,
mapOptions: mapOptions,
apiOptions: apiOptions
};
Load the map:
const mapLoader = new GoogleMap();
mapLoader
.initMap(mapLoaderOptions)
.then(googleMap => {
});
Or if you like, with async/await
const mapLoader = new GoogleMap();
const await googleMap = mapLoader.initMap(mapLoaderOptions);
Documentation
If you're using TypeScript, the map loader exports interfaces for MapLoaderOptions
and MapsJSAPIOptions
. Options for base map settings are provided courteousy of google.maps.MapOptions in @types/googlemaps.
MapLoaderOptions
MapsJSAPIOptions
Property | Type | Description |
---|
version | string | Optional. Defaults to weekly . Version of the Maps JS API to load. For more information on supported values, see Release channels and version numbers. |
libraries | Array | Optional. Additional Maps JS API libraries to load. Supported values are drawing , geometry , places , visualization . |
language | string | Optional. Defaults to en . The language code of the language to localize the map to. For more information on supported values, see supported languages. |
region | string | Optional. Defaults to us . Sets the Unicode region subtag identifier of the region to localize the map to. For more information on supported values, see Region localization. |
Support
This library is community supported. We're comfortable enough with the stability and features of
the library that we want you to build real production applications on it.
If you find a bug, or have a feature suggestion, please log an issue. If you'd like to
contribute, please read How to Contribute.