問題描述
我一定是在文檔中遺漏了一些東西,我認為這應該很容易......
I must be missing somthing out in the docs, I thought this should be easy...
如果我有一個坐標并且想要在 x 米外的某個方向上獲得一個新坐標.我該怎么做?
If I have one coordinate and want to get a new coordinate x meters away, in some direction. How do I do this?
我正在尋找類似的東西
-(CLLocationCoordinate2D) translateCoordinate:(CLLocationCoordinate2D)坐標translateMeters:(int) 米translateDegrees:(double)degrees;
謝謝!
推薦答案
很遺憾,API 中沒有提供這樣的功能,所以你必須自己編寫.
Unfortunately, there's no such function provided in the API, so you'll have to write your own.
這個網站給出了幾個涉及緯度/經度和樣本的計算JavaScript 代碼.具體來說,標題為給定距離起點的目標點和方位角"的部分顯示了如何計算您的要求.
This site gives several calculations involving latitude/longitude and sample JavaScript code. Specifically, the section titled "Destination point given distance and bearing from start point" shows how to calculate what you're asking.
JavaScript 代碼位于該頁面的底部,這是將其轉換為 Objective-C 的一種可能方法:
The JavaScript code is at the bottom of that page and here's one possible way to convert it to Objective-C:
- (double)radiansFromDegrees:(double)degrees
{
return degrees * (M_PI/180.0);
}
- (double)degreesFromRadians:(double)radians
{
return radians * (180.0/M_PI);
}
- (CLLocationCoordinate2D)coordinateFromCoord:
(CLLocationCoordinate2D)fromCoord
atDistanceKm:(double)distanceKm
atBearingDegrees:(double)bearingDegrees
{
double distanceRadians = distanceKm / 6371.0;
//6,371 = Earth's radius in km
double bearingRadians = [self radiansFromDegrees:bearingDegrees];
double fromLatRadians = [self radiansFromDegrees:fromCoord.latitude];
double fromLonRadians = [self radiansFromDegrees:fromCoord.longitude];
double toLatRadians = asin( sin(fromLatRadians) * cos(distanceRadians)
+ cos(fromLatRadians) * sin(distanceRadians) * cos(bearingRadians) );
double toLonRadians = fromLonRadians + atan2(sin(bearingRadians)
* sin(distanceRadians) * cos(fromLatRadians), cos(distanceRadians)
- sin(fromLatRadians) * sin(toLatRadians));
// adjust toLonRadians to be in the range -180 to +180...
toLonRadians = fmod((toLonRadians + 3*M_PI), (2*M_PI)) - M_PI;
CLLocationCoordinate2D result;
result.latitude = [self degreesFromRadians:toLatRadians];
result.longitude = [self degreesFromRadians:toLonRadians];
return result;
}
在 JS 代碼中,它包含 這個鏈接 顯示更準確的計算距離大于地球周長的 1/4.
In the JS code, it contains this link which shows a more accurate calculation for distances greater than 1/4 of the Earth's circumference.
另請注意,上述代碼接受以公里為單位的距離,因此請務必在通過之前將米除以 1000.0.
Also note the above code accepts distance in km so be sure to divide meters by 1000.0 before passing.
這篇關于計算距離一個坐標的新坐標 x 米和 y 度的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!