未定义OverlappingMarkerSpiderfier(Vue.js)

时间:2019-05-22 23:32:18

标签: javascript google-maps vue.js markerspiderfier

我正在尝试为我的Google Maps实现OverlappingMarkerSpiderfier,它之所以有效,是因为当我单击一个标记时,我的标记能够“蜘蛛变身”。

我的问题是,在我的VS Code开发控制台中,ESLint仍然给我未定义错误“ OverlappingMarkerSpiderfier”的提示。我真的不知道问题出在哪里,因为当我单击它们时,标记正在按预期工作。下面的图片显示了即使ESLint出错,OverlappingMarkerWorkers:

enter image description here

我希望摆脱该错误,以防将来由于该错误而出现该错误。我一直在寻找解决方案,很多人评论说应在Google Maps加载后加载OverlappingMarkerSpiderfier。我已经做到了,但是错误仍然存​​在。

我异步加载Google Maps;以下是我的.js文件,可加载Google Maps和OverlappingMarkerSpiderfier:

import api_keys from './api_keys'

const API_KEY = api_keys.google_maps_api_key;
const CALLBACK_NAME = 'gmapsCallback';

let initialized = !!window.google;
let resolveInitPromise;
let rejectInitPromise;

const initPromise = new Promise((resolve, reject) => {
  resolveInitPromise = resolve;
  rejectInitPromise = reject;
});

export default function init() {
  if (initialized) return initPromise;

  initialized = true;

   window[CALLBACK_NAME] = () => resolveInitPromise(window.google);

   const script = document.createElement('script');
   script.async = true;
   script.defer = true;
   script.src = `https://maps.googleapis.com/maps/api/jskey=${API_KEY}&callback=${CALLBACK_NAME}`;
   script.onerror = rejectInitPromise;
   document.querySelector('head').appendChild(script);

   const spiderfier = document.createElement('script');
   spiderfier.defer = true;
   spiderfier.src = "https://cdnjs.cloudflare.com/ajax/libs/OverlappingMarkerSpiderfier/1.0.3/oms.min.js";
   spiderfier.onerror = rejectInitPromise;
   document.querySelector('head').appendChild(spiderfier);

   return initPromise;
}

以下是我的GoogleMaps组件。 OverlappingMarkerSpiderfier实现位于“监视”中:

<template>
    <div id="google-map">

    </div>
</template>

<script>
import gMaps from '../lib/gMaps.js'

export default {
    name: 'GoogleMaps',
    props: {
        events: Array
    },
    data() {
        return {
            map: null,
            locations: []
        }
    },
    async mounted() {
        try {
            const google = await gMaps();
            const geocoder = new google.maps.Geocoder();
            this.map = new google.maps.Map(this.$el);

            geocoder.geocode({ address: 'USA'}, (results, status) => {
                if (status !== 'OK' || !results[0]) {
                    throw new Error(status);
                }

                this.map.setCenter(results[0].geometry.location);
                this.map.fitBounds(results[0].geometry.viewport);
            });
        } catch (error) {
            console.error(error);
        }
    },
    watch: {
        async events() { //creates markers for the map; data is from a 3rd party API that is handled by a different component
            try {
                const google = await gMaps();

                var oms = new OverlappingMarkerSpiderfier(this.map, {
                    markersWontMove: true,
                    markersWontHide: true,
                    basicFormatEvents: true
                })

                for(let i = 0; i < this.events.length; i++) {
                    let marker = new google.maps.Marker({
                        position: {
                            lat: parseInt(this.events[i].latitude, 10),
                            lng: parseInt(this.events[i].longitude, 10)
                        },
                        map: this.map,
                        title: this.events[i].title
                    })

                    let iw = new google.maps.InfoWindow({
                        content: this.events[i].description || 'No description available.'
                    });

                    google.maps.event.addListener(marker, 'spider_click', function() {
                        iw.open(this.map, marker);
                    });

                    oms.addMarker(marker);
                }
            }
            catch(error) {
                console.error(error)
            }
        }
    }
}
</script>

<style lang="scss" scoped>
#google-map {
    width: auto;
    height: 100vh;
}
</style>

1 个答案:

答案 0 :(得分:0)

尝试其中之一

  1. this。$ nexttick(()=> {     代码已安装在挂钩中。... })
  2. 在实例化OverlappingMarkerSpiderfier之前,请检查是否已加载window.google对象,并且您的地图参考是否可用。