当前位置:首页 > VUE

vue实现测距工具

2026-02-18 03:21:19VUE

实现思路

基于Vue实现测距工具的核心在于利用浏览器的地理定位API(如Geolocation API)或地图服务API(如高德、百度、Google Maps)计算两点之间的距离。以下是两种常见实现方式:

方法一:纯前端实现(基于Geolocation API)

适用于不需要地图展示,仅需计算两点距离的场景。

安装依赖

npm install vue geolocation-api

代码实现

<template>
  <div>
    <button @click="getLocation">获取当前位置</button>
    <div v-if="points.length > 0">
      <p>点1: {{ points[0] }}</p>
      <p v-if="points.length > 1">点2: {{ points[1] }} </p>
      <p v-if="points.length > 1">距离: {{ distance }} 米</p>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      points: [],
      distance: 0
    };
  },
  methods: {
    getLocation() {
      if (navigator.geolocation) {
        navigator.geolocation.getCurrentPosition(
          position => {
            const { latitude, longitude } = position.coords;
            this.points.push({ lat: latitude, lng: longitude });
            if (this.points.length === 2) {
              this.calculateDistance();
            }
          },
          error => console.error(error)
        );
      }
    },
    calculateDistance() {
      const [point1, point2] = this.points;
      const R = 6371e3; // 地球半径(米)
      const φ1 = point1.lat * Math.PI / 180;
      const φ2 = point2.lat * Math.PI / 180;
      const Δφ = (point2.lat - point1.lat) * Math.PI / 180;
      const Δλ = (point2.lng - point1.lng) * Math.PI / 180;

      const a = Math.sin(Δφ / 2) * Math.sin(Δφ / 2) +
                Math.cos(φ1) * Math.cos(φ2) *
                Math.sin(Δλ / 2) * Math.sin(Δλ / 2);
      const c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
      this.distance = Math.round(R * c);
    }
  }
};
</script>

方法二:基于地图API(以高德地图为例)

适用于需要地图交互的场景,支持可视化测距。

安装依赖

npm install @amap/amap-jsapi-loader

代码实现

<template>
  <div>
    <div id="map-container" style="width: 100%; height: 400px"></div>
    <p v-if="distance">距离: {{ distance }} 米</p>
  </div>
</template>

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

export default {
  data() {
    return {
      map: null,
      markers: [],
      distance: 0
    };
  },
  mounted() {
    this.initMap();
  },
  methods: {
    initMap() {
      AMapLoader.load({
        key: '你的高德地图Key',
        version: '2.0',
        plugins: ['AMap.RangingTool']
      }).then(AMap => {
        this.map = new AMap.Map('map-container');
        this.addRangingTool(AMap);
      });
    },
    addRangingTool(AMap) {
      const rangingTool = new AMap.RangingTool(this.map);
      rangingTool.on('end', e => {
        this.distance = e.ruler.distance;
        this.markers = e.ruler.points.map(point => {
          return new AMap.Marker({ position: point });
        });
        this.map.add(this.markers);
      });
      rangingTool.turnOn();
    }
  }
};
</script>

关键说明

  1. Haversine公式
    纯前端计算距离时使用以下公式:

    a = sin²(Δφ/2) + cosφ1 * cosφ2 * sin²(Δλ/2)
    c = 2 * atan2(√a, √(1−a))
    distance = R * c

    其中 R 为地球半径(6371e3米)。

  2. 地图API选择

    • 高德/百度地图适合国内项目,需申请开发者Key。
    • Google Maps需处理网络访问问题。
  3. 注意事项

    vue实现测距工具

    • 浏览器定位需用户授权。
    • 移动端建议使用地图API(精度更高)。
    • 复杂路径测距需调用API的路径规划功能。

标签: 工具vue
分享给朋友:

相关文章

vue nexttick实现

vue nexttick实现

Vue 中 nextTick 的实现原理 Vue 的 nextTick 是一个用于在下次 DOM 更新循环结束之后执行延迟回调的方法。它的核心实现依赖于 JavaScript 的事件循环机制。…

vue实现日历

vue实现日历

Vue 实现日历的基本方法 使用 Vue 实现日历可以通过自定义组件或借助第三方库完成。以下是两种常见实现方式: 自定义日历组件 创建基于 Vue 的日历组件需要处理日期计算和渲染逻辑。核心是生成当…

vue 分页 实现

vue 分页 实现

Vue 分页实现方法 使用第三方库(如 Element UI) Element UI 提供了现成的分页组件 el-pagination,适合快速集成。 安装 Element UI: npm ins…

vue实现侧导航

vue实现侧导航

Vue 实现侧边导航 使用 Vue 实现侧边导航可以通过多种方式完成,以下是一种常见的实现方法,结合 Vue Router 和动态组件。 基础结构 创建侧边导航栏的基本结构,通常使用 <ul&…

vue  实现关联属性

vue 实现关联属性

Vue 实现关联属性 在 Vue 中实现关联属性通常涉及计算属性(computed properties)或侦听器(watchers),用于根据其他属性的变化动态更新目标属性。 使用计算属性 计算属…

vue实现流程转化

vue实现流程转化

Vue 实现流程转化 在 Vue 中实现流程转化通常涉及多个步骤,包括状态管理、组件通信和动态渲染。以下是几种常见的实现方法: 使用 Vuex 进行状态管理 Vuex 是 Vue 的官方状态管理库,…