【问题标题】:How do I display my current position using Vue Google Maps如何使用 Vue Google Maps 显示我的当前位置
【发布时间】:2020-04-19 21:15:35
【问题描述】:

当我运行我的代码时,会显示一个坐标为 0、0 的地图,这是我初始化它的位置,但是当我将 lat 设置为 position.coords.latitude 时,该位置并没有更新到我当前的位置和 lng 到 position.coords.longitude。有什么想法可能是错的吗?

<template>
        <GmapMap
            :center="{lat: center.lat, lng: center.lng}"
            ...
          />
</template>


<script>
export default {
data () {
  return {
    center : { 
      lat : 0, 
      lng : 0 
    }
  }
},
methods: {
  getCurrentPosition() {
    Geolocation.getCurrentPosition().then(position => {
      console.log('Current', position)
      this.position = position
      this.center = {
          lat: position.coords.latitude,
          lng: position.coords.longitude
        }
    })
  },
	mounted () {
    this.getCurrentPosition(
    this.geoId = Geolocation.watchPosition({enableHighAccuracy: true}, (position, err) => {
      this.position = position
    })
  },
}
</script>

【问题讨论】:

    标签: javascript vue.js google-maps-api-3 quasar-framework


    【解决方案1】:

    您确实应该提供有关您正在使用的依赖项的更多信息,并提供适当的代码示例(您一开始给我们的代码不会编译)。


    从我所看到的代码来看,这似乎是一个反应性问题。

    如果您希望 Vue 在您更新对象 时注册更改,请务必使用Vue.set 来执行此操作。

    代替

    this.center = {
      lat: position.coords.latitude,
      lng: position.coords.longitude
    }
    

    使用:

    Vue.set(this, 'center', {
      lat: position.coords.latitude,
      lng: position.coords.longitude
    })
    

    或者,你可以使用这个技巧:

    this.center = Object.assign({}, this.center, {
      lat: position.coords.latitude,
      lng: position.coords.longitude
    })
    

    Vue.js Reactivity in Depth

    【讨论】:

    • 对于缺乏信息和不完整的代码,我深表歉意。起初我有完整的代码,但它说我无法提交,因为与文本相比,代码量很大。我想我应该用依赖信息填充文本。感谢您的回复。我正在尝试使用 Object.assign,但地图仍然没有从原始坐标移向当前位置。我知道我的位置正在更新,因为我在更新时进行控制台日志记录,但地图没有移动。地图会有更具体的问题吗?或者我应该在mounted中使用Object.assign?
    • 如果你想通了,请将答案发布给其他人。他们可能会遇到同样的问题:)
    猜你喜欢
    • 1970-01-01
    • 2020-03-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多