Skip to content

fix(map-drawing-terradraw): initialize drawing session only once on projection_changed - #1695

Open
zigzagdev wants to merge 1 commit into
googlemaps-samples:mainfrom
zigzagdev:fix/terradraw-projection-changed-once
Open

zigzagdev wants to merge 1 commit into
googlemaps-samples:mainfrom
zigzagdev:fix/terradraw-projection-changed-once

Conversation

@zigzagdev

Copy link
Copy Markdown

Summary

Motivation

samples/map-drawing-terradraw/index.ts set up the entire drawing session (the TerraDraw instance, its select/deselect/change handlers, the export/upload/resize/delete/undo/redo button handlers, and a document.addEventListener('keydown', ...) rotate-on-r handler) inside a map.addListener('projection_changed', ...) callback.
projection_changed is not a fires-once-at-startup event — it can fire again later, e.g. when the map's rendering/map type changes.
Therefore this sample enables mapTypeControl: true, the built-in map type control is clickable at any time, including mid-drawing.

If projection_changed fired a second time:

  • The live TerraDraw instance was silently replaced with a new one (without calling .stop() on the old one), wiping the user's in-progress drawing from the map without warning.
  • A new document.addEventListener('keydown', ...) handler was added on top of the existing one, since addEventListener (unlike the .onclick = button handlers) doesn't overwrite previous listeners. Each additional firing stacked one more global keydown listener, so pressing r with a feature selected would duplicate the rotated feature once per prior map-type switch.

What I have done

Changed map.addListener('projection_changed', ...) to google.maps.event.addListenerOnce(map, 'projection_changed', ...) so the one-time setup logic can only ever run once, regardless of how many times projection_changed fires afterward.

Test Plans

  • Manually verified the logic: with addListenerOnce, the setup block (TerraDraw instance creation, button handlers, and the keydown rotate listener) is guaranteed to execute exactly once per page load, so a subsequent projection_changed firing (e.g. from toggling the map type control) no longer replaces the active drawing session or registers an additional keydown listener.

…rojection_changed

projection_changed can fire again after the map's rendering/map type changes
(e.g. clicking the built-in map type control, which this sample enables). The
setup block ran on every firing, silently replacing the live TerraDraw
instance (wiping in-progress drawings) and adding a duplicate global keydown
listener each time. Use addListenerOnce so the one-time setup can only ever
run once.
@google-cla

google-cla Bot commented Oct 6, 2026

Copy link
Copy Markdown

Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

View this failed invocation of the CLA check for more information.

For the most up to date status, view the checks section at the bottom of the pull request.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant