iOS-计算两个经纬度之间的距离和方位
2014-03-21 18:45
1201 查看
double DegreesToRadians(double degrees) {return degrees * M_PI / 180;}; double RadiansToDegrees(double radians) {return radians * 180 / M_PI;}; - (NSString *)bearingToLocationFromCoordinate:(CLLocation*)fromLoc toCoordinate:(CLLocation*)toLoc { double lat1 = DegreesToRadians(fromLoc.coordinate.latitude); double lon1 = DegreesToRadians(fromLoc.coordinate.longitude); double lat2 = DegreesToRadians(toLoc.coordinate.latitude); double lon2 = DegreesToRadians(toLoc.coordinate.longitude); double dLon = lon2 - lon1; double y = sin(dLon) * cos(lat2); double x = cos(lat1) * sin(lat2) - sin(lat1) * cos(lat2) * cos(dLon); double radiansBearing = atan2(y, x); NSString *str ; double aDeg = RadiansToDegrees(radiansBearing); if (aDeg >= 75 && aDeg <= 105) { str = @"正东"; } if (aDeg > 15 && aDeg < 75) { str = @"东北"; } if (aDeg >= -15 && aDeg <= 15) { str = @"正北"; } if (aDeg > -75 && aDeg < -15) { str = @"西北"; } if (aDeg >= -105 && aDeg <= -75) { str = @"正西"; } if (aDeg > -165 && aDeg < -105) { str = @"西南"; } if ((aDeg >= 165 && aDeg <= 180) || (aDeg >= -180 && aDeg <= -165)) { str = @"正南"; } if (aDeg > 105 && aDeg < 165) { str = @"东南"; } CLLocationDistance meters = [fromLoc distanceFromLocation:toLoc]; return [NSString stringWithFormat:@"%@%f米", str, meters]; }参考资料来自stackoverflow
相关文章推荐
- IOS 计算两个经纬度之间的距离
- iOS通过经纬度计算两个地点之间的距离
- iOS通过经纬度计算两个坐标之间的距离
- 计算两个GPS经纬度坐标之间的距离 PHP
- 在百度地图API开发中已知两个点之间的经纬度坐标计算其距离
- 给你两个经纬度,计算他们之间的距离
- 怎样计算两个经纬度之间的距离?
- JavaScript计算两个经纬度之间的物理距离
- 计算两个经纬度之间的距离
- 计算两个经纬度之间的直线距离
- 两个经纬度之间的距离计算
- PHP计算两个经纬度之间的距离
- [置顶] 【go语言计算两个经纬度距离】根据经纬度计算两点之间距离
- iOS根据两个经纬度计算相距距离
- java计算两个经纬度点之间的距离
- PHP计算两个经纬度之间的距离
- java通过经纬度计算两个点的之间的距离的算法
- 计算两个经纬度之间的距离
- IOS根据两个经纬度计算相距距离
- mysql实现经纬度计算两个坐标之间的距离