2011-12-01 3 views
4

여러 매장이있는 고객을 위해 iphone 앱 (목표 C)을 개발 중입니다. 배열에있는 모든 상점 (20)의 좌표 (위도, 길이)가 있습니다.기존 좌표에서 가장 가까운 위치 찾기 mapkit

현재 상점 좌표의 배열을 반복하고 사용자의 현재 위치에서 상점 위치까지의 거리를 구한 다음 배열에 추가하고 가장 낮은 거리에서 정렬을 수행하려고합니다.

올바른 접근 방식인가, 아니면 매우 배가 된 자원입니까?

SOF의 가장 가까운 질문이 하나 있지만, 파이썬에 있습니다 extracting nearest stores

는 귀하의 조언에 미리 감사드립니다.

답변

6

가져온 데이터 중에서 가장 가까운 위치를 찾으려면 현재 위치에서 현재 위치까지의 거리를 계산해야합니다. 그런 다음 해당 데이터를 정렬하면 기존 좌표 맵킷에서 가장 가까운 위치를 가져옵니다. 다음

... 거리를 알아내는 방법이다

-(float)kilometresBetweenPlace1:(CLLocationCoordinate2D) currentLocation andPlace2:(CLLocationCoordinate2D) place2 
{ 
    CLLocation *userLoc = [[CLLocation alloc] initWithLatitude:currentLocation.latitude longitude:currentLocation.longitude]; 
    CLLocation *poiLoc = [[CLLocation alloc] initWithLatitude:place2.latitude longitude:place2.longitude]; 
    CLLocationDistance dist = [userLoc getDistanceFrom:poiLoc]/(1000*distance); 
    NSString *strDistance = [NSString stringWithFormat:@"%.2f", dist]; 
    NSLog(@"%@",strDistance); 
    return [strDistance floatValue]; 
} 
관련 문제