2016-07-05 68 views
-2

我正在開發一個項目,在該項目中我必須顯示用戶位置的多個位置的距離。位置基於經度和緯度。iOS:如何從一個位置獲取多個位置的距離

我用下面的代碼獲取兩個位置之間的距離爲顯示大致相同的距離

CLLocation *locA = [[CLLocation alloc] initWithLatitude:28.6379 longitude: 77.2432]; 
CLLocation *locB = [[CLLocation alloc] initWithLatitude:28.6562 longitude:77.2410]; 

CLLocationDistance distance = [locA distanceFromLocation:locB]; 
NSLog(@"Distance is %f",distance); 
float i = distance/1000; 
NSLog(@"distance between two places is %f KM", i); 

但現在我結構來從我的位置的多個位置的距離:locaA。

例如我把NSArray的緯度和經度

NSArray * latArray = [[NSArray alloc]initWithObjects:@"28.6129",@"28.6020",@"28.5244", nil]; 
NSArray * longArray = [[NSArray alloc]initWithObjects:@"77.2295",@"77.2478",@"77.1855", nil]; 

請幫我解決這個問題

採取locaA作爲用戶的位置

+1

有沒有你正在使用'latitude'值的數組和'longitude'值的另一個數組的一個原因?你應該有一個你想要找到的所有'CLLocation'對象的單個數組。 – AdamPro13

+0

在提問前請Google。 –

+0

@ AdamPro13不,沒有任何具體原因,我認爲它提供了一些簡單的方法來輕鬆獲得距離。以及我如何將這些值用於單個數組? – Abhi

回答

1
CLLocation *currentLocation = ... // This is a reference to your current location as a CLLocation 
NSArray *arrayOfOtherCLLocationObjects = ... // This is an array that contains all of the other points you want to calculate the distance to as CLLocations 

NSMutableArray *distancesFromCurrentLocation = [[NSMutableArray alloc] initWithCapacity:arrayOfOtherCLLocationObjects.count]; // We will add all of the calculated distances to this array 

for (CLLocation *location in arrayOfOtherCLLocationObjects) // Iterate through each location object 
{ 
    CLLocationDistance distance = [location distanceFromLocation:currentLocation]; // Calculate distance 
    [distancesFromCurrentLocation addObject:@(distance)]; // Append distance to array. You need to wrap the distance object as an NSNumber so you can append it to the array. 
} 

// At this point, you have the distance for each location point in the array distancesFromCurrentLocation 
+0

它顯示一個錯誤: - [__ NSCFConstantString distanceFromLocation:]:無法識別的選擇器發送到實例。 – Abhi

+0

這是因爲你試圖在'NSString'上調用'distanceFromLocation:',而不是'CLLocation'對象。 – AdamPro13

+0

好吧,以及如何創建陣列的拉特和長我創建像這樣:NSArray * arrayOfOtherCLLocationObjects = [[NSArray alloc] initWithObjects:@「28.6562」,@「77.2410」,nil]; – Abhi

2

您可以使用下面的方法來找到距離

#define DEG2RAD(degrees) (degrees * 0.01745327) 

double currentLatitudeRad = DEG2RAD(currentLatitude); 
double currentLongitudeRad = DEG2RAD(currentLongitude); 
double destinationLatitudeRad = DEG2RAD(destinationLatitude); 
double destinationLongitudeRad = DEG2RAD(destinationLongitude); 

double distance = acos(sin(currentLatitudeRad) * sin(destinationLatitudeRad) + cos(currentLatitudeRad) * cos(destinationLatitudeRad) * cos(currentLongitudeRad - destinationLongitudeRad)) * 6880.1295896; 

這裏,currentLatitude和currentLongitude是用戶的lo陽離子。 destinationLatitude和destinationLongitude是您的數組「latArray」和「longArray」中的每個對象,您可以通過for循環進行迭代。距離是用戶位置與陣列中位置之間的距離。獲得的距離將以公里爲單位。

+0

感謝您的答案:)請問您能否詳細更新我如何打印所有這些值 – Abhi

+0

這是更復雜的,它需要。你可以在'CLLocation'上使用'distanceFromLocation:'方法爲你做這件事。 – AdamPro13

0

斯威夫特版本:

let currentLocation: CLLocation = //current location 
let otherLocations: [CLLocation] = //the locations you want to know their distance to currentLocation 

let distances = otherLocations.map { $0.distanceFromLocation(currentLocation) }