/
JammieStop.php
133 lines (77 loc) · 2.83 KB
/
JammieStop.php
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
<?php
final class jammieStop{
//class will be called by class that will confirm their location
private $name="";
private $id;
private $lat="";
private $long="";
private $connection;
private $distancesArray= array();
public fucniton __construct(){
}
public function getNearestStop($lat, $long){
$retult=$connection->query("SELECT * FROM jammie_stops");
while ($jammieStop= $result->fetch_object()) {
$this->distancesArray[$jammieStop->id] = vincentyGreatCircleDistance($lat,$long, $jammieStop->latitude, $jammieStop->longitude);
}
//arrange associate array from lowest to highest
arsot($this->distancesArray);
$slicedArray= array_slice($this->distancesArray,0,1);
foreach ( $slicedArray as $key = > $value ) {
$nearestJammieStopToReturn = new self();
$nearestJammieStopToReturn->setUpFromId($key);
return $nearestJammieStopToReturn;
}
//create nearestJammieStop object
//return nearestJammieStop object
}
public function setUpFromId($id){
$retult=$connection->query("SELECT * FROM jammie_stops");
while ($jammieStop= $result->fetch_object()) {
$this->id=$jammieStop->id;
$this->name=$jammieStop->name;
$this->latitude=$jammieStop->latitude;
$this->longitude=$jammieStop->longitude;
}
}
private function assignId(){
$result=$this->connection->query("SELECT `id` FROM `jammie_stops` WHERE `name`='$this->name' ");
if(mysqli_num_rows($result)>0){
while($obj=$result->fetch_object()){
$this->id=$obj->id;
}
}
// mysqli_close();
}
public function __get($variableName){
return $this->$variableName;
}
public function __set($variableName, $value){
$this->$variableName=$value;
}
//back-end functions
private function addToDataBase(){
$this->connection->query("INSERT INTO `jammie_stops` (name,latitude,longitude)
VALUES('$this->name','$this->lat','$this->long')") or die(mysqli_error());
}//end of back end function
//find the distance between any two given points in the world
function vincentyGreatCircleDistance($latitudeFrom, $longitudeFrom, $latitudeTo, $longitudeTo)
{
// convert from degrees to radians
$earthRadius = 6371000;
$latFrom = deg2rad($latitudeFrom);
$lonFrom = deg2rad($longitudeFrom);
$latTo = deg2rad($latitudeTo);
$lonTo = deg2rad($longitudeTo);
$lonDelta = $lonTo - $lonFrom;
$a = pow(cos($latTo) * sin($lonDelta), 2) +
pow(cos($latFrom) * sin($latTo) - sin($latFrom) * cos($latTo) * cos($lonDelta), 2);
$b = sin($latFrom) * sin($latTo) + cos($latFrom) * cos($latTo) * cos($lonDelta);
$angle = atan2(sqrt($a), $b);
$distanceInKm = ($angle * $earthRadius)/1000;
return $distanceInKm;
}
}
$stop = new jammieStop();
echo $stop;
?>