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

vue中調(diào)用百度地圖獲取經(jīng)緯度的實(shí)現(xiàn)

 更新時(shí)間:2021年08月16日 08:32:39   作者:鵬仔工作室  
最近做個(gè)項(xiàng)目,需要實(shí)現(xiàn)獲取當(dāng)前位置的經(jīng)緯度,所以本文主要介紹了vue中調(diào)用百度地圖獲取經(jīng)緯度的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

項(xiàng)目中,需要實(shí)現(xiàn)獲取當(dāng)前位置的經(jīng)緯度,或者搜索某個(gè)位置并獲取經(jīng)緯度信息,我使用的的是vue,地圖使用的是百度地圖。

vue調(diào)用百度地圖,獲取當(dāng)前經(jīng)緯度,js獲取當(dāng)前經(jīng)緯度

默認(rèn)自動(dòng)獲取當(dāng)前位置經(jīng)緯度

js獲取經(jīng)緯度

拖動(dòng)小紅標(biāo) 獲取經(jīng)緯度

js獲取經(jīng)緯度 js獲取當(dāng)前經(jīng)緯度

關(guān)鍵詞 查詢獲取經(jīng)緯度

前期準(zhǔn)備

首先,我們需要取百度官方申請(qǐng)一個(gè)地圖api秘鑰,https://lbsyun.baidu.com/apiconsole/key 進(jìn)入后在應(yīng)用管理,我的應(yīng)用去申請(qǐng)即可。

申請(qǐng)好以后,我們打開vue項(xiàng)目中public文件下的index.html文件,拼接百度AK值并引入

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

如上所示,紅色區(qū)域?yàn)锳K值,自行拼接自己的,可以設(shè)置權(quán)限為公開或者針對(duì)網(wǎng)址白名單。

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

我使用了elementui的彈窗,輸入框,提示,如果你沒使用elementui,記得更換哦!

HTML代碼

<template>
  <div>
    <el-dialog
      @close="clearDialog"
      :close-on-click-modal="false"
      :title="text"
      style="text-align: left"
      :visible.sync="popup"
      width="30%"
    >
      <div class="form-layer">
        <el-form label-width="100px" size="mini">
          <el-form-item label="獲取定位">
            <el-button type="primary" @click="fixedPos">重新定位</el-button>
          </el-form-item>
          <el-form-item label="當(dāng)前緯度">
            <p>{{latitude}}</p>
          </el-form-item>
          <el-form-item label="當(dāng)前經(jīng)度">
            <p>{{longitude}}</p>
          </el-form-item>
          <el-form-item>
            <div class="f-a-c">
              <el-input v-model="keyWords" placeholder="請(qǐng)輸入地區(qū)" style="width: 230px;margin-right: 6px;"></el-input>
              <el-button type="primary" @click="setPlace" :disabled="!keyWords">查詢</el-button>
            </div>
          </el-form-item>
        </el-form>
        <div id="map"></div>
      </div>
      <div slot="footer" class="dialog-footer">
        <el-button
          size="small"
          type="primary"
          v-if="type != '2'"
          @click="btnSubmit()"
          >確 認(rèn)</el-button
        >
        <el-button size="small" @click="popup = false">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

JS代碼

