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

6

我从服务器接收到多个餐厅的纬度/经度。我想要计算每个餐厅与用户当前的纬度/经度之间的距离。


你找到用户的当前位置了吗?还是在寻找的过程中遇到问题了? - EmptyStack
谢谢您的回复:我有用户当前位置,我的餐厅经纬度是可变数组,如何访问这些经纬度以查找距离。 - Surya
4个回答

39
你可以使用CLLocationdistanceFromLocation:方法,该方法返回两个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

谢谢您的回复:我有用户当前位置,我的餐厅经纬度是可变数组,如何访问这些经纬度以找到距离。 - Surya
你只有一个数组还是分别有两个数组来存储纬度和经度? - EmptyStack

3
一旦您有了一个地点列表(无论是哪种方式),您可以使用CoreLocation的distanceFromLocation:方法计算这些地点之间的距离。

2
你可以使用谷歌提供的API来实现相同的功能。 谷歌距离矩阵API非常适合查找距离。希望能对你有所帮助。

0

Swift 3

func calculateDistanceFromUser(location:CLLocation)->CLLocation!{
    if let userLocation = self.userUpdatedLocation{ //CLLocation
        let meters = location.distance(from: userLocation)
        return meters
    }
    return nil
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接