Home >Web Front-end >JS Tutorial >Share 2 ways to use Baidu Maps in Vue

Share 2 ways to use Baidu Maps in Vue

yulia
yuliaOriginal
2018-09-20 15:20:065946browse

Maps are used a lot in the project, and I have used them several times recently. I can’t remember them even after not using them for a long time. In my spare time, I compiled two methods of using Baidu Maps in Vue to facilitate my own viewing and share them with everyone. , hoping to help those in need.

The common method is:

1.Introduce

<script type="text/javascript" src="http://api.map.baidu.com/api?v=2.0&ak=您的密钥"></script>

2. Create a new component maps

Note : Do not name the component map, because if there is a map tag in HTML, an error will be reported

Error: Do not use built-in or reserved HTML elements as component id:map

3. Then You can directly re-assemble the components and write the relevant code

mounted(){
      var map = new BMap.Map(&#39;map&#39;)
      var point = new BMap.Point(108.840053, 34.129522)
      map.centerAndZoom(point, 14)
      //...
    }

Another method:

1. Create a new map.js

export function MP(ak) {
  return new Promise(function (resolve, reject) {
    window.onload = function () {
      resolve(BMap)
    }
    var script = document.createElement("script");
    script.type = "text/javascript";
    script.src = "http://api.map.baidu.com/api?v=2.0&ak="+ak+"&callback=init";
    script.onerror = reject;
    document.head.appendChild(script);
  })
}

2. When needed Introduce

import {MP} from &#39;./map.js&#39;

3 into the vue page of the map used. Call

data:{
    return{
        ak:&#39;1287348913029483740293&#39;//密钥
    }
},
mounted(){
    this.$nextTick(function(){
      var _this = this;
      MP(_this.ak).then(BMap => {
       //在此调用api
      })
    }
}
in the vue page

The above is the detailed content of Share 2 ways to use Baidu Maps in Vue. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn