vue baidu map之获取选中点的经纬度

需求:在vue项目中通过点击按钮,弹出百度地图的弹框,用户选择地图上的点或者通过搜索关键字选点,点击确认 关闭弹窗 得到经纬度

效果:

vue baidu map api链接 : https://dafrok.github.io/vue-baidu-map/#/zh/index

使用之前需要先申请百度服务密钥

由于要多次用到地图选点,故封装成一个简单的组件,在需要的地方进行引入,弹窗用的是iview的modal,本例中用的是局部引入vue baidu map

在需要的地方引入组件

eg: <mapComponent v-model="showMapComponent" v-on:cancel="cancelMap" v-on:map-confirm="confirmMap"></mapComponent>

confirmMap中得到回传数据,当然,可根据实际需要回传需要的数据,这里只回传了经纬度

附:源代码

<!--
使用说明:
   v-on:  map-confirm,参数为array数组,传递经纬度值
   v-on   cancel    取消时发出事件
-->
<template>
  <div style="padding-top:50px; border:1px solid red">
    <Modal @on-cancel="cancel" v-model="showMapComponent" width="800" :closable="false" :mask-closable="false">
      <baidu-map v-bind:style="mapStyle" class="bm-view" ak="你的密钥"
      :center="center"
      :zoom="zoom"
      :scroll-wheel-zoom="true"
      @click="getClickInfo"
      @moving="syncCenterAndZoom"
      @moveend="syncCenterAndZoom"
      @zoomend="syncCenterAndZoom">
        <bm-view style="width: 100%; height:500px;"></bm-view>
        <bm-marker :position="{lng: center.lng, lat: center.lat}" :dragging="true" animation="BMAP_ANIMATION_BOUNCE">
        </bm-marker>
        <bm-control :offset="{width: ‘10px‘, height: ‘10px‘}">
          <bm-auto-complete v-model="keyword" :sugStyle="{zIndex: 999999}">
            <input type="text" placeholder="请输入搜索关键字" class="serachinput">
          </bm-auto-complete>
        </bm-control>
        <bm-local-search :keyword="keyword" :auto-viewport="true" style="width:0px;height:0px;overflow: hidden;"></bm-local-search>
      </baidu-map>
      <div slot="footer" style="margin-top: 0px;">
        <Button @click="cancel" type="ghost"
                style="width: 60px;height: 36px;">取消
        </Button>
        <Button type="primary" style="width: 60px;height: 36px;" @click="confirm">确定</Button>
      </div>
    </Modal>
  </div>
</template>
<script>
  import {BaiduMap, BmControl, BmView, BmAutoComplete, BmLocalSearch, BmMarker} from ‘vue-baidu-map‘
  export default {
    components: {
      BaiduMap,
      BmControl,
      BmView,
      BmAutoComplete,
      BmLocalSearch,
      BmMarker
    },
    data: function () {
      return {
        showMapComponent: this.value,
        keyword: ‘‘,
        mapStyle: {
          width: ‘100%‘,
          height: this.mapHeight + ‘px‘
        },
        center: {lng: 116.404, lat: 39.915},
        zoom: 15
      }
    },
    watch: {
      value: function (currentValue) {
        this.showMapComponent = currentValue
        if (currentValue) {
          this.keyword = ‘‘
        }
      }
    },
    props: {
      value: Boolean,
      mapHeight: {
        type: Number,
        default: 500
      }
    },
    methods: {
      /***
       * 地图点击事件。
       */
      getClickInfo (e) {
        this.center.lng = e.point.lng
        this.center.lat = e.point.lat
      },
      syncCenterAndZoom (e) {
        const {lng, lat} = e.target.getCenter()
        this.center.lng = lng
        this.center.lat = lat
        this.zoom = e.target.getZoom()
      },
      /***
       * 确认
       */
      confirm: function () {
        this.showMapComponent = false
        this.$emit(‘map-confirm‘, this.center)
      },
      /***
       * 取消
       */
      cancel: function () {
        this.showMapComponent = false
        this.$emit(‘cancel‘, this.showMapComponent)
      }
    }
  }
</script>

