fawmi / vue-google-maps

Reactive Vue 3 components for Google maps
https://vue-map.netlify.app
MIT License
197 stars 103 forks source link

unable to set the marker animation on and off as intended #213

Closed a8300 closed 4 months ago

a8300 commented 4 months ago

Event xyz is not working? Hello, the code below does not set the marker animation on and off as intended. Here is the code:

<template>
  <GMapMap
      :center="center"
      :zoom="7"
      map-type-id="terrain"
      style="width: 500px; height: 300px"
  >
      <GMapMarker
          :key="index"
          v-for="(marker, index) in markers"
          :position="marker.position"
          :clickable="true"
          :animation="marker.animation"
          @click="clickedMarker(index)"
      />
   </GMapMap>
</template>
<script setup lang="ts">
let center = { lat: 51.093048, lng: 6.842120 };
let markers = [{animation: 0, "position":{ lat: 51.093048, lng: 6.842120 }}];

const clickedMarker = (index: number) => {
  if (markers[index].animation === 0) {
    markers[index].animation = 1;
  } else {
    markers[index].animation = 0;
  }
};
</script>

Describe the bug

To Reproduce Steps to reproduce the behavior:

1, Copy and paste the code above into the project

2, Install the dependencies and run the project

3., Click on the marker, the marker animation does not start (set to 1). Instead it takes the initial value which is 0.

Expected behavior The marker animation should start (animation should be set to 1).

Desktop (please complete the following information):

Additional context Thank you for your help

a8300 commented 4 months ago

Looking at the marker.js code, animation uses a Number class. I tried to initialize animation in my code as a Number but recieved the same result (no marker animation). When clicked, the animation variable in my code changes from zero to one and back to zero.

Screenshot 2024-07-16 at 12 58 41 AM

Code that initializes animation as a Number and sets it to zero then sets it to one if the user clicks on the marker

<template>
  <GMapMap
      :center="center"
      :zoom="7"
      map-type-id="terrain"
      style="width: 500px; height: 300px"
  >
      <GMapMarker
          :key="index"
          v-for="(marker, index) in markers"
          :position="marker.position"
          :clickable="true"
          :animation="marker.animation"
          @click="clickedMarker(index)"
      />
   </GMapMap>
</template>
<script setup lang="ts">
import { onBeforeMount } from 'vue';

let center = { lat: 51.093048, lng: 6.842120 };
let markers = new Array();
let animation: Number =  new Number(0)

onBeforeMount(() => {
  markers.push({animation: animation, "position":{ lat: 51.093048, lng: 6.842120 }});
})

const clickedMarker = (index: number) => {
  if (markers[index].animation === 0) {
    console.log("animation = 0")
    markers[index].animation = 1;
  } else {
    console.log("animation = 1")
    markers[index].animation = 0;
  }
};

</script>

Code (in the marker.js of the vue-google-maps library) that shows the animation type is a Number

  animation: {
    twoWay: true,
    type: Number,
  },
a8300 commented 4 months ago

After receiving some help I managed to resolve the issue, here is the fix.

<template>
  <GMapMap
      :center="center"
      :zoom="7"
      map-type-id="terrain"
      style="width: 500px; height: 300px"
  >
      <GMapMarker
          :key="index"
          v-for="(marker, index) in markers"
          :position="marker.position"
          :clickable="true"
          :animation="marker.animation"
          @click="clickedMarker(index)"
      />
   </GMapMap>
</template>
<script setup lang="ts">
import { onBeforeMount } from 'vue';
import { ref } from 'vue'
import { Marker } from './Marker'
let center = { lat: 51.093048, lng: 6.842120 };
let markers = ref<Marker[]>([]);

onBeforeMount(() => {
  markers.value.push({ animation: 0, position: { lat: 51.093048, lng: 6.842120 } });
})

const clickedMarker = (index: number) => {
  if (markers.value[index].animation === 0) {
    console.log("animation = 0")
    markers.value[index].animation = 1;
  } else {
    console.log("animation = 1")
    markers.value[index].animation = 0;
  }
};

</script>

Thank you