使用uniapp写圆形的电子围栏,判断自己标的点是否在画的圆圈里面。

<template>
  <view>
    <map style="width: 100%;height: 500px;" :longitude="longitude" :latitude="latitude" :markers="markers" :circles="circles" @updated="checkInFence"></map>
  </view>
</template>

<script>
export default {
  data() {
    return {
      longitude: 115.900159, // 当前位置经度(请替换为您实际的经度)
      latitude: 28.689578, // 当前位置纬度(请替换为您实际的纬度)
      fenceLongitude: 115.900159, // 电子围栏中心经度(请替换为您实际需要的经度)
      fenceLatitude: 28.689578, // 电子围栏中心纬度(请替换为您实际需要的纬度)
      fenceRadius: 100, // 电子围栏半径(单位:米)
      markers: [], // 地图上的标记点
      circles: [], // 地图上的圆形围栏
    };
  },
  mounted() {
    this.createFence();
  },
  methods: {
    // 创建电子围栏
    createFence() {
      this.markers.push({
        id: 1,
        longitude: this.fenceLongitude,
        latitude: this.fenceLatitude,
        title: '电子围栏中心'
      });
      this.circles.push({
        longitude: this.fenceLongitude,
        latitude: this.fenceLatitude,
        radius: this.fenceRadius,
        color: '#FF0000',
        fillColor: 'rgba(255, 0, 0, 0.2)'
      });
    },
    // 判断当前位置是否在电子围栏内
    checkInFence(e) {
		console.log(123,e);
      const { longitude, latitude } = e.detail;
		
      const distance = this.getDistance(this.fenceLongitude, this.fenceLatitude, 115.900159, 28.689578);//现在写的是标记的点和圆点一致
	  console.log(distance,this.fenceRadius,444444);
      if (distance <= this.fenceRadius) {
		  console.log('已越过危险区域');
        uni.showToast({
          title: '已越过危险区域',
          icon: 'none'
        });
      }
    },
    // 计算两点间的距离
    getDistance(lng1, lat1, lng2, lat2) {
		console.log(lng1, lat1, lng2, lat2,'lng1, lat1, lng2, lat2');
      const rad = Math.PI / 180.0;
      const latDiff = lat1 - lat2;
      const lngDiff = lng1 - lng2;
      const distance = Math.sqrt(Math.pow(latDiff * 111000, 2) + Math.pow(lngDiff * 111000 * Math.cos(lat1 * rad), 2));
	  console.log(distance,'distance');
      return distance;
    },
  },
};
</script>

 使用map需要一些准备工作,需要自行百度

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要实现这个功能,你需要先获取用户的地理位置信息,并且在地图上绘制电子围栏使用腾讯地图 API 可以轻松实现这一步骤。接下来,你可以使用 JavaScript 中的 `isPointInPath()` 方法检查用户的位置是否电子围栏内。 具体实现步骤如下: 1. 在小程序页面中引入腾讯地图 API,如下所示: ```javascript var QQMapWX = require('../../libs/qqmap-wx-jssdk.js'); var qqmapsdk; ``` 2. 初始化地图 API,如下所示: ```javascript Page({ data: { latitude: 0, longitude: 0, markers: [], polygon: [] }, onLoad: function () { // 初始化地图 API qqmapsdk = new QQMapWX({ key: '你的腾讯地图 API Key' }); } }); ``` 3. 获取用户的地理位置信息,并且绘制地图和电子围栏,如下所示: ```javascript Page({ data: { latitude: 0, longitude: 0, markers: [], polygon: [] }, onLoad: function () { // 初始化地图 API qqmapsdk = new QQMapWX({ key: '你的腾讯地图 API Key' }); // 获取用户的地理位置信息 wx.getLocation({ type: 'gcj02', success: res => { const latitude = res.latitude; const longitude = res.longitude; // 在地图上显示用户的位置 const markers = [{ id: 0, latitude: latitude, longitude: longitude, iconPath: '/images/location.png', width: 50, height: 50 }]; this.setData({ latitude: latitude, longitude: longitude, markers: markers }); // 绘制电子围栏 qqmapsdk.reverseGeocoder({ location: { latitude: latitude, longitude: longitude }, success: res => { const city = res.result.ad_info.city; const polygon = [{ points: [ { latitude: 39.984702, longitude: 116.318417 }, { latitude: 39.984683, longitude: 116.321956 }, { latitude: 39.981026, longitude: 116.322020 }, { latitude: 39.981012, longitude: 116.318385 } ], strokeColor: "#FF0000", fillColor: "#FF0000", strokeWidth: 2, zIndex: -1 }]; this.setData({ polygon: polygon }); }, fail: function(res) { wx.showToast({ title: '获取地理位置失败', icon: 'none' }) } }); }, fail: function(res) { wx.showToast({ title: '获取地理位置失败', icon: 'none' }) } }) } }); ``` 在上面的代码中,我们使用 `wx.getLocation()` 方法获取用户的地理位置信息。然后,在地图上显示用户的位置,并且绘制一个四边形作为电子围栏。 4. 检查用户的位置是否电子围栏内,并且报警,如下所示: ```javascript Page({ data: { latitude: 0, longitude: 0, markers: [], polygon: [] }, onLoad: function () { // 初始化地图 API qqmapsdk = new QQMapWX({ key: '你的腾讯地图 API Key' }); // 获取用户的地理位置信息 wx.getLocation({ type: 'gcj02', success: res => { const latitude = res.latitude; const longitude = res.longitude; // 在地图上显示用户的位置 const markers = [{ id: 0, latitude: latitude, longitude: longitude, iconPath: '/images/location.png', width: 50, height: 50 }]; this.setData({ latitude: latitude, longitude: longitude, markers: markers }); // 绘制电子围栏 qqmapsdk.reverseGeocoder({ location: { latitude: latitude, longitude: longitude }, success: res => { const city = res.result.ad_info.city; const polygon = [{ points: [ { latitude: 39.984702, longitude: 116.318417 }, { latitude: 39.984683, longitude: 116.321956 }, { latitude: 39.981026, longitude: 116.322020 }, { latitude: 39.981012, longitude: 116.318385 } ], strokeColor: "#FF0000", fillColor: "#FF0000", strokeWidth: 2, zIndex: -1 }]; this.setData({ polygon: polygon }); // 检查用户的位置是否电子围栏内 const context = wx.createCanvasContext('canvas'); context.beginPath(); context.moveTo(polygon[0].points[0].latitude, polygon[0].points[0].longitude); for (let i = 1; i < polygon[0].points.length; i++) { context.lineTo(polygon[0].points[i].latitude, polygon[0].points[i].longitude); } context.closePath(); context.stroke(); context.fill(); context.beginPath(); context.arc(latitude, longitude, 5, 0, 2 * Math.PI); context.closePath(); context.stroke(); context.fill(); context.draw(false, () => { wx.canvasGetImageData({ canvasId: 'canvas', x: 0, y: 0, width: 1, height: 1, success: res => { const data = res.data; if (data[0] === 255 && data[1] === 0 && data[2] === 0) { // 用户的位置在电子围栏内 console.log('用户的位置在电子围栏内'); } else { // 用户的位置不在电子围栏内,报警 console.log('用户的位置不在电子围栏内,报警'); } }, fail: function(res) { wx.showToast({ title: '获取像素失败', icon: 'none' }) } }) }); }, fail: function(res) { wx.showToast({ title: '获取地理位置失败', icon: 'none' }) } }); }, fail: function(res) { wx.showToast({ title: '获取地理位置失败', icon: 'none' }) } }) } }); ``` 在上面的代码中,我们使用 `wx.createCanvasContext()` 方法创建一个布,然后在布上绘制电子围栏和用户的位置,最后使用 `wx.canvasGetImageData()` 方法获取布上某个像素的颜色值。如果这个像素的颜色值是红色,说明用户的位置在电子围栏内,否则用户的位置不在电子围栏内,需要报警。 以上就是在微信小程序里面用腾讯地图一个电子围栏,并且判断本人坐标是否电子围栏里面,如果没有则报警的实现方法。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值