Calculate the actual distance between two longitude and latitude
Usage scenarios
This example describes how to calculate the actual distance between two longitude and latitude in YIDA.
Implement functions
Create Page
Calculate distance function
// 根据经纬度计算距离,参数分别为起点的纬度,经度;终的纬度,经度
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; /* 经纬度转换成三角函数中度分表形式。 */
}
Compute button binding event
// 计算距离
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));
}
Effect
Try it online
This doc is generated using machine translation. Any discrepancies or differences created in the translation are not binding and have no legal effect for compliance or enforcement purposes.
本文档对您是否有帮助?