最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

vue項(xiàng)目中使用百度地圖的方法

 更新時(shí)間:2018年06月08日 08:31:57   作者:子規(guī)魚  
這篇文章主要介紹了在vue項(xiàng)目中使用百度地圖的方法,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒借鑒價(jià)值,需要的朋友可以參考下

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

<script type="text/javascript" src=" 中的 密鑰替換成你申請(qǐng)的,在 vue項(xiàng)目的index.html引用。

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

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

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(); //清空原來(lái)的標(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>

總結(jié)

以上所述是小編給大家介紹的vue項(xiàng)目中使用百度地圖的方法,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!

相關(guān)文章

  • vue如何在main.js中配置全局的通用公共組件

    vue如何在main.js中配置全局的通用公共組件

    這篇文章主要介紹了vue如何在main.js中配置全局的通用公共組件問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-01-01
  • Vue3項(xiàng)目中使用防抖節(jié)流的實(shí)現(xiàn)示例

    Vue3項(xiàng)目中使用防抖節(jié)流的實(shí)現(xiàn)示例

    防抖節(jié)流是可以說(shuō)是一種優(yōu)化組件性能的技巧,可以有效減少組件中的渲染次數(shù)和計(jì)算量,本文主要介紹了Vue3項(xiàng)目中使用防抖節(jié)流的實(shí)現(xiàn)示例,感興趣的可以了解一下
    2024-04-04
  • vue 組件簡(jiǎn)介

    vue 組件簡(jiǎn)介

    這篇文章主要介紹了vue組件的相關(guān)資料,文中講解非常細(xì)致,代碼幫助大家更好的理解和學(xué)習(xí),感興趣的朋友可以了解下
    2020-07-07
  • vue中 數(shù)字相加為字串轉(zhuǎn)化為數(shù)值的例子

    vue中 數(shù)字相加為字串轉(zhuǎn)化為數(shù)值的例子

    今天小編就為大家分享一篇vue中 數(shù)字相加為字串轉(zhuǎn)化為數(shù)值的例子,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2019-11-11
  • 在antd Form表單中select設(shè)置初始值操作

    在antd Form表單中select設(shè)置初始值操作

    這篇文章主要介紹了在antd Form表單中select設(shè)置初始值操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2020-11-11
  • vue3中頁(yè)面跳轉(zhuǎn)兩種實(shí)現(xiàn)方式

    vue3中頁(yè)面跳轉(zhuǎn)兩種實(shí)現(xiàn)方式

    在Vue3中Vue?Router是一個(gè)常用的路由管理庫(kù),它提供了一種簡(jiǎn)單而強(qiáng)大的方式來(lái)實(shí)現(xiàn)路由跳轉(zhuǎn)和導(dǎo)航,這篇文章主要給大家介紹了關(guān)于vue3中頁(yè)面跳轉(zhuǎn)的兩種實(shí)現(xiàn)方式,需要的朋友可以參考下
    2024-09-09
  • Vue3?源碼解讀之副作用函數(shù)與依賴收集

    Vue3?源碼解讀之副作用函數(shù)與依賴收集

    本文深入分析了副作用的實(shí)現(xiàn)以及執(zhí)行時(shí)機(jī),并詳細(xì)分析了用于存儲(chǔ)副作用函數(shù)的targetMap的數(shù)據(jù)結(jié)構(gòu)及其實(shí)現(xiàn)原理,還深入分析了依賴收集track函數(shù)以及派發(fā)更新 trigger 函數(shù)的實(shí)現(xiàn),需要的朋友可以參考下
    2022-08-08
  • Vant 中的Toast設(shè)置全局的延遲時(shí)間操作

    Vant 中的Toast設(shè)置全局的延遲時(shí)間操作

    這篇文章主要介紹了Vant 中的Toast設(shè)置全局的延遲時(shí)間操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2020-11-11
  • Vue-component全局注冊(cè)實(shí)例

    Vue-component全局注冊(cè)實(shí)例

    今天小編就為大家分享一篇Vue-component全局注冊(cè)實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2018-09-09
  • 在vue.js中抽出公共代碼的方法示例

    在vue.js中抽出公共代碼的方法示例

    這篇文章主要給大家介紹了在vue.js中抽出公共代碼的方法,文中給出了詳細(xì)的示例代碼供大家參考學(xué)習(xí),對(duì)大家具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起看看吧。
    2017-06-06

最新評(píng)論

乃东县| 德令哈市| 手游| 敦化市| 长海县| 华坪县| 正阳县| 台北县| 绍兴县| 夏邑县| 田东县| 射阳县| 类乌齐县| 西丰县| 四子王旗| 安义县| 七台河市| 福建省| 大竹县| 十堰市| 肥西县| 甘谷县| 涪陵区| 丰都县| 黑水县| 孝昌县| 中超| 德昌县| 博兴县| 台州市| 五原县| 禄丰县| 缙云县| 化州市| 阿瓦提县| 原平市| 金阳县| 东平县| 本溪| 靖安县| 根河市|