inocan-group / vue3-google-map

A set of composable components for easy use of Google Maps in your Vue 3 projects.
https://vue3-google-map.com
MIT License
280 stars 57 forks source link

Updating a marker on button click #56

Closed web-programmer-here closed 2 years ago

web-programmer-here commented 2 years ago

I am trying to update a marker to a new location after a click but cant get it to update

https://codesandbox.io/s/romantic-glitter-32wjh?file=/src/frontend/components/Content.vue

Here is a simple implementation I did:

<template>
  <div>
    <h2>Map Component</h2>
    <button @click="updateLocation()">Update Location</button>
    <GoogleMap
      api-key=""
      style="width: 100%; height: 500px"
      :center="center"
      :zoom="15"
    >
      <Marker :options="{ position: initialLocation }" />
    </GoogleMap>
  </div>
</template>

<script>
import { GoogleMap, Marker } from "vue3-google-map";

export default {
  name: "Content",
  props: {
    data: Object,
  },
  components: { GoogleMap, Marker },

  data() {
    return {
      center: { lat: 40.689247, lng: -74.044502 },
      initialLocation: { lat: 40.689247, lng: -74.044502 },
    };
  },
  methods: {
    updateLocation() {
      this.initialLocation.lat = 40.74860303874883;
      this.initialLocation.lng = -73.98562148637158;
    },
  },
};
</script>
HusamElbashir commented 2 years ago

@web-programmer-here As a workaround you can re-assign initialLocation

updateLocation() {
    this.initialLocation = {
        lat: 40.74860303874883,
        lng: -73.98562148637158
    }
}
web-programmer-here commented 2 years ago

@HusamIbrahim Thanks, that worked