ios - 在固定点和我在 iOS 中的当前位置之间行走时计算距离

标签 ios core-location cllocationmanager

我必须计算 iOS 和 objective-c 中两个位置之间的距离。我的一个位置固定在一个点上,当我走路时,我必须计算我当前位置和固定点之间的距离。我使用了 distanceFromLocation 方法,但我没有得到更近的距离值。我浏览了几篇文章和一些 StackOverflow 解决方案,但没有一个给我正确的结果。

下面是我使用的代码,代码中的 currentLocation 和 destinationLocation 是用于保存位置的纬度和经度的属性。 destinationLocation 始终是固定位置,而 currentLocation 在我走路时不断变化。很少有 UILabel 可以打印当前和固定的纬度和经度值。我每次都必须计算这两点之间的距离。当我移动半米或更短距离时,它会显示一个很大的距离,这也是不一致的。你能帮我看看我在这里犯了什么错误吗?还有其他方法可以实现这一目标吗?谢谢!

- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray<CLLocation *> *)locations{
    CLLocation *newLocation = locations.lastObject;
    self.currentLocation = [[CLLocation alloc] initWithLatitude:newLocation.coordinate.latitude longitude:newLocation.coordinate.longitude];

    if(self.destinationLocation == nil){
        self.destinationLocation = [[CLLocation alloc] initWithLatitude:newLocation.coordinate.latitude longitude:newLocation.coordinate.longitude];
        self.destinationLatitude.text = [NSString stringWithFormat:@"%.8f", self.destinationLocation.coordinate.latitude];
        self.destinationLongitude.text = [NSString stringWithFormat:@"%.8f", self.destinationLocation.coordinate.longitude];
    }

    self.currentLatitude.text = [NSString stringWithFormat:@"%.8f", self.currentLocation.coordinate.latitude];
    self.currentLongitude.text = [NSString stringWithFormat:@"%.8f", self.currentLocation.coordinate.longitude];

    CLLocationDistance distance = [self.currentLocation distanceFromLocation:self.destinationLocation];
    self.gpsDistanceMeasurement.text = [NSString stringWithFormat:@"%.2f m", distance];
}

最佳答案

可以用haversine公式计算两点之间的距离

swift 3.x

let Source = CLLocationCoordinate2D.init(latitude: lat, longitude: long)
let Destination = CLLocationCoordinate2D.init(latitude: lat, longitude: long)

func DistanceCalculator(Source:CLLocationCoordinate2D,Destination:CLLocationCoordinate2D) -> Double
{
    // HaverSine Formula to calculate Diastance On Sphere Refrences//https://www.movable-type.co.uk/scripts/latlong.html
    // Angle = sin2(∆Ø/2) + cosØ1 * cosØ2 * sin2(∆ø/2)
    // constant = 2 * atan2(√angle,√1-angle)
    // distance = R * c
    let Earth_Radius:Double = 6371 * 1000
    let LatDelta = self.DegreeToRad(Degree: (Destination.latitude - Source.latitude))
    let LongDelta = self.DegreeToRad(Degree: (Destination.longitude - Source.longitude))
    let latRad = self.DegreeToRad(Degree: Source.latitude)
    let longRad = self.DegreeToRad(Degree: Destination.latitude)

    let Angle = (sin(LatDelta/2) * sin(LatDelta/2)) + (cos(latRad) * cos(longRad) * sin(LongDelta/2) * sin(LongDelta/2))
    let constant = 2 * atan2(sqrt(Angle),sqrt(1-Angle))
    let Distance = Earth_Radius * constant
    return Distance
}


func DegreeToRad(Degree:Double) -> Double
{
    return Degree * (Double.pi / 180)
}

关于ios - 在固定点和我在 iOS 中的当前位置之间行走时计算距离,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51136210/

相关文章:

ios - 如何在UIView的init方法中传递参数?

ios - 在通用应用程序中滚动查看内容大小?

iphone - 来自 UIAlertView 的 UIActionSheet

swift - framework 的 "import"和 framework 的 "linking"有什么区别?

iphone - 监视区域但位置图标在应用程序被杀死时消失

iphone - 核心数据实体为字典/plist

ios - 使用闭包初始化属性时将自己设置为委托(delegate)

ios - 不会为小距离位置更改调用 didUpdateLocations 委托(delegate)方法

iphone - 从其他 ViewController 的 locationManager 方法访问 newLocation

ios - 在数组中查找距用户位置最近的经度和纬度