在vue中使用高德地图点击打点,搜索打点,高德地图组件封装

一。安装高德地图

npm install @amap/amap-jsapi-loader --save

二、在index.html文件中引入高德地图JavaScript API:

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Vue 2 AMap Demo</title>
  <script src="https://webapi.amap.com/maps?v=2.0&key=你的key"></script>
</head>
<body>
  <div id="app"></div>
  <script src="/dist/build.js"></script>
</body>
</html>`在这里插入代码片`

三、创建Vue组件,AMapMarker.vue

<template>
  <div class="map-wrap" :style="{ width: width, height: height }">
    <el-form
      :model="formData"
      :rules="rules"
      :disabled="disabled"
      class="mapFormData"
      ref="mapFormData"
    >
      <el-form-item label="关键词" prop="address" label-width="80px">
        <el-input
          v-model="formData.address"
          id="tipinput"
          placeholder="请输入关键词"
          clearable
        ></el-input>
      </el-form-item>
    </el-form>
    <div id="container" class="container map"></div>
  </div>
</template>

<script>
import AMapLoader from '@amap/amap-jsapi-loader'

export default {
  props: {
    //地图宽度
    width: {
      type: String,
      default: '100%',
    },
    //地图高度
    height: {
      type: String,
      default: '500px',
    },
    /**
     * 默认显示的地图对象信息
     * 地址 经度 纬度
     *  { address: '', lat: '',lng: '',}
     */
    defaultMapData: {
      type: Object,
      default: () => new Object(),
    },

    //禁用
    disabled: {
      type: Boolean,
      default: false,
    },
  },
  data() {
    return {
      mapInfo: null,
      marker: null,
      searchValue: '',
      formData: {
        address: '',
        lat: '',
        lng: '',
      },
      rules: {},
    }
  },

  watch: {
    defaultMapData: {//默认地图显示的点位
      handler(newV) {
        if ((newV && JSON.stringify != '{}') || newV.lat) {
          this.formData = {...newV} //
          this.initMap()
        } else {
          this.formData = {
            address: '',
            lat: '',
            lng: '',
          }
        }
      },
      deep: true,
      immediate: true,
    },
    disabled: { //地图是否禁用打点
      handler(newV) {
        if (newV) {
          this.setMapEvent(true)
        } else {
          this.setMapEvent(false)
        }
      },
      immediate: true,
    },
  },
  methods: {
    initMap() {
     
      AMapLoader.load({
        key: '你的key',
        version: '2.0',
        plugins: ['AMap.Geocoder', 'AMap.Geolocation', 'AMap.CitySearch'],
        resizeEnable: true,
      })
        .then((AMap) => {
          this.mapInfo = new AMap.Map('container', {
            resizeEnable: true,
            zoom: 16,//缩放等级
            center:[116.397428, 39.90923],//中心点
          })

          if (this.defaultMapData.lat && this.defaultMapData.lng) {//父组件传过来的点 默认点位打点
            this.marker && this.mapInfo.remove(this.marker)
            this.marker = new AMap.Marker({
              position: new AMap.LngLat(this.defaultMapData.lng, this.defaultMapData.lat),
            })
            this.mapInfo.add(this.marker)
            this.setMapCenter(this.defaultMapData.lng, this.defaultMapData.lat)
          }

          const autoOptions = {
            input: 'tipinput',
          }
          AMap.plugin(['AMap.AutoComplete'], () => { //搜索
            const auto = new AMap.AutoComplete(autoOptions)
            auto.on('select', this.adrSelect) //
          })

          this.setMapEvent(this.disabled)
        })
        .catch((e) => {
          //加载错误提示
        })
    },
    setMapCenter(lng, lat) { 
      this.mapInfo.setZoomAndCenter(16, [lng, lat])
    },
    adrSelect(e) {//搜索
      this.setMapCenter(e.poi.location.lng, e.poi.location.lat)
      this.marker && this.mapInfo.remove(this.marker)
      this.marker = new AMap.Marker({
        position: new AMap.LngLat(e.poi.location.lng, e.poi.location.lat),
      })
      this.setFormData( e.poi.location.lng, e.poi.location.lat,e.poi.name)
      this.emitMapData(e.poi.name, e.poi.location.lng, e.poi.location.lat)
      this.mapInfo.add(this.marker)
    },
    addMarker(e) {//添加点位
      this.marker && this.mapInfo.remove(this.marker)
      this.marker = new AMap.Marker({
        position: new AMap.LngLat(e.lnglat.lng, e.lnglat.lat),
      })

      this.setFormData(e.lnglat.lng, e.lnglat.lat, this.formData.address)
      this.emitMapData(this.formData.address, e.lnglat.lng, e.lnglat.lat)

      this.mapInfo.add(this.marker)

      const geocoder = new AMap.Geocoder({
        radius: 1000,
        extensions: 'all',
      })

      geocoder.getAddress([e.lnglat.lng, e.lnglat.lat], (status, result) => {//转换地址
        if (status === 'complete' && result.regeocode) {
          this.$set(this.formData, 'address', result.regeocode.formattedAddress)
        } else {
        }
      })
      this.setMapCenter(e.lnglat.lng, e.lnglat.lat)
    },
    setFormData(lng, lat, address) {
      this.$set(this.formData, 'address', address)
      this.$set(this.formData, 'lng', lng)
      this.$set(this.formData, 'lat', lat)
    },
    emitMapData(address = this.formData.address, lng = this.formData.lng, lat = this.formData.lat) {  //将已选择的点传给父组件
      this.$emit('getMapData', {
        address,
        lng,
        lat,
      })
    },
    setMapEvent(flg) {
      this.$nextTick(() => {
        if (flg) {
          this.mapInfo && this.mapInfo.off('click', this.addMarker)
        } else {
          this.mapInfo && this.mapInfo.on('click', this.addMarker)
        }
      })
    },
  },
  destroyed() {},
}
</script>

