vue 计算我当前位置与另外一个坐标点的距离以及是否在某个区域上

<script type="text/javascript" src="@/assets/GeoUtils.js"></script>
 
const pointA = new BMap.Point(this.wodeWz.lng, this.wodeWz.lat) //我的定位
const point = new BMap.Point(this.shlng, this.shlat) 
const juli = (map.getDistance(pointA, point) / 1000).toFixed(2) //计算距离
let distance = juli * 1000
let qaz = []
this.asyncObject.forEach(item => {
    var ply = new BMap.Point(item.lng, item.lat)
    qaz.push(ply)
 })
var polygon2 = new this.BMap.Polygon(qaz)
var result = BMapLib.GeoUtils.isPointInPolygon(point, polygon2) //判断是否在多边形区域内

此方法需要引入GeoUtils.js

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
Vue 中判断一个点是否在一个区域内,可以采用类似于射线法或多边形顶点法的方法。 射线法:通过 $refs 属性获取到需要判断区域的 DOM 元素,然后获取鼠标点击事件的标,使用 Vue计算属性或方法计算标点击事件的是否区域内。具体实现方法如下: ```html <template> <div ref="area" @click="checkPoint($event)"> <!-- 区域内容 --> </div> </template> <script> export default { methods: { checkPoint(event) { const area = this.$refs.area; const rect = area.getBoundingClientRect(); const x = event.clientX - rect.left; const y = event.clientY - rect.top; const isInside = // 判断 x, y 是否区域内部 if (isInside) { // 点在区域内部 } else { // 点在区域外部 } } } } </script> ``` 多边形顶点法:同样通过 $refs 属性获取到需要判断区域的 DOM 元素,然后获取鼠标点击事件的标,使用 Vue计算属性或方法计算标点击事件的是否区域内。具体实现方法如下: ```html <template> <div ref="area" @click="checkPoint($event)"> <!-- 区域内容 --> </div> </template> <script> export default { data() { return { vertices: [ // 多边形的顶点标 {x: 100, y: 100}, {x: 200, y: 100}, {x: 200, y: 200}, {x: 100, y: 200} ] } }, methods: { checkPoint(event) { const area = this.$refs.area; const rect = area.getBoundingClientRect(); const x = event.clientX - rect.left; const y = event.clientY - rect.top; const isInside = this.isPointInsidePoly(x, y, this.vertices); if (isInside) { // 点在区域内部 } else { // 点在区域外部 } }, isPointInsidePoly(x, y, vertices) { let inside = false; const n = vertices.length; for (let i = 0, j = n - 1; i < n; j = i++) { const xi = vertices[i].x, yi = vertices[i].y; const xj = vertices[j].x, yj = vertices[j].y; const intersect = ((yi > y) != (yj > y)) && (x < (xj - xi) * (y - yi) / (yj - yi) + xi); if (intersect) inside = !inside; } return inside; } } } </script> ``` 以上是两种常见的方法,具体实现可以根据实际情况选择适合的方式。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值