vue中百度地图定位及附近搜索功能使用步骤

目录
  • 1.地图初始化相关
  • 2.获取当前定位
  • 3.根据当前定位地址附近搜索建议

1.地图初始化相关

文档:lbs.baidu.com/index.php?t…

申请账号 => 创建应用 => 生成key值 => 引入百度地图,替换key值

在出口html(public/html)文件下引入标签

<script type="text/javascript" src="https://api.map.baidu.com/api?v=1.0&&type=webgl&ak=申请的ak">

2.获取当前定位

文档:lbsyun.baidu.com/index.php?t…

创建地图容器 可以为其他id名, 但必须有 用来展示地图, 地图大小与container大小一致

<div id="container">
</div>

获取当前位置

*注意,初始化地图,获取定位时,必须在mounted钩子中,因为要先获取到地图容器

对于全局变量需要加上window(脚手架规则)

 // 地图获取当前定位
    getPosition(){
      // 创建地图实例
      const map = new window.BMapGL.Map('container')
      // 创建浏览器定位实例
      var geolocation = new window.BMapGL.Geolocation();
      let that = this
      geolocation.getCurrentPosition(function(r){
        if(this.getStatus() == BMAP_STATUS_SUCCESS){
          // 创建点标记
          var mk = new BMapGL.Marker(r.point);
          // 在地图上添加点标记
          map.addOverlay(mk);
          // 将地图平移至中心点
          map.panTo(r.point);
          console.log('您的位置:' + r.point.lng + ',' + r.point.lat);
          that.position.signLongitude = r.point.lng
          that.position.signLatitude = r.point.lat
          // 创建点坐标
          const point = new window.BMapGL.Point(r.point.lng, r.point.lat)
          //  初始化地图,设置中心点坐标和地图级别
          map.centerAndZoom(point, 30)
          // 逆向编码
          var myGeo = new BMapGL.Geocoder();
          myGeo.getLocation(new BMapGL.Point(r.point.lng,r.point.lat), function(result){
            if (result){
              that.address = result.address // 获取逆编程的地址结果
            }
          });
          }else{
            alert('failed' + this.getStatus());
          }
      });
    },

钉钉签到定位完整代码

<template>
  <div class="sign-in">
    <div v-if="isSign">
      <div class="time-sign">2022年08月26日</div>
      <div class="map-card">
        <div class="title">
          <h3 class="ellipsis"><img class="company" src="../../assets/images/公司@2x.png" alt="">{{address}}</h3>
          <span class="position" @click="$router.replace(`/map?planId=${$route.query.planId}`)">地址微调</span>
        </div>
        <div class="map">
          <div id="container">
          </div>
        </div>
      </div>
      <div class="sign-in-box" @click="toSignIn">
        <span>签到</span>
        <div class="time">{{dateNow}}</div>
        <span class="no-sign">未签到</span>
      </div>
    </div>
  </div>
