2013-05-12 19 views
1

如何將緯度和經度值從CLLocation Manager傳遞到我的CustomView。我有一個視圖控制器隨着CLLocationManger其中IAM獲得位置,我有另一個UIView類用的drawRect在那裏我有分爲觀與座標如何從CLLocationManager傳遞位置

#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經緯度= {41.998035,-116.012215}靜。

你能說我如何傳遞一個視圖控制器值到UIView類

回答

0

你通常會有一個屬性在您的座標(使用您的CGPoint或者一個CLLocationCoordinate2D),然後調用setNeedsDisplay使UIView子類該設備知道它需要調用您的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_RADIUSEARTH_RADIUS(其顯然不適用於UIViewCGRect,其期望在零與視圖的寬度/高度之間的值)。

我假設底層算法是這樣討論的:Converting from longitude\latitude to Cartesian coordinates。我試圖修復cossin所需的弧度轉換度,您仍然需要對其進行縮放並將其轉換爲適合您要顯示的地圖/雷達部分的值。

+0

我想更新設備移動的位置。它沒有地圖。 – 2013-05-12 14:40:46

+0

@ rajesh.k - 我明白了,但我不認爲'convertLatLongCoord'會達到您認爲的任何效果。玩弄它,你會明白我的意思。 – Rob 2013-05-12 14:43:54

+0

選中此項,我想找到沒有地圖的船。 https://play.google.com/store/apps/details?id=com.terboel.myanchorwatch – 2013-05-12 14:55:48

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