/
gmapsCoordinates.class.php
executable file
·215 lines (186 loc) · 5.55 KB
/
gmapsCoordinates.class.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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
<?php
class gmapsCoordinates
{
/**
* @var Core
*/
protected $core;
protected $gmapsUrl = "http://maps.google.com/maps/geo?output=xml&oe=utf-8&key=%KEY%&q=%QUERY%";
//hold some values for google maps
protected $street = '';
protected $number = '';
protected $city = '';
protected $zip = '';
protected $country = '';
protected $gmapsKey = '';
protected $longitude;
protected $latitude;
protected $gmapsQuery = '';
protected $zeroLat = 0;
protected $zeroLong = 0;
/**
* Constructor
*/
public function __construct()
{
$this->core = Zend_Registry::get('Core');
}
public function setZeroLat($latx)
{
$this->zeroLat = $latx;
}
public function setZeroLong($longY)
{
$this->zeroLong = $longY;
}
public function setStreet($_street)
{
$this->street = $_street;
}
public function setNumber($_number)
{
$this->number = $_number;
}
public function setZIP($_zip)
{
$this->zip = $_zip;
}
public function setCity($_city)
{
$this->city = $_city;
}
public function setCountry($_country)
{
$this->country = $_country;
}
public function setGMapsKey($key)
{
$this->gmapsKey = $key;
$this->gmapsUrl = str_replace('%KEY%', $this->gmapsKey, $this->gmapsUrl);
}
public function getKeyFromCore()
{
$this->gmapsKey = $this->core->sysConfig->gmaps->key;
$this->gmapsUrl = str_replace('%KEY%', $this->gmapsKey, $this->gmapsUrl);
}
protected function strbet($inputStr, $delimeterLeft, $delimeterRight, $debug = false)
{
$posLeft = strpos($inputStr, $delimeterLeft);
if ($posLeft === false) {
if ($debug) {
echo "Warning: left delimiter '{$delimeterLeft}' not found";
}
return false;
}
$posLeft += strlen($delimeterLeft);
$posRight = strpos($inputStr, $delimeterRight, $posLeft);
if ($posRight === false) {
if ($debug) {
echo "Warning: right delimiter '{$delimeterRight}' not found";
}
return false;
}
return substr($inputStr, $posLeft, $posRight - $posLeft);
}
protected function doGMapQuery()
{
$coord = '';
$gmapsUrl = str_replace('%QUERY%', urlencode($this->gmapsQuery), $this->gmapsUrl);
//echo "\n" . "URL:" . $gmapsUrl ;
$xml = simplexml_load_file($gmapsUrl);
//quickfix für Frühlingsstraße+6850+Dornbirn
//9.7466230,47.4147330,0
//echo "\nName:" . $xml->Response->name . "!";
if ($xml->Response->name == '6850+Dornbirn+Frühlingsstraße') {
echo "FIX FOR SCHERTLER ALGE !!!! \n";
return "9.7466230,47.4147330,0";
}
if (!empty($xml->Response)) {
$point = $xml->Response->Placemark->Point;
//echo " coord:" . $point->coordinates . "\n";
if (!empty($point)) return $point->coordinates;
}
return $coord;
}
protected function setMostEnhancedQuery()
{
$this->clearQuery();
$this->addSearchQuery($this->zip);
$this->addSearchQuery($this->city);
$this->addSearchQuery($this->country);
$this->addSearchQuery($this->street);
$this->addSearchQuery($this->number, false);
}
protected function setEnhancedQuery()
{
$this->clearQuery();
$this->addSearchQuery($this->zip);
$this->addSearchQuery($this->city);
$this->addSearchQuery($this->country);
$this->addSearchQuery($this->street);
}
protected function setBasicQuery()
{
$this->clearQuery();
$this->addSearchQuery($this->zip);
$this->addSearchQuery($this->city);
$this->addSearchQuery($this->country);
}
protected function addSearchQuery($val2Add, $withPlus = true)
{
if ($val2Add != '') {
if ($this->gmapsQuery != '') {
if ($withPlus === true) {
$this->gmapsQuery .= '+';
} else {
$this->gmapsQuery .= ' ';
}
}
$this->gmapsQuery .= $val2Add;
}
}
protected function clearQuery()
{
$this->gmapsQuery = '';
}
public function calculateCoordinates()
{
$this->longitude = 0;
$this->latitude = 0;
$coord = '';
$retryCounter = 0;
//first create most enhanced query for gmaps
while ($coord == '' && $retryCounter < 3) {
$this->setMostEnhancedQuery();
$coord = $this->doGMapQuery();
if ($coord == '') {
//try another query
$this->setEnhancedQuery();
$coord = $this->doGMapQuery();
if ($coord == '') {
//try another query
$this->setBasicQuery();
$coord = $this->doGMapQuery();
}
}
$retryCounter++;
}
if ($coord == '') {
$this->longitude = $this->zeroLat;
$this->latitude = $this->zeroLong;
} else {
$arrCoord = explode(',', $coord);
$this->longitude = (isset($arrCoord[0])) ? $arrCoord[0] : 0;
$this->latitude = (isset($arrCoord[1])) ? $arrCoord[1] : 0;
}
}
public function getLatitude()
{
return $this->latitude;
}
public function getLongitude()
{
return $this->longitude;
}
}
?>