</template>
<script>
import dayjs from 'dayjs'
import { Toast } from 'vant';
export default {
  name: 'sign-in',
  data () {
    return {
      dateNow: new Date().getHours() + ':' + new Date().getMinutes() + ':' + new Date().getSeconds(),
      timer: null,
      isSign:true,
      // 定位地点
      address: this.$route.query.address ||  '',
      // 经纬度
      position:{
        // 经
        signLongitude:'',
        // 维
        signLatitude:''
      }
    }
  },
  created () {
    this.timer = setInterval(() => {
      const dateNow = Date.now()
      this.dateNow = dayjs(dateNow).format('HH:mm:ss')
    }, 1000)
    if (this.$dd.env.platform === 'notInDingTalk') return
    this.$dd.biz.navigation.setTitle({
      title: '签到',
      onSuccess: function (res) {
        // 调用成功时回调
        console.log(res)
      },
      onFail: function (err) {
        // 调用失败时回调
        console.log(err)
      }
    })
  },
  methods:{
    // 点击签到
    toSignIn(){
      if(!this.address) {
        Toast('暂未定位到地址')
        return
      }
      this.isSign = false
    },
    getAddress(address){
      console.log(address);
      this.address = address
    },
    // 地图获取当前定位
    getPosition(){
      const map = new window.BMapGL.Map('container')
      var geolocation = new window.BMapGL.Geolocation();
      let that = this
      geolocation.getCurrentPosition(function(r){
        if(this.getStatus() == BMAP_STATUS_SUCCESS){
          var mk = new BMapGL.Marker(r.point);
          map.addOverlay(mk);
          map.panTo(r.point);
          console.log('您的位置:' + r.point.lng + ',' + r.point.lat);
          const point = new window.BMapGL.Point(r.point.lng, r.point.lat)
          that.position.signLongitude = r.point.lng
          that.position.signLatitude = r.point.lat
          map.centerAndZoom(point, 30)
          // 逆向编码
          var myGeo = new BMapGL.Geocoder();
          myGeo.getLocation(new BMapGL.Point(r.point.lng,r.point.lat), function(result){
            if (result){
              that.address = result.address
            }
          });
          }else{
            alert('failed' + this.getStatus());
          }
      });
    },
    // 展示地图
    setPosition(){
          // 经度
        const signLongitude = this.$route.query.lng
        // 维度
        const signLatitude = this.$route.query.lat
        var map = new BMapGL.Map("container");          // 创建地图实例
        var point = new BMapGL.Point(signLongitude , signLatitude);  // 创建点坐标
        map.centerAndZoom(point, 15);
        // 创建点标记
        var marker1 = new BMapGL.Marker(new BMapGL.Point(signLongitude, signLatitude));
        // 在地图上添加点标记
        map.addOverlay(marker1);
    }
  },
  destroyed () {
    clearInterval(this.timer)
  },
  mounted () {
    // 有传来的地址就回显定位,  无则进行定位
    if(this.address) {
      this.setPosition()
    } else{
      this.getPosition()
    }
    // const point = new window.BMapGL.Point(116.404, 39.915)
    // map.centerAndZoom(point, 30)
  },
}
</script>
<style scoped lang="less">
.sign-in{
  height: 100vh;
  background-color: #F9F9F9;
  #container{
  width: 315px;
  height: 100px;
  }
  .company{
    width: 12px;
    height: 14px;
    margin-right: 5px;
  }
  .position{
    white-space: nowrap;
  }
  .time-sign{
    margin-left: 15px;
    height: 40px;
    line-height: 40px;
    font-size: 12px;
    color: #666666;
  }
  .map-card{
    padding: 15px;
    margin: auto;
    width: 345px;
    height: 180px;
    background-color: #fff;
    border-radius: 12px;
    .title{
      display: flex;
      width: 100%;
      justify-content: space-between;
      font-size: 12px;
      span{
        color: #30A7FA;
        line-height: 1;
      }
    }
    h3{
      height: 32px;
      line-height: 1;
      font-size: 16px;
      border-bottom: 1px solid #f8f8f8;
    }
    .map{
      margin-top: 15px;
    }
  }
  .sign-in-box{
    position: relative;
    margin: auto;
    margin-top: 147px;
    width: 140px;
    height: 140px;
    background-color: #30A7FA;
    border-radius: 50%;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    color: #fff;
    box-shadow: 0px 4px 13px 0px #BDE4FF;
    .time{
      margin-top: 7px;
    }
    .no-sign{
      position: absolute;
      top: 150px;
      color: #999999;
      font-size: 12px;
    }
  }
}
</style>

3.根据当前定位地址附近搜索建议

文档:lbsyun.baidu.com/jsdemo.htm#…

根据当前定位的结果,给出建议相关列表

html相关:

<template>
  <div class="map-page">
    <div id="container-map">
    </div>
    <div class="search">
      <van-search
        v-model="value"
        shape="round"
        placeholder="请输入搜索关键词"
        @focus="$router.replace(`/searchMap?planId=${$route.query.planId}`)"
      />
    </div>
    <div class="addressList" >
      <van-cell v-for="(item,index) in addressList" :key="index" :value="item.address" @click="chooseMap(item)" />
    </div>
  </div>
</template>

