如何获得围绕中心坐标和半径的N个坐标

时间:2018-05-02 11:46:48

标签: php swift google-maps mapkit coordinate-transformation

我得到了一个中心坐标和一个距离/半径,我需要使用半径从中心得到N个坐标,例如如何在下面的图像中得到12个红点坐标。

enter image description here

2 个答案:

答案 0 :(得分:2)

迅速相当于@Andreas的答案:

func destinationPoint(latitude: Double, longitude: Double, bearing: Double, dist: Double) -> CLLocationCoordinate2D {
    let lat2 = asin(sin(latitude) * cos(dist) + cos(latitude) * sin(dist) * cos(bearing))
    var lon2 = longitude + atan2(sin(bearing) * sin(dist) * cos(latitude),cos(dist) - sin(latitude) * sin(lat2))
        lon2 = fmod(lon2 + 3 * .pi, 2 * .pi) - .pi  // normalise to -180..+180º
    return CLLocationCoordinate2D(latitude: lat2 * (180.0 / .pi), longitude: lon2 * (180.0 / .pi))
}

let latRadian = coordinate.latitude * .pi / 180
let lngRadian = coordinate.longitude * .pi / 180
let distance = (radius / 1000) / 6371 // km
let n = 24

let coordinates = stride(from: 0.0, to: 360.0, by: Double(360 / n)).map {
    destinationPoint(latitude: latRadian, longitude: lngRadian, bearing: $0 * .pi / 180, dist: distance)
}

答案 1 :(得分:1)

您可以使用Great-circle_distance计算来获得积分。

我首先从所需的中心点计算轴承,然后将它们循环并将它们传递给函数。

function destinationPoint($lat, $lng, $brng, $dist) {
    $rad = 6371; // earths mean radius
    $dist = $dist/$rad;  // convert dist to angular distance in radians
    $brng = deg2rad($brng);  // conver to radians 
    $lat1 = deg2rad($lat); 
    $lon1 = deg2rad($lng);

    $lat2 = asin(sin($lat1)*cos($dist) + cos($lat1)*sin($dist)*cos($brng) );
    $lon2 = $lon1 + atan2(sin($brng)*sin($dist)*cos($lat1),cos($dist)-sin($lat1)*sin($lat2));
    $lon2 = fmod($lon2 + 3*M_PI, 2*M_PI) - M_PI;  // normalise to -180..+180º
    $lat2 = rad2deg($lat2);
    $lon2 = rad2deg($lon2);


    echo "lat = ".$lat2."\n";
    echo "lon = ".$lon2."\n\n";
}

$lat = 0;
$lng = 0;
$dist = 1; // km
$n = 12;

$bearings = range(0, 360-(360/$n)  , 360/$n); // create array of all bearings needed from $lat/$lng

foreach($bearings as $brng){
    echo $brng ."\n";
    destinationPoint($lat, $lng, $brng, $dist);
}

在理论中,根据您需要的准确程度,您只需要使用该函数计算一半的值,然后您应该能够通过基本计算来计算另一半。 但如果距离很大(不知道究竟是什么意思)它可能会产生影响。

https://3v4l.org/4fI7F