簡體   English   中英

計算兩組坐標(NSStrings)之間的距離

[英]Calculate Distance between Two Sets of Coordinates (NSStrings)

我有兩組地理坐標,我正在嘗試計算它們之間的距離。 我已經做了一些挖掘工作,但無法弄清楚該怎么做。 我正在嘗試獲取用戶(userLatitude / userLongitude)與地點(placeLatitude / placeLongitude)之間的距離(以英里為單位)。 我的坐標存儲為NSStrings,如下所示。 有誰知道如何做到這一點? 謝謝你們!

NSString *userLatitude =[(PDCAppDelegate *)[UIApplication sharedApplication].delegate 
getUserLatitude];

NSString *userLongitude =[(PDCAppDelegate *)[UIApplication sharedApplication].delegate 
getUserLongitude];

NSString *placeLatitude = [element1 objectForKey:@"placeLatitude"];

NSString *placeLongitude = [element1 objectForKey:@"placeLongitude"];

這可能對您有幫助。 嘗試從lat創建CLLocation

CLLocation* Location1;
CLLocation* Location2;
CLLocationDistance distance = [Location1 distanceFromLocation:Location2];

從下面您可以獲得距離。

CLLocation *locA = [[CLLocation alloc] initWithLatitude:currentLocation.latitude longitude:currentLocation.longitude];

CLLocation *locB = [[CLLocation alloc] initWithLatitude:[[element1 objectForKey:@"placeLatitude"] doubleValue]  longitude:[[element1 objectForKey:@"placeLongitude"] doubleValue]];

CLLocationDistance currentDistance = [locA distanceFromLocation:locB];

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM