计算用户位置与固定位置之间的距离

Sur*_*rya 6 cocoa-touch objective-c geolocation latitude-longitude ios

我从服务器收到多家餐馆的纬度/经度.我想计算每个餐馆和用户当前纬度/经度之间的距离.

Emp*_*ack 39

您可以使用CLLocation的distanceFromLocation:方法返回两个CLLocation之间的距离(以米为单位).

CLLocation *currentLoc = /* Assume you have found it */;
CLLocation *restaurantLoc = [[CLLocation alloc] initWithLatitude:restaurantLat longitude:restaurantLng];
CLLocationDistance meters = [restaurantLoc distanceFromLocation:currentLoc];
// Go ahead with this
Run Code Online (Sandbox Code Playgroud)


Cla*_*och 3

一旦你有了地点列表(一种或另一种方式),你就可以使用 CoreLocation 获取到(或离开)这些地点的距离,并使用 distanceFromLocation 来计算: