Home > Web Front-end > JS Tutorial > body text

Share 2 ways to use Baidu Maps in Vue

yulia
Release: 2018-09-20 15:20:06
Original
5909 people have browsed it

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>
Copy after login

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)
      //...
    }
Copy after login

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);
  })
}
Copy after login

2. When needed Introduce

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

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
      })
    }
}
Copy after login
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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template