How can I use bing maps loaded from external cdn inside my vuejs application?

Viewed 1960

How to use the bing maps api inside a vue js application to display a map ?

Note: I use Bing maps V8 and vuejs 2.5.17.

This is my template

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

This is my style

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

This is my script part (I use class based object component)

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

This is how I include the external script from the cdn inside my app. After some research, I have found and tried 2 options below

Option 1: I have included the script directly in my index.html file:

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

Option 2: I inject programmaticaly the script in the document from my component in the mounted method as below

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);
   ...
}

In both, I have the follow error and I dont understand why:

[Vue warn]: Error in mounted hook: "ReferenceError: Microsoft is not defined"
2 Answers

I've transcripted rdhainaut's answer to JavaScript:

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
    });
  }
}

After a lot of troubles, I have understood. The Bing map api is loaded async way. So the Microsoft / Microsoft.Maps object are not directly available.

I have decided to keep the solution 2 to load the script (this way the script is not globally loaded). I have tried to use the onload method on the injected script but with no success. The Bing Maps api has an option to call a callback function but the function must be global. This is my final working solution

<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>

Et voilĂ  ! :)

Related