如何获取两点折线之间的所有坐标

时间:2019-06-19 12:32:09

标签: javascript google-maps leaflet leaflet.draw

我或我怎么能找到折线上两点之间的所有坐标。 我不需要距离,也不需要中点。

假设我有两点

  1. A点(左)纬度:39.091868长:-9.263187
  2. B点(右)纬度:39.089815长:-9.261857

从A点到B点,所有坐标都位于屏幕截图下方的行上

3 个答案:

答案 0 :(得分:0)

正如我在评论中提到的那样,由于数字是无限的,不可能将所有点都放在一条线上。

但是,您可以返回固定数量的积分。这个数字在很大程度上取决于您想要达到的精度水平。

Point = function(x, y) {
  this.x = x;
  this.y = y;
}
var pointA = new Point(39.091868, -9.263187);
var pointB = new Point(39.089815, -9.261857);

var numberOfPoints = 20;
var points = new Array();

for (var i = 0; i < numberOfPoints; i++) {
  points.push(new Point((Math.abs(pointA.x - pointB.x) / 10) * i + pointB.y, (Math.abs(pointA.y - pointB.y) / numberOfPoints) * i + pointB.y));
}

console.log(points);

答案 1 :(得分:0)

是的,这是可能的。我们得到两点之间的距离,并检查距离是否在3米以下或任何值以下。结果返回true后,我们调用递归函数。 PHP代码。

global $array;
$array = array();

function recurCollectCoords($coords) {
global $array;
$coords = array(
    array(24.925274, 67.096988),
    array(24.924872, 67.097367),
    array(24.924946, 67.097481)
);
foreach ($coords as $key => $value) {
    if (isset($coords[$key + 1])) {
$distance = distance($value[0], $value[1], $coords[$key + 1][0], $coords[$key + 1] 
[1], "M");
if ($distance > 3) { //Using each coordinate collect 3 meter distance 
$coordinates = array(array($value[0], $value[1]), array($coords[$key + 1][0], 
$coords[$key + 1][1]));
$midpoint = getTwoPointMidCoord($coordinates);
$newCoord[] = array(array($value[0], $value[1]), array($midpoint[0], 
$midpoint[1]), array($coords[$key + 1][0], $coords[$key + 1][1]));
recurCollectCoords(call_user_func_array('array_merge', $newCoord));
} else {
array_push($array, array((string) $value[0], (string) $value[1]));
        }
    }
array_push($array, array((string) $value[0], (string) $value[1]));
}
$uniqueCoords = array_values(array_map("unserialize", 
array_unique(array_map("serialize", $array))));
return $uniqueCoords;
}

function distance($lat1, $lon1, $lat2, $lon2, $unit) {
if (($lat1 == $lat2) && ($lon1 == $lon2)) {
    return 0;
} else {
    $theta = $lon1 - $lon2;
    $dist = sin(deg2rad($lat1)) * sin(deg2rad($lat2)) + cos(deg2rad($lat1)) * 
cos(deg2rad($lat2)) * cos(deg2rad($theta));
    $dist = acos($dist);
    $dist = rad2deg($dist);
    $miles = $dist * 60 * 1.1515;
    $unit = strtoupper($unit);

    if ($unit == "K") {
        return ($miles * 1.609344);
    } else if ($unit == "N") {
        return ($miles * 0.8684);
    } else if ($unit == "M") {
        return ($miles * 1.609344) * 1000;
    } else {
        return $miles;
    }
}

}

function getTwoPointMidCoord($coordinates) {

$num_coords = count($coordinates);

$sumY = 0;
$sumZ = 0;
$LATIDX = 0;
$LNGIDX = 1;
$sumX = 0;
$sumY = 0;
$sumZ = 0;

foreach ($coordinates as $key => $value) {
    $lat = deg2rad($value[0]);
    $lng = deg2rad($value[1]);
    $sumX += cos($lat) * cos($lng);
    $sumY += cos($lat) * sin($lng);
    $sumZ += sin($lat);
}

$avgX = $sumX / 2;
$avgY = $sumY / 2;
$avgZ = $sumZ / 2;

$lng = atan2($avgY, $avgX);
$hyp = sqrt($avgX * $avgX + $avgY * $avgY);
$lat = atan2($avgZ, $hyp);
return ([rad2deg($lat), rad2deg($lng)]);
}

答案 2 :(得分:0)

我们获取两点之间的距离,并检查距离是否在3米以下或任何值。结果返回true后,我们调用递归函数。     函数getTwoPointMidCoord($ coordinates){

$num_coords = count($coordinates);

$sumY = 0;
$sumZ = 0;
$LATIDX = 0;
$LNGIDX = 1;
$sumX = 0;
$sumY = 0;
$sumZ = 0;

foreach ($coordinates as $key => $value) {
$lat = deg2rad($value[0]);
$lng = deg2rad($value[1]);
$sumX += cos($lat) * cos($lng);
$sumY += cos($lat) * sin($lng);
$sumZ += sin($lat);
}

$avgX = $sumX / 2;
$avgY = $sumY / 2;
$avgZ = $sumZ / 2;

$lng = atan2($avgY, $avgX);
$hyp = sqrt($avgX * $avgX + $avgY * $avgY);
$lat = atan2($avgZ, $hyp);
return ([rad2deg($lat), rad2deg($lng)]);
}