- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我必须计算 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/
我正在尝试让 Sprite 在 Canvas 上走过背景图像。理想情况下,我会在一张 Canvas 上完成这一切,但使用两张 Canvas 似乎更高效、更容易。 到目前为止我所拥有的: Fiddle
哦,嗨。我是一名初级 Java 开发人员,在空闲时间从事一些基于 2D 图 block 的游戏。现在我正在尝试实现游戏模型中非常基本的东西 - 各种类型的对象如何彼此交互。我希望有一天添加网络支持,所
我们如何使用 CoreMotion 数据检测用户正在驾驶/步行/运行/静止。我们可以使用 CMMotionActivityManager 获取 iPhone 5s 中的用户事件。但是如何进入低版本设备
我是一名优秀的程序员,十分优秀!