Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dynamic Polyline Change #242

Open
HusamElbashir opened this issue Mar 29, 2024 Discussed in #241 · 3 comments
Open

Dynamic Polyline Change #242

HusamElbashir opened this issue Mar 29, 2024 Discussed in #241 · 3 comments
Labels
bug Something isn't working has workaround reactivity

Comments

@HusamElbashir
Copy link
Collaborator

Discussed in #241

Originally posted by Krada88 March 28, 2024
Hello, Friends. Tell me I have a problem.

I'm doing something like a geotracker, I need to periodically update the Polyline in real time. But I came across the fact that it doesn't work.

Maybe I'm doing something wrong? I will be grateful for the hint. And thanks a lot for earlier.

Here is my code.

I created a small sample code that doesn't work. In real life, the Polyline update should occur when the geolocation is changed, but it doesn't work there either. I deleted the api key from the code =)

<template>
  <GoogleMap
    style="width: 100%; height: 500px"
    :center="center"
    :zoom="3"
  >
    <Polyline :options="flightPath" />
  </GoogleMap>
</template>

<script setup>
import { ref } from 'vue'
import { GoogleMap, Polyline } from 'vue3-google-map'

const center = { lat: 0, lng: -180 }
const flightPlanCoordinates = ref([
  { lat: 37.772, lng: -122.214 },
  { lat: 21.291, lng: -157.821 },
  { lat: -18.142, lng: 178.431 },
  { lat: -27.467, lng: 153.027 },
])
const flightPath = ref({
  path: flightPlanCoordinates.value,
  geodesic: true,
  strokeColor: '#FF0000',
  strokeOpacity: 1.0,
  strokeWeight: 2,
})

const addNewCoordinate = () => {
 
  const newCoordinate = {
    lat: Math.random() * 180 - 90,
    lng: Math.random() * 360 - 180,
  }

  flightPlanCoordinates.value.push(newCoordinate)

  flightPath.value = {
    ...flightPath.value,
    path: flightPlanCoordinates.value,
  }
}

setInterval(addNewCoordinate, 1000)
</script>

```</div>
@HusamElbashir HusamElbashir added bug Something isn't working reactivity labels Mar 29, 2024
@Krada88
Copy link

Krada88 commented Apr 2, 2024

Hi. Guys, can you tell me if the problem is solvable? Unfortunately, I can't beat her myself =(

@HusamElbashir
Copy link
Collaborator Author

HusamElbashir commented Apr 2, 2024

As a workaround you can create a new path array every time instead of pushing to the old one

const addNewCoordinate = () => {
  const newCoordinate = {
    lat: Math.random() * 180 - 90,
    lng: Math.random() * 360 - 180,
  }

  flightPath.value = {
    ...flightPath.value,
    path: [...flightPath.value.path, newCoordinate],
  }
}

@Krada88
Copy link

Krada88 commented Apr 2, 2024

Thank you very much. It just happened!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working has workaround reactivity
Projects
None yet
Development

No branches or pull requests

2 participants