当前位置:首页 > 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. 注意事项

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

vue实现测距工具

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

相关文章

vue  select实现

vue select实现

Vue Select 实现方法 在 Vue 中实现下拉选择功能可以使用原生 <select> 标签或第三方库如 vue-select。以下是两种方法的详细说明: 原生 HTML Sel…

vue实现iframe

vue实现iframe

Vue 中实现 iframe 的方法 在 Vue 项目中,可以通过多种方式实现 iframe 的嵌入。以下是几种常见的方法: 使用原生 HTML iframe 标签 直接在 Vue 组件的模板中使用…

vue实现跑车

vue实现跑车

Vue 实现跑车动画效果 使用 Vue 结合 CSS 动画可以实现跑车在屏幕上移动的效果。以下是一个完整的实现方案: 创建 Vue 组件结构 <template> <div c…

vue实现详情

vue实现详情

Vue 实现详情页的常见方法 使用动态路由 在 Vue Router 中配置动态路由参数,通过 $route.params 获取详情 ID。示例路由配置: { path: '/detail/:i…

vue实现图片

vue实现图片

Vue 实现图片功能的方法 静态图片引入 在 Vue 项目中,可以通过 src 属性直接引入静态图片。图片通常放在 public 或 assets 目录下。 <template>…

vue实现素材

vue实现素材

Vue 实现素材的方法 使用 Vue 组件管理素材 在 Vue 项目中,可以通过组件化的方式管理素材。创建一个专门的组件来加载和显示素材,例如图片、视频或音频文件。组件可以接收素材的路径或 URL 作…