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

Vue集成百度地圖實(shí)現(xiàn)位置選擇功能

 更新時(shí)間:2022年06月23日 14:41:08   作者:zouhuu  
由于添加門店時(shí),需要選擇門店的省、市、區(qū)、詳細(xì)地址、以及門店的經(jīng)緯度信息,本文給大家分享Vue集成百度地圖實(shí)現(xiàn)位置選擇功能,感興趣的朋友一起看看吧

Vue集成百度地圖實(shí)現(xiàn)位置選擇

  • 需求:添加門店時(shí),需要選擇門店的省、市、區(qū)、詳細(xì)地址、以及門店的經(jīng)緯度信息。
  • 解決方案:集成百度地圖API,通過(guò)在地圖上搜索或者點(diǎn)擊獲取門店的具體位置信息。

百度地圖選擇地址效果

具體效果如下圖所示

百度地圖選擇地址

集成百度地圖的具體實(shí)現(xiàn)

技術(shù)方案: Vue + ElementUI + 百度地圖 JavaScript API v3.0

工程目錄結(jié)構(gòu)圖:

```
ProjectName              // 項(xiàng)目名稱    
├── public               // 公共文件         
│       └── index.html   // html模板
├── src                  // 源代碼
│       └── components   // 全局共用組件
│       		└── BMapAddressSelect  // 百度地圖位置選擇組件
│       └── views        // 頁(yè)面文件
  • 申請(qǐng)成為百度地圖開(kāi)發(fā)者并獲取秘鑰

參考文檔:https://lbsyun.baidu.com/index.php?title=jspopular3.0/guide/getkey

第一步:引入百度地圖 JavaScript API v3.0 文件

修改文件:public -> index.html

<script type="text/javascript" src="http://api.map.baidu.com/api?v=3.0&ak=你的秘鑰"></script>

第二步:編寫百度地圖選擇位置組件

新增文件:src -> components -> BMapAddressSelect -> index.vue

<template>
  <el-dialog title="位置選擇" :visible.sync="openMap" width="900px" append-to-body>
    <el-form label-width="80px">
      <el-row>
        <el-col :span="10">
          <el-form-item label="搜索地址">
            <el-input size="mini" type="text" id="searchAddres" v-model="searchAddresKeywords" placeholder="請(qǐng)輸入地點(diǎn)">
            </el-input>
          </el-form-item>
        </el-col>
        <el-col :span="14">
          <el-form-item label="當(dāng)前地址">
            <el-input placeholder="請(qǐng)輸入內(nèi)容" v-model="addressInfo.address">
              <template slot="prepend">{{addressInfo.province}}{{addressInfo.city}}{{addressInfo.district}}</template>
            </el-input>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <div id="baidu-map-container" style="width: 100%; height: 400px;"></div>

    <div slot="footer" class="dialog-footer">
      <el-button type="primary" @click="confirmSelect">確 定</el-button>
      <el-button @click="cancel">取 消</el-button>
    </div>
  </el-dialog>
</template>

<script>
export default {
  data() {
    return {
      searchAddresKeywords: "",
      addressInfo: {
        // 地址信息
        longitude: "", // 經(jīng)度
        latitude: "", // 緯度
        province: "", // 省
        city: "", // 市
        district: "", // 區(qū)
        address: "", // 詳細(xì)地址
      },
      openMap: false,
    };
  },
  methods: {
    // 初始化百度地圖
    initBaiduMap() {
      let that = this;
      this.$nextTick(function () {
        /** 初始化地圖Start */
        var map = new BMap.Map("baidu-map-container"); // 創(chuàng)建地圖實(shí)例
        var point = new BMap.Point(116.404, 39.915); // 設(shè)置中心點(diǎn)坐標(biāo)
        map.centerAndZoom(point, 13); // 地圖初始化,同時(shí)設(shè)置地圖展示級(jí)別
        map.enableScrollWheelZoom(true); //開(kāi)啟鼠標(biāo)滾輪縮放
        /** 初始化地圖End */

        /** 點(diǎn)擊地圖創(chuàng)建坐標(biāo)事件Start */
        // 添加地圖點(diǎn)擊事件
        map.addEventListener("click", function (e) {
          var clickpt = e.point; // 點(diǎn)擊的坐標(biāo)
          map.clearOverlays(); // 移除地圖上的標(biāo)注
          var marker = new BMap.Marker(clickpt); // 創(chuàng)建標(biāo)注
          map.addOverlay(marker); // 將標(biāo)注添加到地圖中
          // 逆地址解析
          that.geocAddress(clickpt);
        });
        /** 點(diǎn)擊地圖創(chuàng)建坐標(biāo)事件End */

        /** 搜索地址Start */
        // 建立一個(gè)自動(dòng)完成的對(duì)象
        var ac = new BMap.Autocomplete({
          input: "searchAddres",
          location: map,
        });
        // 鼠標(biāo)點(diǎn)擊下拉列表后的事件
        ac.addEventListener("onconfirm", function (e) {
          map.clearOverlays(); // 移除地圖上的標(biāo)注
          var local = new BMap.LocalSearch(map, {
            //智能搜索
            onSearchComplete: function (results) {
              let poi = results.getPoi(0); //獲取第一個(gè)智能搜索的結(jié)果
              var searchpt = poi.point; // 獲取坐標(biāo)
              map.centerAndZoom(searchpt, 16);
              map.addOverlay(new BMap.Marker(searchpt)); //添加標(biāo)注
              that.geocAddress(searchpt); // 逆地址解析
            },
          });
          // 搜索詞
          var searchValue = e.item.value;
          local.search(
            searchValue.province +
              searchValue.city +
              searchValue.district +
              searchValue.street +
              searchValue.business
          );
        });
        /** 搜索地址End */
      });
    },

    /** 逆向解析地址 point */
    geocAddress(point) {
      let that = this;
      var geoc = new BMap.Geocoder();
      geoc.getLocation(point, function (geocInfo) {
        // 設(shè)置基本信息
        var addressInfo = geocInfo.addressComponents;
        that.addressInfo.longitude = point.lng;
        that.addressInfo.latitude = point.lat;
        that.addressInfo.province = addressInfo.province;
        that.addressInfo.city = addressInfo.city;
        that.addressInfo.district = addressInfo.district;
        let address = addressInfo.street + addressInfo.streetNumber;
        if (geocInfo.surroundingPois.length > 0) {
          address = address + geocInfo.surroundingPois[0].title;
        }
        that.addressInfo.address = address;
      });
    },

    /** 打開(kāi)地圖選擇 */
    show() {
      this.openMap = true;
      this.initBaiduMap();
    },

    /**
     * 確認(rèn)選擇
     */
    confirmSelect() {
        this.$emit("confirmMapAddress", this.addressInfo);
        this.openMap = false;
    },

    /**
     * 取消選擇
     */
    cancel() {
        this.openMap = false;
    }
  },
};
</script>

<style lang="scss">
// 防止地圖自動(dòng)完成的對(duì)象被遮擋
.tangram-suggestion {
  z-index: 9999;
}
</style>

第三步:使用百度地圖選擇位置組件

<template>
  <div class="app-container">
    <el-row>
      <el-col :span="12">
        <el-form ref="addressInfo" :model="addressInfo" :rules="rules" label-width="100px">
          <el-form-item label="門店地址" prop="address">
            <el-input placeholder="請(qǐng)選擇門店地址" v-model="addressInfo.address" disabled>
              <template slot="prepend">{{addressInfo.province}}{{addressInfo.city}}{{addressInfo.district}}</template>
              <el-button slot="append" icon="el-icon-map" @click="showMap" type="primary">選擇地址</el-button>
            </el-input>
          </el-form-item>
          <el-row>
            <el-col :span="12">
              <el-form-item label="門店經(jīng)度" prop="longitude">
                <el-input v-model="addressInfo.longitude" placeholder="請(qǐng)輸入經(jīng)度" disabled />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="門店緯度" prop="latitude">
                <el-input v-model="addressInfo.latitude" placeholder="請(qǐng)輸入緯度" disabled />
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
      </el-col>
    </el-row>
	
	<!-- 百度地圖位置選擇 -->
    <BMapAddressSelect ref="bmapAddressSelect" @confirmMapAddress="confirmMapAddress"></BMapAddressSelect>

  </div>
</template>

<script>

import BMapAddressSelect from "@/components/BMapAddressSelect/index";

export default {
  data() {
    return {
      // 位置信息
      addressInfo: {},
      // 表單校驗(yàn)
      rules: {},
    };
  },
  components: {
    BMapAddressSelect,
  },
  methods: {
    /** 顯示地圖 */
    showMap() {
      this.$refs.bmapAddressSelect.show();
    },

    /** 確認(rèn)地圖地址 */
    confirmMapAddress(addressInfo) {
      this.addressInfo = addressInfo;
    },
  },
};
</script>

百度地圖Web開(kāi)發(fā)

參考文章

https://www.cnblogs.com/googlegis/p/14640897.html

https://lbsyun.baidu.com/index.php?title=jspopular3.0/guide/helloworld

https://zhuanlan.zhihu.com/p/506032108

到此這篇關(guān)于Vue集成百度地圖實(shí)現(xiàn)位置選擇的文章就介紹到這了,更多相關(guān)vue百度地圖位置內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論

原阳县| 富川| 彩票| 厦门市| 余庆县| 宁国市| 桂林市| 泽库县| 无极县| 台州市| 安义县| 上饶市| 苏尼特左旗| 垫江县| 吴江市| 西乌珠穆沁旗| 松原市| 漾濞| 黄浦区| 页游| 应城市| 敦化市| 金沙县| 德清县| 桐庐县| 太谷县| 荆州市| 丁青县| 杭州市| 河间市| 浑源县| 册亨县| 荥阳市| 清镇市| 岑溪市| 澄江县| 峨眉山市| 崇信县| 鹤庆县| 长寿区| 北海市|