【问题标题】:How to make Google Map reactive?如何使谷歌地图反应?
【发布时间】:2017-11-10 19:09:41
【问题描述】:

我目前正在努力弄清楚如何使 Google Map 具有响应性,因为在安装组件时,还没有选择 lat 和 lng。看看我的代码:

<template>
    <div>
        <div class="google-map" id="sidebar-map"></div>
    </div>
</template>

<script>
    export default {
        props: ["coordinates", "zoom"],

        data() {
            return {
                latLng: {
                    lat: 0,
                    lng: 0
                }
            }
        },

        mounted() {
            this.latLng = this.coordinates;

            let map = new google.maps.Map(document.getElementById('sidebar-map'), {
                zoom: this.zoom,
                center: this.latLng
            });
            let marker = new google.maps.Marker({
                position: this.latLng,
                map: map
            });
        }
    }
</script>

有什么办法可以new google.maps.Mapnew google.maps.Marker当数据发生变化时是这个组件吗?

任何想法或方向/提示将不胜感激。

【问题讨论】:

    标签: java google-maps vue.js


    【解决方案1】:

    您可以观察coordinateszoom 道具的变化。

    https://vuejs.org/v2/guide/computed.html#Watchers

    我不熟悉 google maps API,但如果它在不断反弹到同一个元素时代价高昂(或中断),您可以考虑将实例存储在组件的字段中并使用任何可用的 API 方法以编程方式更改缩放和坐标。

    【讨论】:

    • 哦,哇,......不知道我怎么没想到那个解决方案。非常感谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-11
    • 2020-02-20
    • 1970-01-01
    • 2018-11-02
    • 2018-08-03
    相关资源
    最近更新 更多