gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-02 22:02:26 25 4
gpt4 key购买 nike

我必须计算 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com