-
Notifications
You must be signed in to change notification settings - Fork 274
/
geodist.c
39 lines (27 loc) · 846 Bytes
/
geodist.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
//
// geodist.c
// Algorithms - Geo dist
//
// Created by YourtionGuo on 25/05/2017.
// Copyright © 2017 Yourtion. All rights reserved.
//
#include "geodist.h"
#include "geometry.h"
#pragma mark - Public
int geodist(double lat1, double lon1, double lat2, double lon2, double *d)
{
SPoint p1, p2;
/// 检验坐标是否合法
if (lat1 < -90.0 || lat1 > 90.0 || lat2 < -90.0 || lat2 > 90.0) return -1;
if (lon1 < -180.0 || lon1 > 180.0 || lon2 < -180.0 || lon2 > 180.0) return -1;
/// 将经纬度转换为球坐标
p1.rho = EARTH_RADIUS;
p1.theta = -1.0 * DEGTORAD(lon1);
p1.phi = (DEGTORAD(-1.0 * lat1)) + DEGTORAD(90.0);
p2.rho = EARTH_RADIUS;
p2.theta = -1.0 * DEGTORAD(lon2);
p2.phi = (DEGTORAD(-1.0 * lat2)) + DEGTORAD(90.0);
/// 计算两个点的距离
arclen(p1, p2, d);
return 0;
}