2011-12-01 68 views
4

我正在爲擁有多個商店的客戶開發iPhone應用程序(目標C)。我有一個數組中的所有商店(20)的座標(緯度,長度)。從現有座標查找最近的位置mapkit

目前我正在考慮循環訪問商店的座標數組,並獲取從用戶當前位置到商店位置的距離,然後將它們添加到數組中,並在最小距離上進行排序。

這是一個正確的方法還是非常資源飢餓?

SOF的最近的問題是這一個,但它是在Python: extracting nearest stores

預先感謝您的建議。

回答

6

要查找獲取的數據中最近的位置,您需要計算從當前位置到每個位置的距離。然後,只需對這些數據進行排序,就可以從現有的座標mapkit中獲取最近的位置。

以下是找出距離的方法...

-(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]; 
}