メインコンテンツまでスキップ

2つの緯度と経度の実際の距離を計算します

使用シーン

この例では、2つの緯度と経度で実際の距離を計算する方法を紹介します。

機能を実現する

ページの作成

距離関数を計算する

// 根据经纬度计算距离,参数分别为起点的纬度,经度;终的纬度,经度
export function getDistance(startLatitude, startLongitude, endLatitude, endLongitude) {
const radLat1 = this.rad(startLatitude);
const radLat2 = this.rad(endLatitude);
const a = radLat1 - radLat2;
const b = this.rad(startLongitude) - this.rad(endLongitude);
var s = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(a / 2), 2) +
Math.cos(radLat1) * Math.cos(radLat2) * Math.pow(Math.sin(b / 2), 2)));
s = s * 6378.137; // EARTH_RADIUS;
s = Math.round(s * 10000) / 10000; // 输出为公里
return s;
}

// 处理经纬度
export function rad(d) {
return (d * Math.PI) / 180.0; /* 经纬度转换成三角函数中度分表形式。 */
}

計算ボタン結合イベント

// 计算距离 
export function onGetDistance() {
this.$('locationField_lgosgdmj').validate(); // 起点校验
this.$('locationField_lgosgdmk').validate(); // 终点校验
const start = this.$('locationField_lgosgdmj').getValue(); // 起点
const end = this.$('locationField_lgosgdmk').getValue(); // 终点
if (!start || !end) {
return;
};
const startLatitude = start.latitude; // 起点纬度
const startLongitude = start.longitude; // 起点经度
const endLatitude = end.latitude; // 终点纬度
const endLongitude = end.longitude; // 终点经度
this.$('numberField_lgosgdmo').setValue(this.getDistance(startLatitude, startLongitude, endLatitude, endLongitude));
}

効果を実現する

オンラインで試遊する

Copyright © 2025钉钉(中国)信息技术有限公司和/或其关联公司浙ICP备18037475号-4