forked from leancloud/cpp-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAVGeoPoint.cpp
More file actions
84 lines (64 loc) · 2.12 KB
/
AVGeoPoint.cpp
File metadata and controls
84 lines (64 loc) · 2.12 KB
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
/**
* @file AVGeoPoint.cpp
* @author yang chaozhong <cyang@avoscloud.com>
* @date Thu Aug 14 16:43:23 2014
*
* @brief
*
* Copyright 2014 AVOS Cloud Inc. All rights reserved.
*/
#include "Geo/AVGeoPoint.h"
#include <cmath>
NS_AV_BEGIN
const double pi = 3.14159265358;
AVGeoPoint::AVGeoPoint():latitude(0),
longitude(0) {
}
AVGeoPoint* AVGeoPoint::geoPoint() {
AVGeoPoint* point = new AVGeoPoint();
return point;
}
AVGeoPoint* AVGeoPoint::geoPointWithLatitudeAndLongitude(double latitude, double longitude) {
AVGeoPoint* point = AVGeoPoint::geoPoint();
point->latitude = latitude;
point->longitude = longitude;
return point;
}
void AVGeoPoint::release() {
AV_SAFE_DELETE(this);
}
double AVGeoPoint::distanceInRadiansTo(AVGeoPoint* point) {
return this->distanceInKillometersTo(point) / 6378.140;
}
double AVGeoPoint::distanceInMilesTo(AVGeoPoint* point) {
return this->distanceInKillometersTo(point) / 1.609344;
}
double AVGeoPoint::distanceInKillometersTo(AVGeoPoint* point) {
double theta = this->longitude - point->longitude;
double dist = sin(degreeToRadian(this->latitude)) * sin(degreeToRadian(point->latitude))
+ cos(degreeToRadian(this->latitude)) * cos(degreeToRadian(point->latitude))
* cos(degreeToRadian(theta));
dist = acos(dist);
dist = radianToDegree(dist);
dist = dist * 60 * 1.1515 * 1.609344;
return dist;
}
Json::Value AVGeoPoint::dictionaryFromGeoPoint(AVGeoPoint* point) {
Json::Value dict(Json::objectValue);
dict["__type"] = "GeoPoint";
dict["latitude"] = point->latitude;
dict["longitude"] = point->longitude;
return dict;
}
AVGeoPoint* AVGeoPoint::geoPointFromDictionary(Json::Value const & dict) {
return AVGeoPoint::geoPointWithLatitudeAndLongitude(dict["latitude"].asDouble(),
dict["longitude"].asDouble());
}
///////////////////////////////private methods/////////////////////////////
double AVGeoPoint::degreeToRadian(double degree) {
return degree * pi / 180.0;
}
double AVGeoPoint::radianToDegree(double radian) {
return radian * 180.0 / pi;
}
NS_AV_END