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: 85: 86: 87: 88: 89: 90: 91: 92: 93: 94: 95: 96: 97: 98: 99: 100: 101: 102: 103: 104: 105: 106: 107:
<?php
namespace LeanCloud;
class GeoPoint {
private $latitude;
private $longitude;
public function __construct($latitude=0.0, $longitude=0.0) {
if ($latitude <= 90.0 && $latitude >= -90.0 &&
$longitude <= 180.0 && $longitude >= -180.0) {
$this->latitude = $latitude;
$this->longitude = $longitude;
} else {
throw new \InvalidArgumentException("Invalid latitude or " .
"longitude for geo point");
}
}
public function getLatitude() {
return $this->latitude;
}
public function getLongitude() {
return $this->longitude;
}
public function radiansTo(GeoPoint $point) {
$d2r = M_PI / 180.0;
$lat1rad = $this->getLatitude() * $d2r;
$lon1rad = $this->getLongitude() * $d2r;
$lat2rad = $point->getLatitude() * $d2r;
$lon2rad = $point->getLongitude() * $d2r;
$deltaLat = $lat1rad - $lat2rad;
$deltaLon = $lon1rad - $lon2rad;
$sinLat = sin($deltaLat / 2);
$sinLon = sin($deltaLon / 2);
$a = $sinLat * $sinLat +
cos($lat1rad) * cos($lat2rad) * $sinLon * $sinLon;
$a = min(1.0, $a);
return 2 * asin(sqrt($a));
}
public function kilometersTo(GeoPoint $point) {
return $this->radiansTo($point) * 6371.0;
}
public function milesTo(GeoPoint $point) {
return $this->radiansTo($point) * 3958.8;
}
public function encode() {
return array(
'__type' => 'GeoPoint',
'latitude' => $this->latitude,
'longitude' => $this->longitude
);
}
}