js相关:

  mounted () {
     // 创建地图实例
    const map = new window.BMapGL.Map('container-map')
    // 创建浏览器定位实例
    var geolocation = new window.BMapGL.Geolocation();
    let that = this
    geolocation.getCurrentPosition(function(r){
      if(this.getStatus() == BMAP_STATUS_SUCCESS){
        // 创建点标记
        var mk = new BMapGL.Marker(r.point);
        // 在地图上添加点标记
        map.addOverlay(mk);
        // 将地图平移至中心点
        map.panTo(r.point);
        // console.log('您的位置:' + r.point.lng + ',' + r.point.lat);
        const point = new window.BMapGL.Point(r.point.lng, r.point.lat)
        that.lng = r.point.lng
        that.lat = r.point.lat
        console.log(r.point.lng, r.point.lat);
        map.centerAndZoom(point, 30)
        // 逆向编码
        var myGeo = new BMapGL.Geocoder();
        myGeo.getLocation(new BMapGL.Point(r.point.lng,r.point.lat), function(result){
          if (result){
            console.log(result.address);
            // 获取到当前定位的结果, 调用搜索建议
            that.getLocation(result.address)
          }
        });
        }else{
          alert('failed' + this.getStatus());
        }
    });
    // const point = new window.BMapGL.Point(116.404, 39.915)
    // map.centerAndZoom(point, 30)
  },
  methods:{
    // 搜索建议
    getLocation(address){
      var map = new BMapGL.Map("container-map");
      var mPoint = new BMapGL.Point(this.lng, this.lat);
      map.enableScrollWheelZoom();
      map.centerAndZoom(mPoint,15);
      // 绘制圆形范围覆盖物
      var circle = new BMapGL.Circle(mPoint,1000,{fillColor:"blue", strokeWeight: 1 ,fillOpacity: 0.3, strokeOpacity: 0.3});
        map.addOverlay(circle);
        var local =  new BMapGL.LocalSearch(map, {renderOptions: {map: map, autoViewport: false}});
        // 定义搜索地址,以及范围距离
        local.searchNearby(address,mPoint,1000);
        console.log(local);
        this.addressList = local._arrPois
    },
    // 点击选择地址 lng 经度  lat 维度
    chooseMap(addressItem){
      this.addressDetail=addressItem.address
      // 经度
      const lng = addressItem.marker.latLng.lng
      // 维度
      const lat = addressItem.marker.latLng.lat
      this.$router.replace(`/signIn?address=${this.addressDetail}&planId=${this.$route.query.planId}&lng=${lng}&lat=${lat}`)
      // that.$emit('getAddress', this.addressDetail)
    }
  }

完整代码:

<template>
  <div class="map-page">
    <div id="container-map">
    </div>
    <div class="search">
      <van-search
        v-model="value"
        shape="round"
        placeholder="请输入搜索关键词"
        @focus="$router.replace(`/searchMap?planId=${$route.query.planId}`)"
      />
    </div>
    <div class="addressList" >
      <van-cell v-for="(item,index) in addressList" :key="index" :value="item.address" @click="chooseMap(item)" />
    </div>
  </div>
</template>
<script>
import { Search , Cell} from 'vant'
export default {
  name: 'map-page',
  components:{
    [Search.name]:Search,
    [Cell.name]:Cell,
  },
  data () {
    return {
      value:"",
      lng:0, // 经度
      lat:0, // 维度
      // 搜索地址列表
      addressList:[],
      // 详细地址
      addressDetail:""
    }
  },
  mounted () {
    const map = new window.BMapGL.Map('container-map')
    var geolocation = new window.BMapGL.Geolocation();
    let that = this
    geolocation.getCurrentPosition(function(r){
      if(this.getStatus() == BMAP_STATUS_SUCCESS){
        var mk = new BMapGL.Marker(r.point);
        map.addOverlay(mk);
        map.panTo(r.point);
        // console.log('您的位置:' + r.point.lng + ',' + r.point.lat);
        const point = new window.BMapGL.Point(r.point.lng, r.point.lat)
        that.lng = r.point.lng
        that.lat = r.point.lat
        console.log(r.point.lng, r.point.lat);
        map.centerAndZoom(point, 30)
        // 逆向编码
        var myGeo = new BMapGL.Geocoder();
        myGeo.getLocation(new BMapGL.Point(r.point.lng,r.point.lat), function(result){
          if (result){
            console.log(result.address);
            that.getLocation(result.address)
          }
        });
        }else{
          alert('failed' + this.getStatus());
        }
    });
    // const point = new window.BMapGL.Point(116.404, 39.915)
    // map.centerAndZoom(point, 30)
  },
  methods:{
    // 搜索建议
    getLocation(address){
      var map = new BMapGL.Map("container-map");
      var mPoint = new BMapGL.Point(this.lng, this.lat);
      map.enableScrollWheelZoom();
      map.centerAndZoom(mPoint,15);
      var circle = new BMapGL.Circle(mPoint,1000,{fillColor:"blue", strokeWeight: 1 ,fillOpacity: 0.3, strokeOpacity: 0.3});
        map.addOverlay(circle);
        var local =  new BMapGL.LocalSearch(map, {renderOptions: {map: map, autoViewport: false}});
        local.searchNearby(address,mPoint,1000);
        console.log(local);
        this.addressList = local._arrPois
    },
    // 点击选择地址 lng 经度  lat 维度
    chooseMap(addressItem){
      this.addressDetail=addressItem.address
      // 经度
      const lng = addressItem.marker.latLng.lng
      // 维度
      const lat = addressItem.marker.latLng.lat
      this.$router.replace(`/signIn?address=${this.addressDetail}&planId=${this.$route.query.planId}&lng=${lng}&lat=${lat}`)
      // that.$emit('getAddress', this.addressDetail)
    }
  }
}
</script>
<style scoped lang="less">
// 地图大小
#container-map{
  width: 100%;
  height: 180px;
}
</style>

