2010-03-10 3 views
5

지도 키트로 1000 피트 또는 1/2 마일 거리를 어떻게 결정합니까? 어떤 핀의 반지름 또는 두 개의 핀 사이의 거리.지도 키트를 사용하여 거리 결정

예를 들어 핀 A에지도를 중앙에 배치합니다. 핀 B, C 및 D도 핀 A에서부터 다양한 거리에서지도에 있습니다. B와 C는 A에서 1/2 마일 이내에 있지만 D는 1 마일입니다. 떨어져. B와 C가 A에서 1/2 마일 이내에 있다는 것을 알고 싶습니다. 어떻게 계산할 수 있습니까?

+0

이 더 많은 질문을 설명 할 수있다, 그래서 우리는 정확한 요구 사항을 이해할 수있다? –

+0

질문을 수정했습니다. 더 많은 정보가 필요하면 알려주세요. – user230949

답변

13

두 개의 다른 점이 "핀"이라고 언급 했으므로 MKPinAnnotationView (또는 다른 주석보기)를 사용하고 있다고 가정합니다. 그렇지 않은 경우 위치를 다른 방식으로 가져야합니다. 당신이이 위치에 대한 주석 객체에 대한 포인터가있는 경우

, 당신은 쉽게 (-initWithLatitude:longitude:를 사용하여)이 좌표에서 CLLocations을 생성, 그들에 -coordinate를 호출 할 수 있습니다, 다음 미터의 거리를 검색하는 방법 -getDistanceFrom를 사용합니다. 거기에서부터 마일로 쉽게 변환됩니다. 모두 말해서, 코드는 다음과 같이 보일 것입니다 :

CLLocationCoordinate2D pointACoordinate = [pointAAnnotation coordinate]; 
CLLocation *pointALocation = [[CLLocation alloc] initWithLatitude:pointACoordinate.latitude longitude:pointACoordinate.longitude]; 
CLLocationCoordinate2D pointBCoordinate = [pointBAnnotation coordinate]; 
CLLocation *pointBLocation = [[CLLocation alloc] initWithLatitude:pointBCoordinate.latitude longitude:pointBCoordinate.longitude]; 
double distanceMeters = [pointALocation getDistanceFrom:pointBLocation]; 
double distanceMiles = (distanceMeters/1609.344); 

거리가 마일로 끝나고 거기에서 비교할 수 있습니다. 희망이 도움이됩니다.

+0

ok 그러나이 방법으로 우리는 그 두 점 사이의 직선 거리를 얻습니다 ... 실제 거리가 아닌이 실제 거리를 찾는 방법 .... – AJPatel

12

getDistanceFrom은 이제 더 이상 사용되지 않으므로 여기를 클릭하여이 작업을 수행 할 수 있습니다. 위와 같이

CLLocationCoordinate2D pointACoordinate = [pointAAnnotation coordinate]; 
CLLocation *pointALocation = [[CLLocation alloc] initWithLatitude:pointACoordinate.latitude longitude:pointACoordinate.longitude]; 

CLLocationCoordinate2D pointBCoordinate = [pointBAnnotation coordinate]; 
CLLocation *pointBLocation = [[CLLocation alloc] initWithLatitude:pointBCoordinate.latitude longitude:pointBCoordinate.longitude]; 

float distanceMeters = [pointALocation distanceFromLocation:pointBLocation]; 
float distanceMiles = (distanceMeters/1609.344); 

[pointALocation release]; 
[pointBLocation release]; 

, 당신은 대신 doublefloat를 사용할 수하고 float 그래서 등의 정밀도를 필요로하지 않는 경우는 int로 결과를 캐스팅 할 수 다음 int

int distanceCastToInt = (int) [pointALocation distanceFromLocation:pointBLocation]; 

을 주석에서 거리의 대략적인 아이디어를 다음과 같이 제공하려는 경우 편리합니다.

pointAAnnotation.title = @"Point A"; 
pointAAnnotation.subtitle = [NSString stringWithFormat: @"Distance: %im",distanceCastToInt]; 

t 자막 그 주석은 "Distance : 50m"을 예로들 수 있습니다.

0

이 주어진 답의 3 동등한 스위프트입니다 :

let pointACoordinate = pointAAnnotation.coordinate 
let pointALocation = CLLocation(latitude: pointACoordinate.latitude, longitude: pointACoordinate.longitude) 
let pointBCoordinate = pointBAnnotation.coordinate 
let pointBLocation = CLLocation(latitude: pointBCoordinate.latitude, longitude: pointBCoordinate.longitude) 
let distanceMeters = pointALocation.distance(from: pointBLocation) 
let distanceMiles = (distanceMeters/1609.344)