• 赚钱入口【需求资源】限时招募流量主、渠道主,站长合作;【合作模式】CPS长期分成,一次推广永久有收益。主动打款,不扣量;

marker-animate-unobtrusive 初始化报错

Vue cps12345 8个月前 (08-16) 137次浏览 0个评论
import SlidingMarker from 'marker-animate-unobtrusive'

初始化:

SlidingMarker.initializeGlobally()

报错:

marker-animate-unobtrusive 初始化报错

解决方法

因为SlidingMarkerextends google.maps.Marker类需要首先加载GoogleMaps JavaScript API库,一个选择是通过index.html文件添加引用:

<script src="https://maps.googleapis.com/maps/api/js?key=--KEY-GOES-HERE--"></script>

另一种选择是利用异步 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库 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>
喜欢 (0)

您必须 登录 才能发表评论!