Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/44.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone 如何从CLLocationManager传递位置_Iphone_Ios - Fatal编程技术网

Iphone 如何从CLLocationManager传递位置

Iphone 如何从CLLocationManager传递位置,iphone,ios,Iphone,Ios,如何将纬度和经度值从CLLocation Manager传递到我的CustomView。我有一个带有CLLocationManger的ViewController,我在其中获取位置,我有另一个带有drawRect的UIView类,我在其中用坐标将视图分割为 #define EARTH_RADIUS 6371 - (void)drawRect:(CGRect)rect { CGPoint latLong = {41.998035, -116.012215}; CGPoint n

如何将纬度和经度值从CLLocation Manager传递到我的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类吗?通常,您的UIView子类中会有一个属性用于坐标,可以使用CGPoint,也可以使用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_RADIUS到EARTH_RADIUS的值,这显然不适用于UIView的CGRect,它期望的值介于零和视图的宽度/高度之间


我假设底层算法与这里讨论的一样:。我试着修正cos和sin所需的从度到弧度的转换,您仍然需要缩放此比例,并将其转换为适合您打算显示的地图/雷达部分的值。

根据您想要找到您的船的注释,听起来您想要从当前位置到您的船的指定位置的航向。这个计算是这样的

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

我想在设备移动时更新位置。它没有地图。@rajesh.k-明白,但我不认为convertLatLongCoord能实现你所认为的任何目标。玩玩它,你就会明白我的意思。看看这个,我想在没有地图的情况下找到这艘船。如何在UIViewOk上绘制连接位置的连续线,但您的算法存在两个问题:首先,您需要将纬度和经度转换为弧度,例如坐标。纬度*M_PI/180.0。使用以弧度为基础的cos和sin函数以及以度为单位测量的纬度和经度将不会呈现所需的结果。第二,即使这样做,也会得到从-EARTH_RADIUS到EARTH_RADIUS的值。因此,您必须将其缩放到适合您的视图的坐标。