代码之家  ›  专栏  ›  技术社区  ›  rdhainaut

如何在vuejs应用程序中使用从外部cdn加载的bing地图?

  •  1
  • rdhainaut  · 技术社区  · 7 年前

    如何在vue js应用程序中使用bing maps api来显示地图?

    注意

    这是我的模板

    <template>
       <div id="map"></div>
    </template>
    

    这是我的风格

    <style lang="scss" scoped>
       #map {
          height: 300px;
          width: 500px;
       }
    </style>
    

    mounted() {
       let mapElement: HTMLElement = <HTMLElement>document.getElementById("map")
       var map = new Microsoft.Maps.Map(mapElement, {
         credentials: [API_KEY]
       });
    }
    

    我是如何从我的应用程序内包括这个脚本。 经过一番研究,我发现并尝试了以下两种选择

    <!-- index.html -->
    ...
    <head>
       ...
       <script src="https://www.bing.com/api/maps/mapcontrol?key=[API_KEY]" async defer></script>
    </head>
    

    选项2:我在mounted方法中从我的组件在文档中注入programmatical脚本,如下所示

    mounted() { 
       // Add programmaticaly the external Bing maps api script
       var scriptTag = document.createElement("script");
       scriptTag.src = "https://www.bing.com/api/maps/mapcontrol";
       scriptTag.id = "bingApiMaps";
       // Inject the dynamic script in the DOM
       document.head.appendChild(scriptTag);
       ...
    }
    

    在这两种情况下,我都有以下错误,我不明白为什么:

    [Vue warn]: Error in mounted hook: "ReferenceError: Microsoft is not defined"
    
    1 回复  |  直到 7 年前
        1
  •  4
  •   rdhainaut    7 年前

    在经历了很多麻烦之后,我明白了。 Bing映射api是异步加载的。所以微软/Microsoft.Maps对象不直接可用。

    我决定保留解决方案2来加载脚本(这样脚本就不会全局加载)。 这是我最后的解决方案

    <template>
      <div id="map" ref="map"></div>
    </template>
    
    <script lang="ts">
    // Vue
    import { Vue, Component } from "vue-property-decorator";
    
    @Component
    export default class AppMap extends Vue {
      mounted() {
        if (document.getElementById("scriptBingMaps")) {
          return; // already loaded
        }
    
        // Add a global function for the callback from Bing Maps api
        (<any>window).OnLoadBingMapsApi = () => this.InitMap();
    
        // Add programmaticaly the external Bing maps api script
        var scriptTag = document.createElement("script");
        scriptTag.src = "https://www.bing.com/api/maps/mapcontrol?callback=OnLoadBingMapsApi";
        scriptTag.id = "scriptBingMaps";
        // Inject the dynamic script in the DOM
        document.head.appendChild(scriptTag);
      }
    
      private InitMap(): void {
        let mapElement: HTMLElement = <HTMLElement>this.$refs.map;
        var map = new Microsoft.Maps.Map(mapElement, {
          credentials: [API_KEY]
        });
      }
    }
    </script>
    
    <style lang="scss" scoped>
    /* ==========================================================================
       Map
      ========================================================================== */
    #map {
      height: 300px;
      width: 500px;
    }
    </style>
    

    等等!:)

        2
  •  4
  •   barbsan Cibi    7 年前

    mounted: function() {
      if (document.getElementById("scriptBingMaps")) {
        return; // already loaded
      }
    
      // Add a global function for the callback from Bing Maps api
      window.OnLoadBingMapsApi = () => this.InitMap();
    
      // Add programmaticaly the external Bing maps api script
      var scriptTag = document.createElement("script");
      scriptTag.src = "https://www.bing.com/api/maps/mapcontrol?callback=OnLoadBingMapsApi&key=[BING_API_KEY]";
      scriptTag.id = "scriptBingMaps";
    
      // Inject the dynamic script in the DOM
      document.head.appendChild(scriptTag);
    },
    methods: {
      InitMap: function() {
        var mapElement = this.$refs.myMap;
    
        this.map = new Microsoft.Maps.Map(mapElement, {
          mapTypeId: Microsoft.Maps.MapTypeId.aerial,
          zoom: 15,
          maxZoom: 21,
          //minZoom: 15,
          center: new Microsoft.Maps.Location(52.7759872, -1.5119702),
          maxNetworkLinkDepth: 3
        });
      }
    }