問題描述
我一定是在文檔中遺漏了一些東西,我認(rèn)為這應(yīng)該很容易......
I must be missing somthing out in the docs, I thought this should be easy...
如果我有一個(gè)坐標(biāo)并且想要在 x 米外的某個(gè)方向上獲得一個(gè)新坐標(biāo).我該怎么做?
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)坐標(biāo)translateMeters:(int) 米translateDegrees:(double)degrees;
謝謝!
推薦答案
很遺憾,API 中沒有提供這樣的功能,所以你必須自己編寫.
Unfortunately, there's no such function provided in the API, so you'll have to write your own.
這個(gè)網(wǎng)站給出了幾個(gè)涉及緯度/經(jīng)度和樣本的計(jì)算JavaScript 代碼.具體來說,標(biāo)題為給定距離起點(diǎn)的目標(biāo)點(diǎn)和方位角"的部分顯示了如何計(jì)算您的要求.
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 代碼位于該頁面的底部,這是將其轉(zhuǎn)換為 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 代碼中,它包含 這個(gè)鏈接 顯示更準(zhǔn)確的計(jì)算距離大于地球周長的 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.
另請注意,上述代碼接受以公里為單位的距離,因此請務(wù)必在通過之前將米除以 1000.0.
Also note the above code accepts distance in km so be sure to divide meters by 1000.0 before passing.
這篇關(guān)于計(jì)算距離一個(gè)坐標(biāo)的新坐標(biāo) x 米和 y 度的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!