@frankhoodbs/gmaps-utils
v1.3.2
Published
Utility for load the Google Maps JS SDK
Downloads
282
Keywords
Readme
Gmaps Utils
A lightweight utility to help initialize Google Maps JavaScript API with ease.
Features
- Simplified Google Maps JavaScript API initialization.
- Supports setting language and region.
- Uses the latest version of Google Maps on a weekly basis.
- Includes the 'places' library by default.
Usage
- Import the utility:
import gMapsInit, { GmapsInitOptions } from './path-to-gMapsInit-file';
- Initialize Google Maps:
const gmapsOptions: GmapsInitOptions = {
apiKey: 'YOUR_API_KEY',
language: 'en', // Optional. Default is 'it'
region: 'US' // Optional. Default is 'IT'
};
gMapsInit(gmapsOptions)
.then((googleMaps) => {
// Google Maps is now initialized and can be used.
const map = new googleMaps.Map(document.getElementById('map'), {
center: {lat: -34.397, lng: 150.644},
zoom: 8
});
})
.catch((error) => {
console.error('Error loading Google Maps', error);
});
API Reference
GmapsInitOptions
apiKey
(string): Required. Your Google Maps API key.language
(string, optional): Preferred language for the maps display. Default is 'it'.region
(string, optional): Region biasing for address localization. Default is 'IT'.
gMapsInit(options: GmapsInitOptions): Promise<google.maps.MapsLibrary>
Initializes the Google Maps JavaScript API and returns a promise. When resolved, you can use the Google Maps API as you normally would.