Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

261
Visualizações
How to remove Google map markers in Vue?

I'm using Google API to display a map, markers, and polygons.
When I add a new marker, the previous markers are not gone.

I save the map, markers & polygons inside my data state.

data() {
    return {
    google: window.google,
    map: null,
    activeInfoWindow: null,
    polygons: [],
    markers: []
    }
},

I tried to remove the previous markers with marker.setMap(null) before showing new markers

filtersResults: function (filterResults) {
    // REMOVE PREVIOUS ACTIVE MARKER
    this.markers.map((marker) => marker.setMap(null))
},

I tried to empty the markers array too. Nothing happens, the markers still show on the map. enter image description here enter image description here

The setMap is not undefined, this is the result when I'm console.log(marker.setMap)

ƒ (c){try{this.set(a,b(c))}catch(d){_.Pe(_.Oe("set"+_.Bf(a),d))}}
  data() {
      return {
        google: window.google,
        map: null,
        activeInfoWindow: null,
        polygons: [],
        markers: []
      }
    },
    async mounted() {
      const { LatLng, Map, MapTypeId } = this.google.maps
        this.map = new Map(document.getElementById('map'), {
            zoom: 10,
            center: new LatLng(process.env.INITIAL_LAT, process.env.INITIAL_LNG),
            mapTypeId: MapTypeId.ROADMAP
        })
    },
    watch: {
      filtersResults: function (filterResults) {
        // REMOVE PREVIOUS ACTIVE MARKER
        this.markers.map((marker) => marker.setMap(null))
        this.markers = []
      },
      assets: {
        handler: function (newValue) {
          const { map } = this
          if (isNotNullAndUndefined(newValue) && map) {
            
            // ! GENERATE MARKER IN HERE
            this.markers = newValue.map((value) => {
              const { location } = value
              return this.generateMarkers({
                latitude: dotFormat(location.lat),
                longitude: dotFormat(location.lng),
                details: value
              })
            })
          }
        },
        immediate: true
      }
    },
    methods: {
      generateMarkers({ latitude, longitude, details }) {
        const { map, google } = this
        const { LatLng, Marker } = google.maps

        const marker = new Marker({
          position: new LatLng(latitude, longitude),
          draggable: false
        })

        marker.setMap(map)
        return marker
      },
    }
  }
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

When we store the Markers in the markers array, vue.js 3 converts them to Proxy objects.
That causes a problem when we later call Proxy.setMap(null); Google Maps somehow doesn't like that, probably because the Proxy object is not === the original Marker object added to the map initially.

The workaround I found was to use toRaw() before using any Google Maps objects that were converted to Proxy objects:

import {toRaw} from 'vue';

this.markers.map((marker) => toRaw(marker).setMap(null))
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda