2012-05-25 9 views
0

주석과의 거리를 계산하려하지만 어떤 이유로 주석 좌표를 얻을 수 없습니다. 내가 plist에서 특수 효과를로드합니다.주석과의 거리를 계산할 수 없습니다.

CLLocationCoordinate2D annocoord = myAnnotation.coordinate; 
     CLLocationCoordinate2D usercoord = self.mapView.userLocation.coordinate; 

     NSLog(@"ANNO = %f, %f", annocoord.latitude, annocoord.longitude); 
     NSLog(@"USER = %f, %f", usercoord.latitude, usercoord.longitude); 

     CLLocation *loc = [[CLLocation alloc] initWithLatitude:myAnnotation.coordinate.latitude longitude:myAnnotation.coordinate.longitude]; 
     CLLocation *loc2 = [[CLLocation alloc] initWithLatitude:self.mapView.userLocation.coordinate.latitude longitude:self.mapView.userLocation.coordinate.longitude]; 

     NSLog(@"LOC = %f, %f", loc.coordinate.latitude, loc.coordinate.longitude); 
     NSLog(@"LOC2 = %f, %f", loc2.coordinate.latitude, loc2.coordinate.longitude); 

     CLLocationDistance dist = [loc distanceFromLocation:loc2]; 

     NSLog(@"DIST: %f", dist); 

로그 :

ANNO = 0.000000, 0.000000 
USER = 60.492631, 22.258933 
LOC = 0.000000, 0.000000 
LOC2 = 60.492631, 22.258933 
DIST: 6999960.693412 

답변

0
-(CGFloat)distanceBetweenTwoPoints:(CGPoint)firstP secondPoint:(CGPoint)secondP{ 

    CGFloat distance = sqrtf(powf((secondP.y - firstP.y), 2.0) + powf((secondP.x - firstP.x), 2.0)); 

    return distance; 

} 

사용이 코드는 두 점 사이의 거리를 찾을 수 있습니다.

+0

이 방법은 장거리 용으로 만 사용할 수 있습니다. 장거리 전화로 작업하는 경우 2D 평면에서 작업 할 때 지구 원형도에 대처할 수 있도록 좀 더 복잡한 수식 ('distanceFromLocation'에서 구현)을 사용해야합니다 . – CitronEvanescent

+0

확인. 조언 주셔서 감사합니다. –

관련 문제