<style scoped>
.serachinput{
  width: 300px;
  box-sizing: border-box;
  padding: 9px;
  border: 1px solid #dddee1;
  line-height: 20px;
  font-size: 16px;
  height: 38px;
  color: #333;
  position: relative;
  border-radius: 4px;
  -webkit-box-shadow: #666 0px 0px 10px;
  -moz-box-shadow: #666 0px 0px 10px;
  box-shadow: #666 0px 0px 10px;
}
</style>

原文地址:https://www.cnblogs.com/moqq/p/8489299.html

时间: 2024-08-03 23:20:27

vue baidu map之获取选中点的经纬度的相关文章

关于 Vue Baidu Map 自动定位

vue 中自动定位 <template> <baidu-map class="map" :zoom="zoom" :center="center" @ready="handler"> <bm-geolocation anchor="BMAP_ANCHOR_BOTTOM_RIGHT" :showAddressBar="true" :autoLocation=&

Android 百度地图开发之一(Hello BaiDu Map)

之前也接触过百度地图的开发,但那是在网上找的案例或代码,而且是比较老的版本.打算重新学习一下百度地图的开发. 本次使用的百度地图的版本是 Android SDK v3.0.0 本篇文章主要讲述百度地图开发的准备和地图的成功显示即可. 首先建议大家先申请注册一个百度账号. 申请密钥(要想实现百度地图的相关服务必须申请密钥),网址:http://lbsyun.baidu.com/apiconsole/key 需要注意的是:应用名称不要重复或无效,安全码的输入详情请查看:http://develope

vue复选框获取值的补充

要通过vue的v-model获取选中复选框的值,可以用遍历对象的方式获取,代码如下: 1 <!DOCTYPE html> 2 <html xmlns="http://www.w3.org/1999/xhtml"> 3 <head> 4 <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> 5 <title>

提高Baidu Map聚合的效率

百度的MAP的例子里提供了一个聚合效果,地址是http://developer.baidu.com/map/jsdemo.htm#c1_4 ,效果图如下图: 这个效果很赞,但效率很低,当数据量达到5000的时候就难以忍受了,加载和地图缩放都很卡,用户体验很差劲.官方提供的MarkerClusterer.js 文件是这样的: /** * @fileoverview MarkerClusterer标记聚合器用来解决加载大量点要素到地图上产生覆盖现象的问题,并提高性能. * 主入口类是<a href=

[Baidu Map]MarkerManager扩展

关键代码: /// <reference path="MarkerManager.js" /> BMapLib.MarkerManager.prototype.count = function () { /// <summary> /// 获取MarkerManager管理标记数量 /// </summary> /// <returns type="">管理标记数量</returns> return thi

IT小鲜肉 Widgets Tree 单选、多选、相关回调函数、获取选中的节点功能

写一个树控件并没有想象中的那么容易,今天又花了我一个1个多小时,主要为IT小鲜肉 Widgets Tree控件添加了 单选.多选.选择前和选择后两个回调函数.获取选中节点的功能.后面会继续努力完善这个树控件. 1.通过设置初始化时候的选项{select:true}开启单选,通过设置初始化时候的选项{select:{type:'multiple'}}开启多选 使用实例代码如下: 运行效果如下: 2.添加了onBeforeSelect回调函数,用来实现自定义选择,如果该函数返回false会中断默认的

js如何获取选中radio单选按钮的值

js如何获取选中radio单选按钮的值:radio单选按钮在是非常常用的表单元素之一,经常需要获取被选中按钮的value属性值,下面就通过实例简单介绍一下如何使用javascript实现此功能,代码实例如下: <!DOCTYPE html> <html> <head> <meta charset=" utf-8"> <meta name="author" content="http://www.soft

JQuery判断radio是否选中并获取选中值的示例代码

这篇文章主要介绍了JQuery判断radio是否选中并获取选中值的方法,代码很简单,但很实用,需要的朋友可以参考下 其他对radio操作功能,以后在添加.直接上代码,别忘记引用JQuery包 ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 <!DOCTYPE html PUBLIC "

[Baidu Map]添加全景地图

关键代码: /* 为地图添加全景 @map BMap.Map @divid 全景地图承载div @ylng,xlat 全景地图初始化经纬度 eg:panorama = addPanorama(map, 'panorama', ylng, xlat); */ function addPanorama(map, divid, ylng, xlat) { var panorama = new BMap.Panorama(divid); //全景图展示 panorama.setPosition(new