1

如何将纬度和经度值从 CLLocation Manager 传递到 My CustomView。我有一个带有 CLLocationManger 的 ViewController,我在其中获取位置,我有另一个带有 drawRect 的 UIView 类,我将带有坐标的视图划分为

#define EARTH_RADIUS 6371

- (void)drawRect:(CGRect)rect
{
    CGPoint latLong = {41.998035, -116.012215};

    CGPoint newCoord = [self convertLatLongCoord:latLong];

    NSLog(@"Cartesian Coordinate: (%f, %f)",newCoord.x, newCoord.y);

    //Draw dot at coordinate
    CGColorRef darkColor = [[UIColor colorWithRed:21.0/255.0
                                            green:92.0/255.0
                                             blue:136.0/255.0
                                            alpha:1.0] CGColor];
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetFillColorWithColor(context, darkColor);
    CGContextFillRect(context, CGRectMake(newCoord.x, newCoord.y, 100, 100));

}

-(CGPoint)convertLatLongCoord:(CGPoint)latLong
{
    CGFloat x = EARTH_RADIUS * cos(latLong.x) * cos(latLong.y);
    CGFloat y = EARTH_RADIUS * cos(latLong.x) * sin(latLong.y);

    return CGPointMake(x, y);
}

我拿了 CGPoint latLong = {41.998035, -116.012215} 静态的。

你能说我如何将 ViewController 值传递给 UIView 类吗

4

2 回答 2

0

您通常会在您的UIView子类中有一个属性用于您的坐标(使用您的CGPoint或可能是 a CLLocationCoordinate2D)然后调用setNeedsDisplay,以便设备知道它需要调用您的drawRect方法。

因此,有一个属性:

@property (nonatomic) CLLocationCoordinate2D coordinate;

然后更新视图的实现,如下所示:

- (void)setCoordinate:(CLLocationCoordinate2D)coordinate
{
    _coordinate = coordinate;

    [self setNeedsDisplay];
}

- (void)drawRect:(CGRect)rect
{
    CGPoint newCoord = [self convertLatLongCoord:self.coordinate];

    NSLog(@"Cartesian Coordinate: (%f, %f)",newCoord.x, newCoord.y);

    //Draw dot at coordinate
    CGColorRef darkColor = [[UIColor colorWithRed:21.0/255.0
                                            green:92.0/255.0
                                             blue:136.0/255.0
                                            alpha:1.0] CGColor];
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetFillColorWithColor(context, darkColor);
    CGContextSetStrokeColorWithColor(context, darkColor);
    CGContextFillRect(context, CGRectMake(newCoord.x, newCoord.y, 100.0, 100.0));
}

// I assume you're using the algorithm from here: https://stackoverflow.com/a/1185413/1271826

- (CGPoint)convertLatLongCoord:(CLLocationCoordinate2D)coordinate
{
    CGFloat x = EARTH_RADIUS * cos(coordinate.latitude * M_PI / 180.0) * cos(coordinate.longitude * M_PI / 180.0);
    CGFloat y = EARTH_RADIUS * cos(coordinate.latitude * M_PI / 180.0) * sin(coordinate.longitude * M_PI / 180.0);

    // TODO: The above calculates x and y values from -EARTH_RADIUS to EARTH_RADIUS.
    // You presumably want to scale this appropriately for your view. The
    // specifics will vary based upon your desired user interface.

    return CGPointMake(x, y);
}

然后,当您想更新视图时,可以执行以下操作:

myCustomView.coordinate = CLLocationCoordinate2DMake(41.998035, -116.012215);

虽然我试图弥补 中convertLatLongCoord的一些缺陷,但它仍然不完美,因为它会产生从-EARTH_RADIUSto范围内的值EARTH_RADIUS(这显然不适用于 aCGRect的 a UIView,它期望值在零和视图的宽度/高度之间)。

我假设底层算法就像这里讨论的那样:Converting from longitude\latitude to Cartesian coordinatescos我已经尝试修复和所需的弧度转换sin,您仍然需要对其进行缩放并将其转换为适合您打算显示的地图/雷达部分的值。

于 2013-05-12T13:49:31.290 回答
0

根据您想要“找到您的船”的评论,听起来您想要从当前位置前往指定位置(您的船)。计算是这样的

+ (float)getHeadingForDirectionFromCoordinate:(CLLocationCoordinate2D)fromLoc toCoordinate:(CLLocationCoordinate2D)toLoc
{
    float fLat = degreesToRadians(fromLoc.latitude);
    float fLng = degreesToRadians(fromLoc.longitude);
    float tLat = degreesToRadians(toLoc.latitude);
    float tLng = degreesToRadians(toLoc.longitude);

    float degree = radiansToDegrees(atan2(sin(tLng-fLng)*cos(tLat), cos(fLat)*sin(tLat)-sin(fLat)*cos(tLat)*cos(tLng-fLng)));

    if (degree >= 0) {
        return degree;
    } else {
        return 360+degree;
    }
}
于 2013-05-30T19:58:38.707 回答