欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

vue中调用百度地图获取经纬度的实现

程序员文章站 2022-07-05 22:19:00
项目中,需要实现获取当前位置的经纬度,或者搜索某个位置并获取经纬度信息,我使用的的是vue,地图使用的是百度地图。默认自动获取当前位置经纬度拖动小红标 获取经纬度关键词 查询获取经纬度前期准备首先,我...

项目中,需要实现获取当前位置的经纬度,或者搜索某个位置并获取经纬度信息,我使用的的是vue,地图使用的是百度地图。

vue中调用百度地图获取经纬度的实现

默认自动获取当前位置经纬度

vue中调用百度地图获取经纬度的实现

拖动小红标 获取经纬度

vue中调用百度地图获取经纬度的实现

关键词 查询获取经纬度

前期准备

首先,我们需要取百度官方申请一个地图api秘钥, 进入后在应用管理,我的应用去申请即可。

申请好以后,我们打开vue项目中public文件下的index.html文件,拼接百度ak值并引入

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

如上所示,红色区域为ak值,自行拼接自己的,可以设置权限为公开或者针对网址白名单。

<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="当前纬度">
            <p>{{latitude}}</p>
          </el-form-item>
          <el-form-item label="当前经度">
            <p>{{longitude}}</p>
          </el-form-item>
          <el-form-item>
            <div class="f-a-c">
              <el-input v-model="keywords" placeholder="请输入地区" 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()"
          >确 认</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();
      },
      // 确认
      btnsubmit() {
        let key = {
          latitude: this.latitude,
          longitude: this.longitude
        }
        // 打印经纬度
        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); // 开启鼠标滚轮缩放
          this.map.addcontrol(new bmap.navigationcontrol());
          this.fixedpos();
        });
      },
      // 点击定位-定位到当前位置
      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; //获取第一个智能搜索的结果
            this.map.centerandzoom(point, 18);
            this.handlemarker(this, point);
            console.log("经度:" + point.lng + "--" + "纬度" + point.lat);
            this.latitude = point.lat;
            this.longitude = point.lng;
          } else {
            this.$message.error("未匹配到地点!");
          }
        } else {
          this.$message.error("未找到搜索结果!");
        }
      },
      // 设置标注
      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) {
          // 监听标注的拖拽,获取拖拽后的经纬度
          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="当前纬度">
            <p>{{latitude}}</p>
          </el-form-item>
          <el-form-item label="当前经度">
            <p>{{longitude}}</p>
          </el-form-item>
          <el-form-item>
            <div class="f-a-c">
              <el-input v-model="keywords" placeholder="请输入地区" 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()"
          >确 认</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();
      },
      // 确认
      btnsubmit() {
        let key = {
          latitude: this.latitude,
          longitude: this.longitude
        }
        // 打印经纬度
        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); // 开启鼠标滚轮缩放
          this.map.addcontrol(new bmap.navigationcontrol());
          this.fixedpos();
        });
      },
      // 点击定位-定位到当前位置
      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; //获取第一个智能搜索的结果
            this.map.centerandzoom(point, 18);
            this.handlemarker(this, point);
            console.log("经度:" + point.lng + "--" + "纬度" + point.lat);
            this.latitude = point.lat;
            this.longitude = point.lng;
          } else {
            this.$message.error("未匹配到地点!");
          }
        } else {
          this.$message.error("未找到搜索结果!");
        }
      },
      // 设置标注
      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) {
          // 监听标注的拖拽,获取拖拽后的经纬度
          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>

到此这篇关于vue中调用百度地图获取经纬度的实现的文章就介绍到这了,更多相关vue调用百度地图获取经纬度内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!