<script>
  export default {
    name: "mapView",
    data() {
      return {
        map: null,
        local: null,
        mk: null,
        latitude: '',
        longitude: '',
        keyWords: ''
      };
    },
    methods: {
      // 打開彈窗,name為彈窗名稱
      async openDialog(name) {
        this.text = name;
        this.popup = true;
        this.initMap();
      },
      // 確認(rèn)
      btnSubmit() {
        let key = {
          latitude: this.latitude,
          longitude: this.longitude
        }
        // 打印經(jīng)緯度
        console.log(key);
        this.popup = false;
      },
      initMap() {
        this.$nextTick(() => {
          this.map = new BMap.Map("map");
          let point = new BMap.Point(116.404, 39.915);
          this.map.centerAndZoom(point, 12);
          this.map.enableScrollWheelZoom(true); // 開啟鼠標(biāo)滾輪縮放
          this.map.addControl(new BMap.NavigationControl());
          this.fixedPos();
        });
      },
      // 點(diǎn)擊定位-定位到當(dāng)前位置
      fixedPos() {
        const _this = this;
        const geolocation = new BMap.Geolocation();
        this.confirmLoading = true;
        geolocation.getCurrentPosition(function (r) {
          if (this.getStatus() == BMAP_STATUS_SUCCESS) {
            _this.handleMarker(_this, r.point);
            let myGeo = new BMap.Geocoder();
            myGeo.getLocation(
              new BMap.Point(r.point.lng, r.point.lat),
              function (result) {
                _this.confirmLoading = false;
                if (result) {
                  _this.latitude = result.point.lat;
                  _this.longitude = result.point.lng;
                }
              }
            );
          } else {
            _this.$message.error("failed" + this.getStatus());
          }
        });
      },
      // 搜索地址
      setPlace() {
        this.local = new BMap.LocalSearch(this.map, {
          onSearchComplete: this.searchPlace,
        });
        this.local.search(this.keyWords);
      },
      searchPlace() {
        if (this.local.getResults() != undefined) {
          this.map.clearOverlays(); //清除地圖上所有覆蓋物
          if (this.local.getResults().getPoi(0)) {
            let point = this.local.getResults().getPoi(0).point; //獲取第一個(gè)智能搜索的結(jié)果
            this.map.centerAndZoom(point, 18);
            this.handleMarker(this, point);
            console.log("經(jīng)度:" + point.lng + "--" + "緯度" + point.lat);
            this.latitude = point.lat;
            this.longitude = point.lng;
          } else {
            this.$message.error("未匹配到地點(diǎn)!");
          }
        } else {
          this.$message.error("未找到搜索結(jié)果!");
        }
      },
      // 設(shè)置標(biāo)注
      handleMarker(obj, point) {
        let that = this;
        obj.mk = new BMap.Marker(point);
        obj.map.addOverlay(obj.mk);
        obj.mk.enableDragging(); // 可拖拽
        obj.mk.addEventListener("dragend", function (e) {
          // 監(jiān)聽標(biāo)注的拖拽,獲取拖拽后的經(jīng)緯度
          that.latitude = e.point.lat;
          that.longitude = e.point.lng;
        });
        obj.map.panTo(point);
      },
    }
  };
</script>

CSS代碼

<style scoped>
  .form-layer {
    width: 100%;
  }
  #map {
    margin-top: 30px;
    width: 100%;
    height: 300px;
    border: 1px solid gray;
    box-sizing: border-box;
    overflow: hidden;
  }
  /deep/ .el-dialog {
    min-width: 550px;
  }
  /deep/ .el-dialog__body {
    padding: 10px;
  }
</style>

完整代碼

