2010-07-06 2 views
5

센터 맵에서 화면의 다른 쪽까지의 거리 (킬로미터)를 알아야합니다 (그리고 줌이 변경되면 거리가 변경됩니다).지도 키트를 사용하여 미터 단위로 반지름을 계산하십시오.

나는이 기능

- (void)mapView:(MKMapView *)mapView regionDidChangeAnimated:(BOOL)animated{ 
} 

내가이 작업을 수행 할 수있는 방법 어떤 아이디어가이 기능을 구현해야합니까?

감사

답변

17

MkMapView이 centerCoordinate (CLLocationCoordinate2D) 명명 된 속성 및 지역 (MKCoordinateRegion)가 있습니다.

typedef struct { 
    CLLocationDegrees latitudeDelta; 
    CLLocationDegrees longitudeDelta; 
}MKCoordinateSpan 

당신은 centerCoordinate에 따라 또 다른 지점을 만들 수 있어야합니다, 당신은 재산이나 centerCoordinate 위도에의이 latitudeDelta을 추가하여, 가정 해 봅시다, 그리고 CLLocation의 방법을 사용하여 거리를 계산 :

을 지역은 구조체이다 이

MkMapView * mapView; // init somewhere 
MKCoordinateRegion region = mapView.region; 
CLLocationCoordinate2D centerCoordinate = mapView.centerCoordinate; 
CLLocation * newLocation = [[[CLLocation alloc] initWithLatitude:centerCoordinate.latitude+region.span.latitudeDelta longitude:centerCoordinate.longitude] autorelease]; 
CLLocation * centerLocation = [[[CLLocation alloc] initWithLatitude:centerCoordinate.latitude:longitude:centerCoordinate.longitude] autorelease]; 
CLLocationDistance distance = [centerLocation distanceFromLocation:newLocation]; // in meters 

단지 대리인이 당신이 필요로하는 특정 방법 (결정을 발사 할 때마다 계산과 같은

- (CLLocationDistance)distanceFromLocation:(const CLLocation *)location 

뭔가 : MKMapViewDelegate)

+0

이 좋은 답변입니다. – sudo

+0

대단한 답변입니다. 고마워요! – Andres

+3

정확한 반경을 얻기 위해서는'centerCoordinate.latitude + region.span.latitudeDelta'가'centerCoordinate.latitude + region.span.latitudeDelta/2 '가되어야합니다. –

0

사각형이 원이 아니기 때문에 반경을 얻을 수 없습니다.

그러나 지역의 중심에서 가장 먼 거리를 얻을 수 있습니다. 위와 동일한 개념을 재사용하지만 신속한 확장 기능에 포함됩니다.

extension MKCoordinateRegion { 
    func distanceMax() -> CLLocationDistance { 
     let furthest = CLLocation(latitude: center.latitude + (span.latitudeDelta/2), 
          longitude: center.longitude + (span.longitudeDelta/2)) 
     let centerLoc = CLLocation(latitude: center.latitude, longitude: center.longitude) 
     return centerLoc.distanceFromLocation(furthest) 
    } 
} 

사용

let radius = mapView.region.distanceMax() 
관련 문제