<style lang="less" scoped>
.map-wrap {
  width: 100%;
  height: 500px;
  position: relative;
  .mapFormData {
    position: absolute;
    width: 100%;
    top: 18px;
    left: 0;
    z-index: 99;
    ::v-deep .el-input__inner {
      width: 70%;
    }
  }
  .map {
    width: 100%;
    height: 100%;
  }
  .row {
    margin-top: 10px;
    display: flex;
  }
  .mr20 {
    margin-right: 20px;
  }
}
</style>

四、在需要使用组件的地方引入并使用AMapMarker组件

<template>
  <div :style='width:500px;height:500px;'>
    <AMapMarker @getMapData='getMapData' :defaultMapData='defaultMapData' />
  </div>
</template>

<script>
import AMapMarker from './AMapMarker.vue';

export default {
  components: {
    AMapMarker 
  },
  data(){
  	return {
  		defaultMapData:{},//默认点位信息为空
	}
  },
  methods:{
  getMapData(mapInfo){
  console.log('获取到的地图地址经纬度信息:',mapInfo)
  }
}
};
</script>
  • 7
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
要实现vue集成高德地图实现考勤打卡,首先需要在vue项目引入高德地图API。接下来,我们可以使用高德地图的地图组件来展示地图,并将地图与用户信息进行关联。 首先,我们需要在项目安装高德地图JavaScript API,并在项目引入相关的库文件。 在vue组件,我们可以通过创建地图容器元素来展示地图,然后使用高德地图的Map类来初始化地图。可以设置地图的心点、缩放级别、控件等属性。 为了实现考勤打卡功能,我们需要在地图上添加打卡点。可以使用标记(Marker)来表示打卡点,并给每个打卡点添加点击事件。 当用户点击地图上的打卡点时,可以弹出打卡窗口,显示用户的相关信息,例如姓名、工号等。可以通过自定义窗体(InfoWindow)来实现。 另外,为了保证用户只能在指定的区域进行打卡,可以使用高德地图的多边形(Polygon)工具来标记可打卡区域。在每次打卡时,可以使用高德地图的点位检索(PlaceSearch)功能来判断用户当前位置是否在可打卡区域内。 当用户点击打卡按钮时,可以触发相关的逻辑代码,例如获取用户位置信息、判断用户位置是否在可打卡区域内等。根据打卡结果,可以将相关信息保存到数据库,并给用户显示打卡成功或失败的提示。 总结起来,通过vue集成高德地图实现考勤打卡,我们可以使用高德地图的API来展示地图、添加打卡点、设置打卡区域等功能。通过与用户信息和数据库的交互,可以实现考勤打卡的功能需求。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

葫芦娃y

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值