Google Maps V3 Utility Libraries (Community Fork)
This package is a community-maintained fork of the Google Maps JavaScript API v3 utility libraries, initially updated around 2018. It bundles a collection of client-side JavaScript components such as MarkerClusterer, InfoBox, InfoBubble, KeyDragZoom, and RouteBoxer. These utilities are designed to extend the core functionality of Google Maps v3 by providing enhanced marker management, custom information windows, and map interaction tools. Despite its `latest` moniker, the package's content reflects updates from 2018, making its compatibility with recent Google Maps API versions potentially limited. The current version is 1.0.1, and it does not appear to follow an active release cadence, suggesting it is in a frozen or abandoned state, differing from the potentially more current official Google Maps utility library repositories.
Common errors
-
TypeError: google.maps.MarkerClusterer is not a constructor
cause The Google Maps API global object (`google`) is not loaded or the MarkerClusterer utility is not correctly imported and attached to the `google.maps` namespace.fixEnsure the Google Maps JavaScript API script tag is loaded before your application code, and that you are importing `MarkerClusterer` directly from the package: `const MarkerClusterer = require('googlemaps-v3-utility-library-latest');` -
ReferenceError: google is not defined
cause The Google Maps JavaScript API has not been loaded into the browser environment.fixMake sure to include the Google Maps JavaScript API script tag in your HTML `head` or `body` with your API key and `callback` parameter, before any code that attempts to use the `google` object. Example: `<script src="https://maps.googleapis.com/maps/api/js?key=YOUR_API_KEY&callback=initMap"></script>` -
Error: Cannot find module 'googlemaps-v3-utility-library-latest/lib/InfoBox'
cause Incorrect path specified for a utility library or the file does not exist at the expected location after installation.fixDouble-check the exact casing and path for the desired utility library. Ensure the package is correctly installed via `npm i`. The `/lib` directory contains most individual utilities.
Warnings
- breaking This specific `googlemaps-v3-utility-library-latest` package reflects updates from 2018. It may not be fully compatible with newer versions of the Google Maps JavaScript API (e.g., v3.40+ or changes after 2018), potentially leading to unexpected behavior or breakage.
- deprecated The 'Google Earth API for Google Maps v3' utility included in this collection is explicitly marked as deprecated within the package's own README. Using it may lead to errors or unsupported functionality.
- gotcha This package is primarily designed for CommonJS/UMD environments. Direct ESM `import` statements may require bundler configuration or specific import paths (e.g., `import * as pkg from 'package'`) to resolve correctly, especially if relying on Node.js module resolution.
- gotcha The package name `googlemaps-v3-utility-library-latest` can be misleading. While it implies 'latest', its contents are from 2018. Relying on this package for 'latest' features or bug fixes from the Google Maps API can lead to outdated functionality.
- gotcha The project's GitHub link in the README (github.com/nblavoie/v3-utility-library) appears to be broken or redirects. Support is directed to the main Google Maps GitHub repository, implying this specific npm package might not have dedicated support channels.
Install
-
npm install googlemaps-v3-utility-library-latest -
yarn add googlemaps-v3-utility-library-latest -
pnpm add googlemaps-v3-utility-library-latest
Imports
- MarkerClusterer
import { MarkerClusterer } from 'googlemaps-v3-utility-library-latest'; // Or (incorrect path): import MarkerClusterer from 'googlemaps-v3-utility-library-latest/markerclusterer';import MarkerClusterer from 'googlemaps-v3-utility-library-latest'; // Or for CommonJS: const MarkerClusterer = require('googlemaps-v3-utility-library-latest'); - InfoBox
import { InfoBox } from 'googlemaps-v3-utility-library-latest'; // Or (incorrect path): import InfoBox from 'googlemaps-v3-utility-library-latest/InfoBox';import InfoBox from 'googlemaps-v3-utility-library-latest/lib/InfoBox'; // Or for CommonJS: const InfoBox = require('googlemaps-v3-utility-library-latest/lib/InfoBox'); - KeyDragZoom
import { KeyDragZoom } from 'googlemaps-v3-utility-library-latest';import KeyDragZoom from 'googlemaps-v3-utility-library-latest/lib/KeyDragZoom'; // Or for CommonJS: const KeyDragZoom = require('googlemaps-v3-utility-library-latest/lib/KeyDragZoom');
Quickstart
import MarkerClusterer from 'googlemaps-v3-utility-library-latest';
function initMap() {
const map = new google.maps.Map(document.getElementById('map'), {
zoom: 3,
center: { lat: -28.024, lng: 140.887 },
mapId: 'DEMO_MAP_ID' // Replace with your Map ID
});
const markers = [];
// Create some random markers
for (let i = 0; i < 100; i++) {
const latLng = new google.maps.LatLng(
-28.024 + Math.random() * 10,
140.887 + Math.random() * 10
);
markers.push(new google.maps.Marker({
position: latLng
}));
}
// Add a marker clusterer to the map.
new MarkerClusterer(map, markers, {
imagePath: 'https://developers.google.com/maps/documentation/javascript/examples/markerclusterer/m'
});
}
// Ensure the global `google` object is available (e.g., via script tag in HTML)
// For demonstration purposes, if not using a global callback, call directly:
// This assumes the Google Maps API script is loaded globally and defines `google`
// For a real application, follow Google's recommended async loading with a callback.
window.addEventListener('DOMContentLoaded', () => {
if (typeof google !== 'undefined' && google.maps) {
initMap();
} else {
console.warn('Google Maps API not loaded. Ensure the script tag is present.');
// You might want to dynamically load it here or handle the missing global differently
const script = document.createElement('script');
script.src = `https://maps.googleapis.com/maps/api/js?key=${process.env.GOOGLE_MAPS_API_KEY ?? 'YOUR_API_KEY'}&callback=initMap`;
script.async = true;
document.head.appendChild(script);
window.initMap = initMap; // Attach to window for the callback
}
});