2013-05-12 3 views
1

위도 및 경도 값을 전달하는 방법은 CLLocation Manager에서 내 CustomView로 전달합니다. 나는 위치를 받고, 나는 내가 CGPoint latLong = {41.998035, -116.012215} 정적 툭 한 내가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); 
} 

로 좌표로보기를 분할 한의 drawRect 또 다른 UIView의 클래스가 스피 CLLocationManger와 함께의 ViewController 있습니다.

당신은 UIView의 클래스에의 ViewController 값을 전달하는 방법을 나에게 말할 수

답변

0

당신은 UIView 서브 클래스 당신은 그래서 setNeedsDisplay를 호출 한 후와 (당신의 CGPoint 또는 아마도 CLLocationCoordinate2D을 사용 중) 좌표에 대한 당신의 속성이 일반적으로 것 장치는 사용자의 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); 
} 

, 당신이 당신의보기를 업데이트 할 때, 당신은 뭔가를 할 수 따라서

는 속성이 :

내가 convertLatLongCoord 일부 결함을 해결하기 위해 노력했습니다 동안
myCustomView.coordinate = CLLocationCoordinate2DMake(41.998035, -116.012215); 

, 그것은 당 아직 아니다 foo는 -EARTH_RADIUS에서 EARTH_RADIUS까지의 값을 산출하므로 (0과 뷰의 너비/높이 사이의 값을 예상하는 UIViewCGRect은 분명히 작동하지 않음).

기본 알고리즘이 여기에서 설명한 것처럼 가정합니다 (Converting from longitude\latitude to Cartesian coordinates). 나는 도수를 cossin에 의해 필요로하는 라디안 변환으로 고정하려고 시도했지만 여전히 이것을 스케일하고 표시하려는지도/레이더의 부분에 적절한 값으로 변환해야합니다.

+0

장치가 이동함에 따라 위치를 업데이트하고 싶습니다. 그지도없이. –

+0

@ rajesh.k - 이해하지만, 'convertLatLongCoord'는 당신이 생각하는대로 달성 할 것이라고 생각하지 않습니다. 그것으로 놀아 라. 그러면 나는 당신이 의미하는 것을 볼 것이다. – Rob

+0

이것을 확인하십시오.지도가없는 보트를 찾고 싶습니다. https://play.google.com/store/apps/details?id=com.terboel.myanchorwatch –

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; 
    } 
} 
관련 문제