<template>
  <div>
    <el-dialog
      @close="clearDialog"
      :close-on-click-modal="false"
      :title="text"
      style="text-align: left"
      :visible.sync="popup"
      width="30%"
    >
      <div class="form-layer">
        <el-form label-width="100px" size="mini">
          <el-form-item label="獲取定位">
            <el-button type="primary" @click="fixedPos">重新定位</el-button>
          </el-form-item>
          <el-form-item label="當(dāng)前緯度">
            <p>{{latitude}}</p>
          </el-form-item>
          <el-form-item label="當(dāng)前經(jīng)度">
            <p>{{longitude}}</p>
          </el-form-item>
          <el-form-item>
            <div class="f-a-c">
              <el-input v-model="keyWords" placeholder="請(qǐng)輸入地區(qū)" style="width: 230px;margin-right: 6px;"></el-input>
              <el-button type="primary" @click="setPlace" :disabled="!keyWords">查詢</el-button>
            </div>
          </el-form-item>
        </el-form>
        <div id="map"></div>
      </div>
      <div slot="footer" class="dialog-footer">
        <el-button
          size="small"
          type="primary"
          v-if="type != '2'"
          @click="btnSubmit()"
          >確 認(rèn)</el-button
        >
        <el-button size="small" @click="popup = false">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
  export default {
    name: "mapView",
    data() {
      return {
        map: null,
        local: null,
        mk: null,
        latitude: '',
        longitude: '',
        keyWords: ''
      };
    },
    methods: {
      // 打開彈窗,name為彈窗名稱
      async openDialog(name) {
        this.text = name;
        this.popup = true;
        this.initMap();
      },
      // 確認(rèn)
      btnSubmit() {
        let key = {
          latitude: this.latitude,
          longitude: this.longitude
        }
        // 打印經(jīng)緯度
        console.log(key);
        this.popup = false;
      },
      initMap() {
        this.$nextTick(() => {
          this.map = new BMap.Map("map");
          let point = new BMap.Point(116.404, 39.915);
          this.map.centerAndZoom(point, 12);
          this.map.enableScrollWheelZoom(true); // 開啟鼠標(biāo)滾輪縮放
          this.map.addControl(new BMap.NavigationControl());
          this.fixedPos();
        });
      },
      // 點(diǎn)擊定位-定位到當(dāng)前位置
      fixedPos() {
        const _this = this;
        const geolocation = new BMap.Geolocation();
        this.confirmLoading = true;
        geolocation.getCurrentPosition(function (r) {
          if (this.getStatus() == BMAP_STATUS_SUCCESS) {
            _this.handleMarker(_this, r.point);
            let myGeo = new BMap.Geocoder();
            myGeo.getLocation(
              new BMap.Point(r.point.lng, r.point.lat),
              function (result) {
                _this.confirmLoading = false;
                if (result) {
                  _this.latitude = result.point.lat;
                  _this.longitude = result.point.lng;
                }
              }
            );
          } else {
            _this.$message.error("failed" + this.getStatus());
          }
        });
      },
      // 搜索地址
      setPlace() {
        this.local = new BMap.LocalSearch(this.map, {
          onSearchComplete: this.searchPlace,
        });
        this.local.search(this.keyWords);
      },
      searchPlace() {
        if (this.local.getResults() != undefined) {
          this.map.clearOverlays(); //清除地圖上所有覆蓋物
          if (this.local.getResults().getPoi(0)) {
            let point = this.local.getResults().getPoi(0).point; //獲取第一個(gè)智能搜索的結(jié)果
            this.map.centerAndZoom(point, 18);
            this.handleMarker(this, point);
            console.log("經(jīng)度:" + point.lng + "--" + "緯度" + point.lat);
            this.latitude = point.lat;
            this.longitude = point.lng;
          } else {
            this.$message.error("未匹配到地點(diǎn)!");
          }
        } else {
          this.$message.error("未找到搜索結(jié)果!");
        }
      },
      // 設(shè)置標(biāo)注
      handleMarker(obj, point) {
        let that = this;
        obj.mk = new BMap.Marker(point);
        obj.map.addOverlay(obj.mk);
        obj.mk.enableDragging(); // 可拖拽
        obj.mk.addEventListener("dragend", function (e) {
          // 監(jiān)聽標(biāo)注的拖拽,獲取拖拽后的經(jīng)緯度
          that.latitude = e.point.lat;
          that.longitude = e.point.lng;
        });
        obj.map.panTo(point);
      },
    }
  };
</script>
<style scoped>
  .form-layer {
    width: 100%;
  }
  #map {
    margin-top: 30px;
    width: 100%;
    height: 300px;
    border: 1px solid gray;
    box-sizing: border-box;
    overflow: hidden;
  }
  /deep/ .el-dialog {
    min-width: 550px;
  }
  /deep/ .el-dialog__body {
    padding: 10px;
  }
</style>

