npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

draw-on-google-maps

v2.0.1

Published

Draw polygons, markers, polylines, circles, rectangles, and freehand strokes on Google Maps.

Readme

Draw On Google Map

npm version License: MIT

draw-on-google-maps is a framework-agnostic TypeScript library for drawing and managing overlays on Google Maps.

Supported tools:

  • Brush (freehand)
  • Polygon
  • Polyline
  • Circle
  • Rectangle
  • Marker

Installation

npm install draw-on-google-maps

Google Maps Loading Options

The library works with all common Google Maps JS loading patterns.

1) Direct script tag

<script src="https://maps.googleapis.com/maps/api/js?key=YOUR_API_KEY&libraries=marker,geometry"></script>
<script src="https://unpkg.com/draw-on-google-maps@latest/dist/draw-on-google-map.umd.js"></script>
<script>
  const map = new google.maps.Map(document.getElementById('map'), {
    center: { lat: 30.0444, lng: 31.2357 },
    zoom: 10,
    mapId: 'YOUR_MAP_ID',
  });

  const draw = new DrawOnGoogleMap(map);
</script>

2) Dynamic library import (google.maps.importLibrary)

<script>
  (g => {
    let h, a, k, p = 'The Google Maps JavaScript API', c = 'google', l = 'importLibrary', q = '__ib__', m = document, b = window;
    b = b[c] || (b[c] = {});
    const d = b.maps || (b.maps = {}), r = new Set(), e = new URLSearchParams();
    const u = () => h || (h = new Promise(async (f, n) => {
      a = m.createElement('script');
      e.set('key', g.key);
      e.set('v', g.v || 'weekly');
      e.set('libraries', [...r] + '');
      e.set('callback', c + '.maps.' + q);
      a.src = `https://maps.${c}apis.com/maps/api/js?` + e;
      d[q] = f;
      a.onerror = () => n(Error(p + ' could not load.'));
      m.head.append(a);
    }));
    d[l] ? console.warn(p + ' only loads once. Ignoring:', g) : d[l] = (f, ...n) => r.add(f) && u().then(() => d[l](f, ...n));
  })({ key: 'YOUR_API_KEY' });
</script>
<script type="module">
  import DrawOnMap from 'https://cdn.jsdelivr.net/npm/draw-on-google-maps@latest/dist/draw-on-google-map.es.js';

  const { Map } = await google.maps.importLibrary('maps');
  await google.maps.importLibrary('geometry');
  await google.maps.importLibrary('marker');

  const map = new Map(document.getElementById('map'), {
    center: { lat: 30.0444, lng: 31.2357 },
    zoom: 10,
    mapId: 'YOUR_MAP_ID',
  });

  const draw = new DrawOnMap(map);
</script>

3) NPM @googlemaps/js-api-loader

import { setOptions, importLibrary } from '@googlemaps/js-api-loader';
import DrawOnMap from 'draw-on-google-maps';

setOptions({ key: process.env.GOOGLE_MAPS_API_KEY!, v: 'weekly' });

const { Map } = await importLibrary('maps');
await importLibrary('geometry');
await importLibrary('marker');

const map = new Map(document.getElementById('map') as HTMLElement, {
  center: { lat: 30.0444, lng: 31.2357 },
  zoom: 10,
  mapId: 'YOUR_MAP_ID',
});

const draw = new DrawOnMap(map);

Notes:

  • marker is required for AdvancedMarkerElement support.
  • geometry is recommended for accurate circle radius calculations (library includes fallback when missing).
  • Use a valid mapId when advanced markers are expected.

Bundled Files / CDN Hosting

Bundled files are published to npm and can be consumed from CDNs:

  • UMD: https://unpkg.com/draw-on-google-maps@latest/dist/draw-on-google-map.umd.js
  • UMD: https://cdn.jsdelivr.net/npm/draw-on-google-maps@latest/dist/draw-on-google-map.umd.js
  • ESM: https://cdn.jsdelivr.net/npm/draw-on-google-maps@latest/dist/draw-on-google-map.es.js

Quick Start (NPM)

import DrawOnMap from 'draw-on-google-maps';

const map = new google.maps.Map(document.getElementById('map') as HTMLElement, {
  center: { lat: 30.0444, lng: 31.2357 },
  zoom: 10,
  mapId: 'YOUR_MAP_ID',
});

const draw = new DrawOnMap(map);
draw.changeColor('#ff4d4f');
draw.changeStrokeWeight(4);
draw.brush.startDraw();

Tool API

Each tool exposes:

  • startDraw()
  • stopDraw()
  • clearArt()

Tool entrypoints:

  • draw.brush
  • draw.polygon
  • draw.polyline
  • draw.circle
  • draw.rectangle
  • draw.marker

Tool-specific methods:

  • draw.polygon.changeFillColor(color: string)
  • draw.polygon.changeOpacity(opacity: number)
  • draw.marker.changeIcon(icon: string)

Global API

  • changeColor(color: string)
  • changeStrokeWeight(weight: number)
  • changePolygonFillColor(color: string)
  • changePolygonOpacity(opacity: number)
  • changeMarkerIcon(icon: string)
  • getSelectedTool(): 'BRUSH' | 'POLYGON' | 'POLYLINE' | 'CIRCLE' | 'RECTANGLE' | 'MARKER' | null
  • getSelectedColor(): string
  • clearAllArt()
  • destroy()

Events API

const unsubscribe = draw.on('shapeCreated', ({ tool, shape }) => {
  console.log(tool, shape.id);
});

unsubscribe();

Available events:

  • toolChanged
  • shapeCreated
  • shapeCleared
  • clearedAll
  • imported
  • exported
  • error

You can also remove listeners with draw.off(eventName, handler).

Serialization API

const json = draw.exportData('json');
const geojson = draw.exportData('geojson');

draw.importData(json);
draw.importData(geojson, { clearExisting: true });

Accepted import payloads:

  • DrawShape[]
  • { shapes: DrawShape[] }
  • GeoJSON FeatureCollection

Framework Usage

The package works with Vanilla JS, React, Vue, Angular, and any environment where Google Maps API is loaded.

See examples in:

  • examples/vanilla
  • examples/react
  • examples/vue
  • examples/angular

Migration from v1.0.4 to v2.0.0

Breaking changes:

  • Internal architecture was rewritten in TypeScript.
  • The legacy DrawingManager-based flow is no longer used.
  • New tools are available (polyline, circle, rectangle).

Compatibility notes:

  • Existing public entrypoints (draw.brush.startDraw(), draw.polygon..., draw.marker...) are preserved.
  • getSelectedTool() now safely returns null when no tool is active.

Security Best Practices

  • Never commit real API keys, tokens, certificates, or .env files.
  • Use environment variables and CI/CD secret managers for all credentials.
  • Run npm run audit before publishing.

Troubleshooting

  • Google Maps JavaScript API is not loaded. : Ensure the API is loaded before new DrawOnMap(map).
  • Circle radius not accurate : Include geometry in your loaded libraries.
  • Markers do not render as expected : Include marker and use a valid mapId.
  • Multiple map instances influence each other : Use one DrawOnMap instance per map container.

License

MIT