Conversation
…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.
|
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. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Motivation
samples/map-drawing-terradraw/index.tsset up the entire drawing session (theTerraDrawinstance, itsselect/deselect/changehandlers, the export/upload/resize/delete/undo/redo button handlers, and adocument.addEventListener('keydown', ...)rotate-on-rhandler) inside amap.addListener('projection_changed', ...)callback.projection_changedis 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_changedfired a second time:TerraDrawinstance 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.document.addEventListener('keydown', ...)handler was added on top of the existing one, sinceaddEventListener(unlike the.onclick =button handlers) doesn't overwrite previous listeners. Each additional firing stacked one more global keydown listener, so pressingrwith a feature selected would duplicate the rotated feature once per prior map-type switch.What I have done
Changed
map.addListener('projection_changed', ...)togoogle.maps.event.addListenerOnce(map, 'projection_changed', ...)so the one-time setup logic can only ever run once, regardless of how many timesprojection_changedfires afterward.Test Plans
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 subsequentprojection_changedfiring (e.g. from toggling the map type control) no longer replaces the active drawing session or registers an additionalkeydownlistener.