从一个坐标计算新坐标x米和y度 [英] Calculate new coordinate x meters and y degree away from one coordinate

查看:140
本文介绍了从一个坐标计算新坐标x米和y度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



如果我有一个坐标,想要得到一个新的坐标x数米之遥,在一些方向。如何做到这一点?



我正在寻找类似于

- ( CLLocationCoordinate2D)translateCoordinate:(CLLocationCoordinate2D)coordinate
translateMeters:(int)meters
translateDegrees:(double)degrees;



Thanks!

解决方案

不幸的是,API中没有提供这样的功能,所以您必须自己编写。 / p>

此网站 a>给出了一些涉及纬度/经度和JavaScript代码示例的计算。具体来说,标题为Destination point given distance and bearing from starting point的部分显示了如何计算您要求的内容。



JavaScript代码位于底部页面,这里有一个可能的方法将它转换为Objective-C:

   - (double)radiansFromDegrees:(double)degrees 
{
回报度*(M_PI / 180.0);

$ b - (double)degreesFromRadians:(double)弧度
{
返回弧度*(180.0 / M_PI);

$ b $(CLLocationCoordinate2D)coordinateFromCoord:
(CLLocationCoordinate2D)fromCoord
atDistanceKm:(double)distanceKm
atBearingDegrees:(double)bearingDegrees
{
double distanceRadians = distanceKm / 6371.0;
// 6,371 =地球半径以公里为单位
double bearingRadians = [self radiansFromDegrees:bearingDegrees];
double fromLatRadians = [self radiansFromDegrees:fromCoord.latitude];
doubleLonRadians = [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));

//将toLonRadians调整到-180到+180 ...
toLonRadians = fmod((toLonRadians + 3 * M_PI),(2 * M_PI)) - M_PI;

CLLocationCoordinate2D结果;
result.latitude = [self degreesFromRadians:toLatRadians];
result.longitude = [self degreesFromRadians:toLonRadians];
返回结果;
}

在JS代码中,它包含这个链接显示了更精确的计算距离大于地球四分之一的距离。



另外请注意,上面的代码接受以km为单位的距离,因此请务必在通过之前将米除以1000.0。


I must be missing somthing out in the docs, I thought this should be easy...

If I have one coordinate and want to get a new coordinate x meters away, in some direction. How do I do this?

I am looking for something like

-(CLLocationCoordinate2D) translateCoordinate:(CLLocationCoordinate2D)coordinate translateMeters:(int)meters translateDegrees:(double)degrees;

Thanks!

解决方案

Unfortunately, there's no such function provided in the API, so you'll have to write your own.

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.

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;
}

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.

Also note the above code accepts distance in km so be sure to divide meters by 1000.0 before passing.

这篇关于从一个坐标计算新坐标x米和y度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