问题
I got a center coordinate and a distance/radius, I need to get N number of coordinates from center using radius, for example how to get 12 coordinates of red dots in following image.
回答1:
you can use the Great-circle_distance calculation to get the points.
I first calculate the bearings from the center point needed then foreach loop them and pass them to the function.
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);
}
In theroy and depending on how accurate you need it to be, you only need to calculate half the values using the function and then you should be able to calculate the other half with basic calculation.
But if the distance is large (don't know exactly what large means) it may make a difference.
https://3v4l.org/4fI7F
回答2:
Swift equivalent of @Andreas's answer:
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)
}
来源:https://stackoverflow.com/questions/50133937/how-to-get-n-number-of-coordinates-around-center-coordinate-and-radius