找到地图上两点之间距离一个点特定距离的点

问题描述 投票:0回答:1

我一直在从事一个测绘项目,遇到了一个问题,我需要在地图上的两个给定点之间精确找到一个点,并且距其中一个点有特定的距离。我尝试过各种方法,但一直未能达到预期的效果。

我研究并找到了计算地球表面两点之间距离的公式,并进行了相应的调整。现在,我尝试使用这个公式在地图上找到第三个点,该点恰好位于两个给定点之间,并且距其中一个点有特定的距离。

这是我迄今为止尝试过的:

// Variable declare
let start_get_lat = 31.5387761;
let start_get_lng = 72.9696664;
let end_get_lat = 31.569235;
let end_get_lng = 72.983557;
let center = {lat: Number(start_get_lat), lng: Number(start_get_lng)};
let endLocation = {lat: Number(end_get_lat), lng: Number(end_get_lng)};

计算两点之间的距离并且

// calculate distance function 
const distance = calculateDistance(lat1, lon1, centerLat, centerLon);

获取点bwtween功能及用法

function getPointBetween(startLat, startLng, endLat, endLng, distance) {
    const earthRadius = 6371000; // Earth's radius in meters

    // Convert latitudes and longitudes from degrees to radians
    const lat1 = startLat * Math.PI / 180;
    const lon1 = startLng * Math.PI / 180;
    const lat2 = endLat * Math.PI / 180;
    const lon2 = endLng * Math.PI / 180;

    // Calculate the angular distance between the points
    const delta = distance / earthRadius;

    // Calculate the intermediate point
    const A = Math.sin((1 - delta) * Math.PI / 2) + Math.sin(delta * Math.PI / 2) * Math.cos(lat1) * Math.cos(lat2);
    const B = Math.sin(delta * Math.PI / 2) * Math.sin(lon2 - lon1) * Math.cos(lat2);
    const C = Math.atan2(B, A);
    const lat3 = Math.asin(A * Math.sin(lat1) + B * Math.sin(lat2)) * 180 / Math.PI;
    const lon3 = (lon1 + Math.atan2(Math.sin(lon2 - lon1) * Math.cos(lat2), Math.cos(lat1) * Math.sin(lat2) + Math.sin(lat1) * Math.cos(lat2) * Math.cos(lon2 - lon1))) * 180 / Math.PI;

    return { lat: lat3, lng: lon3 };
}

let anyDistance = distance - 1000;
const pointBetween = getPointBetween(start_get_lat, start_get_lng, end_get_lat, end_get_lng, anyDistance);
let bwMarker = new google.maps.Marker({
    position: pointBetween,
    map: map,
    title: "Center",
    fillColor: 'blue', //
});
console.log(pointBetween);

但是,虽然此函数返回两个坐标之间的点,但它不能保证该点恰好位于它们之间指定距离的线段上。目标是实现从给定点之一的精确位移。

我正在寻找见解或替代手动计算方法,以在不使用谷歌地图的情况下实现两个坐标之间的精确位移,因为我需要一个可以在前端和后端环境中实现的解决方案。

如有任何建议或建议,我们将不胜感激。感谢您的时间和帮助!

javascript google-maps deployment google-maps-api-3
1个回答
0
投票

Google 地图上传播点的解决方案

经过一些实验,我找到了一个简单有效的解决方案,可以将 Google 地图上的一个点传播到另一个点。这是我想出的 JavaScript 函数:

function propagatePoint(latlng1, latlng2, distanceToPropagate, totalDistanceBwPoints) {
    let ratio = distanceToPropagate / totalDistanceBwPoints;
    let newLat = latlng1[0] + (latlng2[0] - latlng1[0]) * ratio;
    let newLng = latlng1[1] + (latlng2[1] - latlng1[1]) * ratio;
    return { lat: newLat, lng: newLng };
}

我还提高了公式的精度以提高准确性:

const totalDistanceBwPoints = calculateDistance(latlng1[0], latlng1[1], latlng2[0], latlng2[1]);

function calculateDistance(lat1, lon1, lat2, lon2) {
    const earthRadius = 6371; // Radius of the Earth in kilometers
    const dLat = toRadians(lat2 - lat1);
    const dLon = toRadians(lon2 - lon1);
    const a = Math.sin(dLat / 2) * Math.sin(dLat / 2) +
        Math.cos(toRadians(lat1)) * Math.cos(toRadians(lat2)) *
        Math.sin(dLon / 2) * Math.sin(dLon / 2);
    const c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
    const distance = earthRadius * c; // Distance in kilometers

    // Calculate the percentage
    const percentage = (distance * 0.11202322513989) / 100; // 0.1121% precision percentage

    // Add the percentage to the distance
    const distanceWithPercentage = distance + percentage;

    return (distanceWithPercentage * 1000).toFixed(4);
}

此函数获取两个点(

latlng1
latlng2
)的纬度和经度坐标,以及传播距离
distanceToPropagate
以及点
totalDistanceBwPoints
之间的总距离。然后它计算传播点的新坐标。

我已经测试了这个函数,它非常适合我的用例,提供了几乎 99% 的准确结果。感谢大家的关注和支持!

© www.soinside.com 2019 - 2024. All rights reserved.