- xml - AJAX/Jquery XML 解析
- 具有多重继承的 XML 模式
- .net - 枚举序列化 Json 与 XML
- XML 简单类型、简单内容、复杂类型、复杂内容
这是我的第一个 VueJS 项目,我已经启动并运行了 vue2-google-maps,但是当我尝试将 map 标记连接到我网站的 JSON 提要时遇到了一个问题(使用 Wordpress REST API),Lat 和 Lng 值返回undefined 或 NaN。
经过进一步调查(感谢下面的@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/
根据 jQueryDoc在术语中,.ready() 被称为位于 jQuery.prototype 中的查询选择方法。对于前 $(document).ready() 作为jQueryDoc说:$ 命名空
我见过一些代码,他们只是这样做: $().ready(function() { ... }); 这比执行文档选择器要短,但它是同一件事吗? 最佳答案 略有变化: $(document).rea
各个处理程序的回调函数何时执行? 最佳答案 在主要过程中 当您的应用完成初始化并准备打开浏览器窗口时,将触发app上的“就绪”事件。由于在此之前您无法打开窗口,因此可以使用回调函数来创建Browser
最近发现了head.js图书馆和男孩,我对此感到满意,尽管我仍然对一件事感到有点困惑。 来自 headjs.com: The “DOM ready” event such as $(document)
我在我的代码库中发现代码在另一个 $(document).ready(function() { ... 例如 $(document).ready(function() { // 20
我已经养成了从ready函数开始jquery编码的习惯 $(function(){...}); 并将所有从ready调用的函数放入ready中。 然后我意识到,放入就绪函数中的一些函数可能不需要在那里
在浏览旧代码库时,我发现了以前的软件开发人员正在使用的地方 $(function(a) {}(A || (A = {}))); 即使页面尚未准备好,它仍在执行。在我删除传递的全局变量后它开始工作。代码
我已经看到了两种方式,但哪种更好或者并不重要。 我觉得包装每个语句可能会更干净,但只是想知道如果您有 50 个语句,每个语句都有自己的 document.ready 事件处理程序,是否会有更多回调?
这个问题已经有答案了: Four variations of jQuery ready() -- what's the difference? (4 个回答) 已关闭 4 年前。 我正在编写一些我没有
我们最近遇到了一个错误,jquery document.ready 似乎在 DOM 加载之前触发。事实证明,在调用的第一个项目之后有一些错误的代码 $.ready(function(){}); 这条语
什么应该在 jQuery.ready() 中,什么应该在 jQuery.ready() 之外? 从性能角度来看,我在某处读到,将所有代码包装在 jQuery.ready() 中并不是一种有效的方法。
我实现了一个带有选项列表(工作类别)的页面,单击该页面时应显示数据(工作描述)。我正在使用 BBQ 来处理后台堆栈。 一切正常,除了在用户首次导航到页面时设置初始选择。我的代码被调用到 addClas
解决方案 我有两个 users.js和 users.coffee在我的 Assets 管道中。显然 users.coffee正在阻止 users.js从被加载。确保删除它! 我正在尝试实现 this
我正在研究一个指令,但我不想玩 $document或 $window , 只有 element本身。 之前我有: angular.element($document).ready(function()
我想在移动设备方向改变时使用 $(window).resize 来调用一些函数,我在 $(document).ready 中编写了所有代码,这在我使用 Android 设备时有效,但在使用 iPhon
我有一个使用数据库的 Ionic 应用程序。我想用一个文件的内容填充这个数据库。 这部分我开始工作了。我想创建一个 DB.ready() 事件,很像 $ionicPlatform.ready() 或
我有一个名为“loadTimeTrackersGrid()”的函数,它加载一个弹性网格。设置如下所示: $(document).ready(function () { var edi
我有一个定义数量为 replicas 的部署.如果我的 Pod 准备好/未准备好处理新连接,我使用就绪探针进行通信 - 我的 Pod 在 ready 之间切换/not ready他们一生中的状态。 我
有什么区别: $(document).ready(initialize); 和 $(document).on('ready', initialize); 对我来说,它们似乎以相同的方式工作。 最佳答案
我看到很多项目都在使用 $(document).on('ready', function(e){ //jquery stuff }) 而不是: $( document ).ready(functio
我是一名优秀的程序员,十分优秀!