以上就是vue中百度地图定位及附近搜索功能使用步骤的详细内容,更多关于vue百度地图定位附近搜索的资料请关注我们其它相关文章!

(0)

相关推荐

  • vue整合百度地图显示指定地点信息

    目录 一.安装相关依赖 二.在main.js中引用 三.创建地图工具 map.js 四.绘制地图 先看看效果图 一.安装相关依赖 npm i --save vue-baidu-map 二.在main.js中引用 import BaiduMap from "vue-baidu-map" Vue.use(BaiduMap,{   ak: '你的密钥(百度地图开放API官网可免费申请)' }) 三.创建地图工具 map.js export function MP(ak) {       re

  • Vue集成百度地图实现位置选择功能

    目录 Vue集成百度地图实现位置选择 百度地图选择地址效果 集成百度地图的具体实现 第一步:引入百度地图 JavaScript API v3.0 文件 第二步:编写百度地图选择位置组件 第三步:使用百度地图选择位置组件 百度地图Web开发 参考文章 Vue集成百度地图实现位置选择 需求:添加门店时,需要选择门店的省.市.区.详细地址.以及门店的经纬度信息. 解决方案:集成百度地图API,通过在地图上搜索或者点击获取门店的具体位置信息. 百度地图选择地址效果 具体效果如下图所示 集成百度地图的具体

  • Vue使用百度地图实现城市定位

    本文实例为大家分享了Vue使用百度地图实现城市定位的具体代码,供大家参考,具体内容如下 Vue项目运行环境:Vue 2.0,Vue Cli 3.0 步骤一:登录 百度地图开放平台 在 控制台---->应用管理---->我的应用 下面创建一个应用. 目的是获取 ak 步骤二:在public文件夹的index.html文件中 导入百度地图,拼上你的ak 步骤三:在项目的根目录中创建vue.config.js文件 如果有vue.config.js文件的话,直接添加以下代码即可. vue.config

  • vue实现滚动条到顶部或者到指定位置

    目录 vue滚动条到顶部或到指定位置 vue设置滚动条 vue如何在div中设置滚动条呢? vue滚动条到顶部或到指定位置 首先在 html 里面给你要滚动的元素设置属性 ref='box' 这就相当于是DOM操作了 , 然后 根据 属性名找到则个元素就可以操作啦 vue设置滚动条 vue如何在div中设置滚动条呢? 首先需要写一下css样式 <div     :style="{'max-height': this.timeLineHeight + 'px' }"     sty

  • VUE + OPENLAYERS实现实时定位功能

    前言 本系列文章介绍一个简单的实时定位示例,示例的组成主要包括: 服务后端,使用 Java 语言编写,模拟生成 GeoJSON 数据. 前端展示,使用 Vue + OpenLayers ,负责定时向后端服务请求 GeoJSON 数据,并在以标签的形式展现定位数据. 实现的效果: 一.定义标签样式 var image = new CircleStyle({ radius: 5, fill: new Fill({ color: "rgba(255, 0, 0, 1)" }), stroke

  • vue项目实现便捷接入百度地图API

    目录 1.账号注册 2.获取密钥 3.创建项目 4.项目导入 5.效果展示 1.账号注册 在百度地图开放平台注册账号并登录网站地址: https://lbsyun.baidu.com/index.php?title=jspopularGL 2.获取密钥 进入开发文档并进行密钥申请 3.创建项目 4.项目导入 此时我们已经获取了密钥,接着就可以在项目中导入了 首先安装百度地图 npm install vue-baidu-map --save 接着注册(这里我采用局部注册) //局部注册 百度地图

  • vue中百度地图定位及附近搜索功能使用步骤

    目录 1.地图初始化相关 2.获取当前定位 3.根据当前定位地址附近搜索建议 1.地图初始化相关 文档:lbs.baidu.com/index.php?t… 申请账号 => 创建应用 => 生成key值 => 引入百度地图,替换key值 在出口html(public/html)文件下引入标签 <script type="text/javascript" src="https://api.map.baidu.com/api?v=1.0&&

  • vue百度地图 + 定位的详解

    vue 百度地图 + 定位 前提需要自己有百度的密钥,如没有可以去百度地图申请 一.在主目录下的index.html引入js,例如:   二.在webpack.base.conf.js配置文件中配置BMap,在module.exports 中与entry平级,例如: 三.在项目中引入BMap: 四.代码: <template> <div class="home"> <div id="allmap" class="allmap&

  • vue项目使用高德地图的定位及关键字搜索功能的实例代码(踩坑经验)

    1.首先在index.html引入高德地图的秘钥.如图: 注意:如果使用关键字搜索功能要加上 plugin=AMap.Autocomplete,AMap.PlaceSearch,否则功能无法使用,并会报错 2. 定位功能,代码如下: const map = new AMap.Map(this.$refs.container, { resizeEnable: true }) // 创建Map实例 const options = { 'showButton': true, // 是否显示定位按钮 '

  • Android百度地图定位后获取周边位置的实现代码

    本文实例讲解Android百度地图定位后获取周边位置的实现代码,分享给大家供大家参考,具体内容如下 效果图: 具体代码: 1.布局文件 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical&q

  • Android 百度地图定位实现仿钉钉签到打卡功能的完整代码

    导语 本章根据百度地图API,实现仿钉钉打卡功能.用到了基础地图.覆盖物.定位图层.陀螺仪方法.悬浮信息弹框. 百度地图API地址  :Android 地图SDK 请先注册注册百度账号和获取密钥,并实现地图显示出来.(注意:密钥.权限要设置) 另外,我得说明本章所下载官方Demo 和 导入的jar包和so文件.自定义下载即可,如下图: 接下来,一起看实现效果. 源码Git地址:BaiduMapApp 效果图 实现代码·三步骤 第一步:基础地图和方向传感器 类先实现方向传感器 implements

  • 微信企业号开发之微信考勤百度地图定位

    之前在微信企业号开发:微信考勤中使用了百度地图的定位组件,但发现在部分手机上会出现定位失败的提示,于是有研究了一下百度地图.原来使用的Web组件百度不打算更新了,也是重新查了一下百度地图的其他API,还有一个JavaScript API大众版,于是试了试,没想到竟然解决了. 核心代码很简单: <div id="allmap"></div> <script type="text/javascript" src="http://a

  • JS开发中百度地图+城市联动实现实时触发查询地址功能

    缘由: 由于项目需要实现一个根据省市区+详细地址的路径进行查询地址的功能. 所用技术:百度地图API+jQuery 实现步骤: 1.省市区三级联动(ps:已经忘记这个小插件的出处的) 引入area.js /* * 全国三级城市联动 js版 */ function Dsy(){ this.Items = {}; } Dsy.prototype.add = function(id,iArray){ this.Items[id] = iArray; } Dsy.prototype.Exists = f

  • iOS实现百度地图定位签到功能

    写在前面: 项目需求用到这个功能,主要目的是实现老师设置位置签到范围,学生在一定范围内进行签到的功能. 功能如下方截图: 屏幕快照 2019-01-28 上午10.29.26.png 简要介绍: 下面记录一下主要的实现流程,功能的实现主要是根据百度地图开发者官网提供的api文档,各项功能之间组合.百度地图的SDK现在分成了地图功能和定位功能两块不同的SDK,BaiduMapAPI这个是基础的地图功能,BMKLocationKit这个是定位功能.项目里实现定位签到功能用的的SDK包括上面说的这两个

随机推荐