VueJS:谷歌地图在数据准备好之前加载 - 如何让它等待?(Nuxt)

Alx*_*Red 8 javascript google-maps vue.js nuxt.js vue2-google-maps

这是我的第一个VueJS项目,我已经启动并运行了vue2-google-maps但是当我尝试将地图标记连接到我的网站的JSON提要(使用Wordpress REST API)时,我遇到了一个问题,LatLng值返回undefinedNaN.

在进一步调查时(感谢下面的@QuỳnhNguyễn),似乎在数据准备好之前正在运行Google Maps实例.在初始化地图之前,我已经尝试过观看要加载的Feed,但它似乎不起作用.

标记位置使用JSON从WordPress REST API中提取并存在于数组(位置)中.该数组存在并填充在Vue Dev Tools(51条记录)中,但在检查已安装时,该数组为空.数据在创建阶段被拉入,所以我不知道为什么安装阶段不会准备好.

有问题的代码如下......

模板:

<template>
    <gmap-map v-if="feedLoaded" ref="map" :center="center" :zoom="zoom" :map-type-id="mapTypeId" :options="options">
        <gmap-marker 
            :key="index" v-for="(m, index) in locations" 
            :position="{ lat: parseFloat(m.place_latitude), lng: parseFloat(m.place_longitude) }" 
            @click="toggleInfoWindow(m,index)" 
            :icon="mapIconDestination">
        </gmap-marker>
        <gmap-info-window></gmap-info-window>
    </gmap-map>
</template>
Run Code Online (Sandbox Code Playgroud)

脚本

<script>
    const axios = require('axios');
    const feedURL = "API_REF";

    export default {
        props: {
            centerRef: {
                type: Object,
                default: function() {
                    return { lat: -20.646378400026226, lng: 116.80669825605469 }
                }
            },
            zoomVal: {
               type: Number,
               default: function() {
                   return 11
               }
            }
        },
        data: function() {
            return {
                feedLoaded: false,
                zoom: this.zoomVal,
                center: this.centerRef,
                options: {
                    mapTypeControl: false,
                    streetViewControl: false,
                },
                mapTypeId: 'styledMapType',
                mapIconDestination: '/images/map-pin_destination.png',
                mapIconActivity: '/images/map-pin_activity.png',
                mapIconAccommodation: '/images/map-pin_accommodation.png',
                mapIconEvent: '/images/map-pin_event.png',
                mapIconBusiness: '/images/map-pin_business.png',
                locations: [],
                markers: []
            }
        },
        created: function() {
            this.getData();
        },
        mounted: function() {
            this.$nextTick(() => {
                this.$refs.karrathaMap.$mapPromise.then((map) => {
                    var styledMapType = new google.maps.StyledMapType(
                        [...MAP_STYLE SETTINGS...]
                    )
                    map.mapTypes.set('styled_map', styledMapType);
                    map.setMapTypeId('styled_map');

                })

            });
        },
        watch: {
            feedLoaded: function() {
                if (this.feedLoaded == true) {
                    console.log(JSON.stringify(this.locations))
                }
            }
        },
        methods: {
            getData() {
                const url = feedURL;
                axios
                    .get(url)
                    .then((response) => {this.locations = response.data;})
                    .then(this.feedLoaded = true)
                    .catch( error => { console.log(error); }
                );
            }
        }
    }
</script>
Run Code Online (Sandbox Code Playgroud)

Alx*_*Red 4

事实证明问题是脏数据。

JSON 响应包含不应该包含在地图上的位置,因此每次遇到不包含 ACF 字段的条目时都会失败,尽管我将提要设置为仅返回包含在地图上的数据真的。

我已经解决了这个问题,方法是在加载提要后处理数据,并使用有效数据从中创建一个新数组(标记),然后使用它而不是原始(位置)数组将标记放置在地图上。

清理数据:

watch: {
    feedLoaded: function() {
        if (this.feedLoaded == true) {

            var LocationList = this.locations;

            for (var i = 0; i < LocationList.length; i++) {
                var includeOnMap = LocationList[i].acf['place_include-on-map'];

                if (includeOnMap === true) {
                    var placeName = LocationList[i].title.rendered;
                    var placeDescription = LocationList[i].acf['place_short-description'];
                    var placeLatitude = LocationList[i].acf['place_latitude'];
                    var placeLongitude = LocationList[i].acf['place_longitude'];
                    var placeIcon = this.mapIconDestination;

                    this.markers.push({ name: placeName, lat: placeLatitude, lng: placeLongitude, icon: placeIcon });
                }

            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

然后,gmap 组件

<gmap-map ref="karrathaMap" :center="center" :zoom="zoom" :map-type-id="mapTypeId" :options="options">
    <gmap-marker v-if="feedLoaded == true" :key="index" v-for="(m, index) in markers" :position="{ lat: parseFloat(m.lat), lng: parseFloat(m.lng) }" @click="toggleInfoWindow(m,index)" :icon="m.icon"></gmap-marker>
    <gmap-info-window></gmap-info-window>
</gmap-map>
Run Code Online (Sandbox Code Playgroud)

感谢所有帮助我弄清问题根源的人。我现在将花一些时间重新思考数据的结构。