gpt4 book ai didi

javascript - VueJS : Google Maps loads before data is ready - how to make it wait?(Nuxt)

转载 作者:数据小太阳 更新时间:2023-10-29 04:39:36 27 4
gpt4 key购买 nike

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

经过进一步调查(感谢下面的@QuỳnhNguyễn),似乎在数据准备好之前运行了 Google map 实例。我曾尝试在初始化 map 之前观察要加载的提要,但它似乎不起作用。

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

有问题的代码如下...

模板:

<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>

脚本

<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>

最佳答案

事实证明问题出在脏数据上。

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

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

清理数据:

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 });
}

}
}
}
}

然后,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>

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

关于javascript - VueJS : Google Maps loads before data is ready - how to make it wait?(Nuxt),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53946183/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com