当前位置:首页 > 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(以高德地图为例)

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

vue实现测距工具

安装依赖

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公式
    纯前端计算距离时使用以下公式:

    vue实现测距工具

    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. 注意事项

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

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

相关文章

实现vue

实现vue

安装Vue.js 通过npm安装Vue.js是最常见的方式。确保已安装Node.js和npm,运行以下命令: npm install vue 或者使用CDN直接引入: <script src…

vue实现流程

vue实现流程

Vue 实现流程的基本步骤 安装 Vue.js 可以通过 CDN 引入或使用 npm/yarn 安装。 CDN 方式: <script src="https://cdn.jsdelivr.…

vue实现portal

vue实现portal

Vue 实现 Portal 功能 Portal 是一种将子节点渲染到父组件 DOM 层级之外的 DOM 节点的技术,常用于实现模态框、弹出层等需要脱离当前组件层级的场景。Vue 可以通过多种方式实现…

vue实现 单选

vue实现 单选

实现 Vue 单选功能 在 Vue 中实现单选功能可以通过多种方式完成,以下是几种常见的方法: 使用 v-model 绑定单选按钮 通过 v-model 可以轻松实现单选功能,将单选按钮的值绑定到同…

vue核心实现

vue核心实现

Vue 核心实现原理 Vue.js 的核心实现主要包括响应式系统、虚拟 DOM、模板编译和组件化机制。以下是关键实现原理的详细说明: 响应式系统 Vue 使用 Object.defineProper…

vue实现评分

vue实现评分

Vue 实现评分功能 使用组件库实现 安装 element-ui 或 ant-design-vue 这类 UI 库,它们已经内置了评分组件。 以 element-ui 为例: <templa…