到此這篇關(guān)于vue中調(diào)用百度地圖獲取經(jīng)緯度的實(shí)現(xiàn)的文章就介紹到這了,更多相關(guān)vue調(diào)用百度地圖獲取經(jīng)緯度內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • vue如何統(tǒng)一樣式(reset.css與border.css)

    vue如何統(tǒng)一樣式(reset.css與border.css)

    這篇文章主要介紹了vue如何統(tǒng)一樣式(reset.css與border.css),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-05-05
  • Vue3.0中的monorepo管理模式的實(shí)現(xiàn)

    Vue3.0中的monorepo管理模式的實(shí)現(xiàn)

    這篇文章主要介紹了Vue3.0中的monorepo管理模式的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-10-10
  • 關(guān)于vant的日歷組件,在iPhonex上可選日期空白

    關(guān)于vant的日歷組件,在iPhonex上可選日期空白

    這篇文章主要介紹了關(guān)于vant的日歷組件,在iPhonex上可選日期空白,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-04-04
  • vue2結(jié)合element-ui的gantt圖實(shí)現(xiàn)可拖拽甘特圖

    vue2結(jié)合element-ui的gantt圖實(shí)現(xiàn)可拖拽甘特圖

    因?yàn)楣ぷ髦幸玫礁侍貓D,所以我在網(wǎng)上搜索可以用的甘特圖,搜索了好多,但是網(wǎng)上搜到大多數(shù)都很雞肋,不能直接使用,下面這篇文章主要給大家介紹了關(guān)于vue2結(jié)合element-ui的gantt圖實(shí)現(xiàn)可拖拽甘特圖的相關(guān)資料,需要的朋友可以參考下
    2022-11-11
  • vue中如何攜帶參數(shù)跳轉(zhuǎn)頁面

    vue中如何攜帶參數(shù)跳轉(zhuǎn)頁面

    這篇文章主要介紹了vue中如何攜帶參數(shù)跳轉(zhuǎn)頁面問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-04-04
  • vue項(xiàng)目history模式刷新404問題解決辦法

    vue項(xiàng)目history模式刷新404問題解決辦法

    這篇文章主要給大家介紹了關(guān)于vue項(xiàng)目history模式刷新404問題的解決辦法,需要的朋友可以參考下
    2023-11-11
  • 在vue-cli腳手架中配置一個(gè)vue-router前端路由

    在vue-cli腳手架中配置一個(gè)vue-router前端路由

    這篇文章主要給大家介紹了在vue-cli腳手架中配置一個(gè)vue-router前端路由的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面跟著小編一起來學(xué)習(xí)學(xué)習(xí)吧。
    2017-07-07
  • Vue.js移動(dòng)端左滑刪除組件的實(shí)現(xiàn)代碼

    Vue.js移動(dòng)端左滑刪除組件的實(shí)現(xiàn)代碼

    本篇文章主要介紹了Vue.js移動(dòng)端左滑刪除組件的實(shí)現(xiàn)代碼,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2017-09-09
  • npm?install安裝報(bào)錯(cuò)的幾種常見情況

    npm?install安裝報(bào)錯(cuò)的幾種常見情況

    當(dāng)你跑起一個(gè)項(xiàng)目的時(shí)候,第一步需要先安裝依賴npm install,下面這篇文章主要給大家介紹了關(guān)于npm?install安裝報(bào)錯(cuò)的幾種常見情況,文中通過示例代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2022-07-07
  • Vue子組件如何修改父組件數(shù)據(jù)的方法及注意事項(xiàng)

    Vue子組件如何修改父組件數(shù)據(jù)的方法及注意事項(xiàng)

    這篇文章主要介紹了Vue子組件如何修改父組件數(shù)據(jù)的方法及注意事項(xiàng),具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2024-06-06

最新評(píng)論

休宁县| 山阴县| 东安县| 樟树市| 双辽市| 于都县| 中超| 平武县| 贵溪市| 修武县| 舒城县| 徐水县| 渭源县| 汉源县| 嘉黎县| 滨海县| 高陵县| 旺苍县| 衡东县| 深水埗区| 景宁| 通江县| 呈贡县| 林西县| 嘉义县| 贵阳市| 曲阜市| 呼伦贝尔市| 日土县| 华蓥市| 邓州市| 修文县| 白朗县| 当阳市| 祁门县| 北流市| 永修县| 清水县| 汉中市| 凌海市| 会理县|