Skip to content

Bug: impossible to remove markers with v-if or v-for #301

@basuneko

Description

@basuneko

Please complete all sections that you can but please don't remove any section, otherwise the bot will close your issue because it doesn't meet our issue template (you can remove this comment)

Describe the bug

Hi, I'm working a project where the user can select things and corresponding markers should be added/removed from the map.
I have code similar to this:

<GmvMap>
  <GmvMarker v-if="showMarker" ... />

  <GmvMarker v-for="marker in currentlyVisibleMarkers" ... />
</GmvMap

I can add markers to the map with no problem. But when I try to remove them (e.g. by setting showMarker to false, or by changing the currentlyVisibleMarkers array), the markers remain on the map.

💡 Updated:
After a lot of monkey-patchning I believe I've gotten to the bottom of it: google maps api doesn't work well with proxies.

// components/marker-icon.vue
onUnmounted(() => {
  ...
  } else if (markerInstance.value) {
    console.log(markerInstance.value) // this is a proxy object.
    markerInstance.value.setMap(null);
  }
});

The above code is executed correctly. However, the markerInstance.value object is not the actual google maps api' marker - it's a reactivity proxy. I'm not sure if the issue is in the maps api or in vue reactivity system but it just doesn't do what it's supposed to.

Fix

The solution seems to be to unproxify the object:

const rawMarkerInstance = toRaw(markerInstance.value)
rawMarkerInstance.setMap(null);

And this seems to work as expected.

To reproduce

Code sandbox
github
(branch: gmap-vue-bug-repro)

Steps to reproduce the behavior:

  1. Open the code sandbox repro
  2. Enter your google maps api key, the page will reload
  3. click "Show marker", "Show markers list"
  4. click "Hide marker", "Hide markers list"

Expected behavior

The markers should appear and then get removed

Current behavior

The markers appear but never get removed from the map

Screenshots

Desktop (please complete the following information)

  • OS: MacOS
  • Browser Chrome
  • Version 114.0.5735.198

Smartphone (please complete the following information)

Additional context

Versions

  • Node: v20.2.0
  • NPM: 9.7.1

Package manager

NPM

Plugin version

@gmap-vue/[email protected]

Metadata

Metadata

Assignees

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions