溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

怎么在vue項(xiàng)目中調(diào)用百度地圖api

發(fā)布時(shí)間:2021-03-11 16:25:30 來源:億速云 閱讀:313 作者:Leah 欄目:web開發(fā)

怎么在vue項(xiàng)目中調(diào)用百度地圖api?很多新手對(duì)此不是很清楚,為了幫助大家解決這個(gè)難題,下面小編將為大家詳細(xì)講解,有這方面需求的人可以來學(xué)習(xí)下,希望你能有所收獲。

1.在百度地圖申請(qǐng)密鑰: http://lbsyun.baidu.com/  將

<script type="text/javascript" src="http://api.map.baidu.com/api?v=2.0&ak=密鑰" ></script> 中的 密鑰替換成你申請(qǐng)的,在 vue項(xiàng)目的index.html引用。

2. 在build 文件下下的 webpack.base.conf.js貼入代碼

externals: {
  "BMap": "BMap"
 },

怎么在vue項(xiàng)目中調(diào)用百度地圖api

3. map.vue代碼(demo可以直接使用,demo使用了vue-clipboard2插件,請(qǐng)自行安裝)

<template>
  <div>
    <el-row >
     <el-col :offset="2" :span="8">
       <el-input :id="suggestId" v-model="address_detail" :clearable='clearable' placeholder="請(qǐng)輸入店鋪地址,獲取店鋪?zhàn)鴺?biāo)" >
      </el-input>
    </el-col>
    <el-col :span="2">
      <el-button id="position" @click="search" type="primary">定位</el-button>
    </el-col>
    <el-col :span="12" >
     <el-tag type="success" v-clipboard:copy="userlocation.lng" v-clipboard:success="onCopy" v-clipboard:error="onError" >經(jīng)度 {{userlocation.lng}}</el-tag>
     <el-tag type="success" v-clipboard:copy="userlocation.lat" v-clipboard:success="onCopy" v-clipboard:error="onError">緯度 {{userlocation.lat}}</el-tag>
     <el-tag type="success" ><<<<點(diǎn)擊左側(cè)按鈕復(fù)制經(jīng)緯度信息</el-tag>
    </el-col> 
  </el-row>
  <el-row>
    <el-col :offset="2" :span="18">
      <div id="map_canvas" class="allmap"></div>
    </el-col>
  </el-row>
  </div>
</template>
<script>
export default {
 data() {
  return {
   address_detail: "", //詳細(xì)地址
   userlocation: { lng: "", lat: "" },
   clearable: true,
   suggestId: "suggestId",
   map : {},
   mk: {}
  };
 },
 created () {
   
 },
 methods: {
   drawMap() {
    this.map = new BMap.Map("map_canvas");        // 創(chuàng)建地圖實(shí)例
    this.map.addControl(new BMap.NavigationControl());      // 啟用放大縮小 尺
    this.map.enableScrollWheelZoom();
    this.getlocation();//獲取當(dāng)前坐標(biāo), 測(cè)試時(shí)獲取定位不準(zhǔn)。
  
    var point = new BMap.Point(this.userlocation.lng, this.userlocation.lat); // 創(chuàng)建點(diǎn)坐標(biāo) 
    this.map.centerAndZoom(point, 13);         // 初始化地圖,設(shè)置中心點(diǎn)坐標(biāo)和地圖級(jí)別 
    var marker = new BMap.Marker(point);    // 創(chuàng)建標(biāo)注  
    this.map.addOverlay(marker);           // 將標(biāo)注添加到地圖中
     
    var ac = new BMap.Autocomplete({
    //建立一個(gè)自動(dòng)完成的對(duì)象
    input: "suggestId",
    location: this.map
    });
    var myValue;
   ac.addEventListener("onconfirm", (e)=> {
    //鼠標(biāo)點(diǎn)擊下拉列表后的事件
    var _value = e.item.value;
    myValue =_value.province +_value.city +_value.district +_value.street +_value.business;
    this.address_detail = myValue;
    this.setPlace();
   });
   },
   getMarker (point) {
        this.mk = new BMap.Marker(point); 
        this.mk.addEventListener("dragend", this.showInfo);
        this.mk.enableDragging();  //可拖拽
        this.getAddress(point);
        this.map.addOverlay(this.mk);//把點(diǎn)添加到地圖上 
        this.map.panTo(point);
   },
   getlocation () {
    //獲取當(dāng)前位置
    var geolocation = new BMap.Geolocation(); 
    geolocation.getCurrentPosition((r) =>{ 
      if(geolocation.getStatus() == BMAP_STATUS_SUCCESS){ 
        this.getMarker(r.point);
        this.userlocation = r.point;
      }else { 
        alert('failed'+this.getStatus()); 
      } 
    });
   },
   //綁定Marker的拖拽事件
     showInfo(e){
      var gc = new BMap.Geocoder();
      gc.getLocation(e.point, (rs)=>{
        var addComp = rs.addressComponents;
        var address = addComp.province + addComp.city + addComp.district + addComp.street + addComp.streetNumber;//獲取地址
         
        //畫圖 ---》顯示地址信息
        var label = new BMap.Label(address,{offset:new BMap.Size(20,-10)});
        this.map.removeOverlay(this.mk.getLabel());//刪除之前的label
 
        this.mk.setLabel(label);
        this.address_detail = address;
        this.userlocation = e.point;
         
       });
    },
     //獲取地址信息,設(shè)置地址label
     getAddress(point){
      var gc = new BMap.Geocoder();
       
      gc.getLocation(point, (rs)=>{
        var addComp = rs.addressComponents;
        var address = addComp.province + addComp.city + addComp.district + addComp.street + addComp.streetNumber;//獲取地址
         
        //畫圖 ---》顯示地址信息
        var label = new BMap.Label(address,{offset:new BMap.Size(20,-10)});
        this.map.removeOverlay(this.mk.getLabel());//刪除之前的label
        this.address_detail = address;
        this.mk.setLabel(label);
         
       });
       
    },
    setPlace() {
    this.map.clearOverlays(); //清除地圖上所有覆蓋物
    var th = this
    function myFun() {
     th.userlocation = local.getResults().getPoi(0).point; //獲取第一個(gè)智能搜索的結(jié)果
     th.map.centerAndZoom(th.userlocation, 18);
     th.getMarker(th.userlocation);
    }
 
    var local = new BMap.LocalSearch(this.map, {
     onSearchComplete: myFun //智能搜索
    });
    local.search(this.address_detail);
   },
   search () {
       var localSearch = new BMap.LocalSearch(this.map);
       localSearch.enableAutoViewport(); //允許自動(dòng)調(diào)節(jié)窗體大小
       this.searchByInputName(localSearch);
 },
   searchByInputName(localSearch) {
     this.map.clearOverlays(); //清空原來的標(biāo)注
     var th = this;
     var keyword = this.address_detail;
     localSearch.setSearchCompleteCallback(function(searchResult) {
       var poi = searchResult.getPoi(0);
       th.userlocation = poi.point;
       th.map.centerAndZoom(poi.point, 13);
       th.getMarker(th.userlocation);
    });
     localSearch.search(keyword);
   },
   onCopy () {
     this.$message('內(nèi)容已復(fù)制到剪貼板!');
   },
   onError () {
     this.$message('內(nèi)容復(fù)制失敗,請(qǐng)重試!');
 
   }
     
 },
 mounted() {
  this.$nextTick(function() {
   this.drawMap();
  });
   
 }
};
</script>
<style scoped>
.allmap {
 width: 100%;
 height: 400px;
 font-family: "微軟雅黑";
 border: 1px solid green;
}
.el-tag {
 cursor: pointer;
}
</style>

看完上述內(nèi)容是否對(duì)您有幫助呢?如果還想對(duì)相關(guān)知識(shí)有進(jìn)一步的了解或閱讀更多相關(guān)文章,請(qǐng)關(guān)注億速云行業(yè)資訊頻道,感謝您對(duì)億速云的支持。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

vue
AI