我正在尝试使用marker-animate-unobtrusive,但我一直收到此错误:
我在SO上发现了另一条帖子,该帖子谈到在Google加载后需要文件,但我不知道该怎么做。在我的组件中,我有这个:
import SlidingMarker from 'marker-animate-unobtrusive'
在我安装的方法中,我有这个:
SlidingMarker.initializeGlobally()
非常感谢您的帮助
答案 0 :(得分:1)
由于SlidingMarker
扩展了google.maps.Marker
class,这是预期的错误,需要先加载GoogleMaps JavaScript API库,一种选择是通过index.html
文件添加引用:
<script src="https://maps.googleapis.com/maps/api/js?key=--KEY-GOES-HERE--"></script>
另一种选择是利用 async JavaScript加载器,例如scriptjs
。加载GoogleMaps JavaScript API和marker-animate-unobtrusive
模块的示例如下所示:
created: function(){
get("https://maps.googleapis.com/maps/api/js?key=", () => {
const SlidingMarker = require('marker-animate-unobtrusive')
SlidingMarker.initializeGlobally()
const map = new google.maps.Map(document.getElementById('map'), this.mapOptions);
const marker = new SlidingMarker({
position: this.mapOptions.center,
map: map,
title: 'Im sliding marker'
});
});
}
更新
使用vue-google-maps
library marker-animate-unobtrusive
插件可以像这样集成:
<template>
<div>
<GmapMap :center="center" :zoom="zoom" ref="mapRef"></GmapMap>
</div>
</template>
<script>
/* global google */
export default {
data() {
return {
zoom: 12,
center: { lat: 51.5287718, lng: -0.2416804 },
};
},
mounted: function() {
this.$refs.mapRef.$mapPromise.then(() => {
this.initSlidingMarker(this.$refs.mapRef.$mapObject)
})
},
methods: {
initSlidingMarker(map){
const SlidingMarker = require('marker-animate-unobtrusive')
SlidingMarker.initializeGlobally()
const marker = new SlidingMarker({
position: map.getCenter(),
map: map,
title: 'Im sliding marker'
});
google.maps.event.addListener(map, 'click', (event) => {
marker.setDuration(1000);
marker.setEasing('linear');
marker.setPosition(event.latLng);
});
}
}
}
</script>
<style>
.vue-map-container {
height: 640px;
}
</style>
答案 1 :(得分:0)
Install the package google from npm this should fix it.