绘制x-y平面中两点之间的航向

时间:2011-07-28 09:15:55

标签: iphone objective-c cllocationmanager

我正在尝试实施一个小型雷达,根据纬度和经度坐标绘制目标,类似于Layar AR iPhone应用程序中的雷达。我有罗盘和locationManager工作来获得两点之间的纬度/经度,航向和距离。但是我无法将点绘制到x-y平面上。你能指出我正确的方向(可以这么说)吗?

这是我用来绘制的方法,但结果不正确:

-(void) addTargetIndicatorWithHeading:(float)heading andDistance:(float)distance{
    //draw target indicators
    //need to convert radians and distance to cartesian coordinates
    float radius = 50;
    float x0 = 0.0; 
    float y0 = 0.0;

    //convert heading from radians to degrees
    float angle = heading * (180/M_PI);

    //x-y coordinates
    float x1 = (x0 + radius * sin(angle)); 
    float y1 = (y0 + radius * cos(angle)); 

    TargetIndicator *ti = [[TargetIndicator alloc] initWithFrame:CGRectMake(x1, y1, 5, 5)];
    [self addSubview:ti];
    [ti release];

}

2 个答案:

答案 0 :(得分:0)

我猜问题在于当前视图的原点坐标没有添加到ur坐标。 只需通过添加当前视图的origin.x和origin.y来修改x1和y1,将ti作为子视图添加到其中。

答案 1 :(得分:0)

我弄清楚出了什么问题,但我不知道背后的原因。首先,我不应该将弧度转换为度数。这给了我正确的定位,但旋转了180度。所以要修复它,我从PI中减去弧度。

以下是解决方案:

-(void) addTargetIndicatorWithHeading:(float)heading andDistance:(float)distance{
    //draw target indicators
    //need to convert radians and distance to cartesian coordinates
    float radius = 50;

    //origin offset
    float x0 = 50.0; 
    float y0 = 50.0;

    //convert heading from radians to degrees and rotate by 180 deg
    float angle = M_PI - heading; 

    float x1 = (x0 + radius * sin(angle));   
    float y1 = (y0 + radius * cos(angle)); 

    TargetIndicator *ti = [[TargetIndicator alloc] initWithFrame:CGRectMake(x1, y1, 5, 5)];
    [self addSubview:ti];
    [ti release];


}