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

200
Visualizações
Adding Multiple Markers on Vue JS Google Maps

I have Google Maps embedded on Vue JS code. But I want to show multiple markers using latitude and longitude on that map. Any idea on how to do that? Here is my code.

Maps.vue

<template>
    <card type="plain" title="Google Maps">
      <div id="map" class="map">
      </div>
    </card>
</template>
<script>
export default {
  mounted() {
    let myLatlng = new window.google.maps.LatLng(40.748817, -73.985428);
    let mapOptions = {
      zoom: 13,
      center: myLatlng,
      scrollwheel: false, //we disable de scroll over the map, it is a really annoing when you scroll through page
      styles: [{
        "elementType": "geometry",
        "stylers": [{
          "color": "#1d2c4d"
        }]
      },
        {
          "elementType": "labels.text.fill",
          "stylers": [{
            "color": "#8ec3b9"
          }]
        },
        {
          "elementType": "labels.text.stroke",
          "stylers": [{
            "color": "#1a3646"
          }]
        },
        {
          "featureType": "water",
          "elementType": "labels.text.fill",
          "stylers": [{
            "color": "#4e6d70"
          }]
        }
      ]
    };
    let map = new window.google.maps.Map(
      document.getElementById("map"),
      mapOptions
    );

    let marker = new window.google.maps.Marker({
      position: myLatlng,
      title: "Hello World!"
    });

   
    marker.setMap(map);
  }
};
</script>

How can I display multiple markers on this google map? Any advice or tips would be really helpful. Thanks.

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

You can use the gmap-vue plugin for VueJS to add multiple markers by clicking on the map.

First, initialize your Google Map, and make sure to add an array of markers: [] that will contain all the markers that you will be adding on the map:

<template>
  <div>
    <gmap-map
      :center="center"
      ref="mmm"
      :zoom="12"
      style="width: 100%; height: 400px"
      @click="addMarker"
    >
      <gmap-marker
        :key="index"
        v-for="(m, index) in markers"
      />
    </gmap-map>
  </div>
</template>


export default {
  name: "GoogleMap",
  data() {
    return {
      center: { lat: 45.508, lng: -73.587 },
      markers: [],
    };
  },
  
};

You can add markers by calling the @click="addMarker" event on <gmap-map>. Then, on the your methods you can add the addMarker(event) function where you should be able to get the click coordinates from the map. You can now

Here is what it looks like:

methods: {
    addMarker(event) {
      const marker = {
        lat: event.latLng.lat(),
        lng: event.latLng.lng(),
      };
      console.log(marker);
      this.markers.push({ position: marker });
      this.$refs.mmm.panTo(marker);
      //this.center = marker;
  },
},

Here is a working codesandbox for your reference, just put your API key on the main.js file: https://codesandbox.io/s/gifted-fast-b41ps

about 4 years ago · Juan Pablo Isaza Relatório

0

You can use- but you don't need any plug-in. It doesn't really save time in this use-case imho.

With the standard implementation it would look something like that:

this.yourPOIs.forEach(poi => {
    var myLatLng = { lat: poi.latitude, lng: poi.longitude };
    var marker = new google.maps.Marker({
        position: myLatLng,
        map: this.myMap,
        title: poi.annotation
        // you can also add data here with key names that are not in use like for example "uuid: 1" so you can access that data later on if you click on markers or iterate through them
    });
    marker.addListener('click', function() {
        console.log(marker.uuid); // if you want to react on clicking on a marker
    });
    this.addedMarkers.push(marker);
});

myMap being the map object written in a local variable instead of writing it in a let-variable (you will probably need to access the map later on so you better safe them in the local data of the component or your vuex store).

That's also why i pushed the markers in an array of the component. By that you can later on do this to delete the markers again (for e.g. updating the map):

this.addedMarkers.forEach(marker => {
     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