/
BcGmapsComponent.php
222 lines (194 loc) · 3.76 KB
/
BcGmapsComponent.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
216
217
218
219
220
221
222
<?php
/**
* baserCMS : Based Website Development Project <http://basercms.net>
* Copyright (c) baserCMS Users Community <http://basercms.net/community/>
*
* @copyright Copyright (c) baserCMS Users Community
* @link http://basercms.net baserCMS Project
* @package Baser.Controller.Component
* @since baserCMS v 0.1.0
* @license http://basercms.net/license/index.html
*/
define('MAPS_HOST', 'maps.googleapis.com');
/**
* GoogleMap コンポーネント
*
* @package Baser.Controller.Component
*/
class BcGmapsComponent extends Component {
/**
* 接続試行回数
* @var int
*/
const RETRY_TIMES = 3;
/**
* 接続試行の間隔(ミリ秒)
* @var int
*/
const RETRY_INTERVAL = 250;
/**
* Latitude
*
* @var double
*/
protected $_latitude;
/**
* Longitude
*
* @var double
*/
protected $_longitude;
/**
* Address
*
* @var string
*/
protected $_address;
/**
* Country name
*
* @var string
*/
protected $_countryName;
/**
* Country name code
*
* @var string
*/
protected $_countryNameCode;
/**
* Administrative area name
*
* @var string
*/
protected $_administrativeAreaName;
/**
* Postal Code
*
* @var string
*/
protected $_postalCode;
/**
* Base Url
*
* @var string
*/
protected $_baseUrl;
/**
* Construct
* fixme 親クラスのコンストラクタを呼ばないとControllerへのアクセスができない。newしている箇所の修正も必要
* @return void
*/
public function __construct() {
$this->_baseUrl = "http://" . MAPS_HOST . "/maps/api/geocode/xml?";
}
/**
* getInfoLocation
*
* @param string $address
* @return boolean
*/
public function getInfoLocation($address) {
if (!empty($address)) {
return $this->_connect($address);
}
return false;
}
/**
* connect to Google Maps
*
* @param string $param
* @return boolean
*/
protected function _connect($param) {
$apiKey = empty($this->Controller->siteConfig['google_maps_api_key']) ? "" : $this->Controller->siteConfig['google_maps_api_key'];
$requestUrl = $this->_baseUrl . "key=" . $apiKey . "&address=" . urlencode($param);
App::uses('Xml', 'Utility');
try {
$xml = retry(self::RETRY_TIMES, function () use ($requestUrl) {
return Xml::build($requestUrl);
}, self::RETRY_INTERVAL);
$xmlArray = Xml::toArray($xml);
} catch (XmlException $e) {
return false;
} catch (\Exception $e) {
return false;
}
$xml = $xmlArray['GeocodeResponse'];
$result = null;
if (!empty($xml['result']['geometry'])) {
$result = $xml['result'];
} elseif(!empty($xml['result'][0])) {
$result = $xml['result'][0];
}
if ($result) {
if (!isset($result['geometry']['location'])) {
return false;
}
$point = $result['geometry']['location'];
if (!empty($point)) {
$this->_latitude = $point['lat'];
$this->_longitude = $point['lng'];
}
return true;
} else {
return false;
}
}
/**
* get the Postal Code
*
* @return string
*/
//public function getPostalCode () {
// return $this->_postalCode;
//}
/**
* get the Address
*
* @return string
*/
//public function getAddress () {
// return $this->_address;
//}
/**
* get the Country name
*
* @return string
*/
//public function getCountryName () {
// return $this->_countryName;
//}
/**
* get the Country name code
*
* @return string
*/
//public function getCountryNameCode () {
// return $this->_countryNameCode;
//}
/**
* get the Administrative area name
*
* @return string
*/
//public function getAdministrativeAreaName () {
// return $this->_administrativeAreaName;
//}
/**
* get the Latitude coordinate
*
* @return double
*/
public function getLatitude() {
return $this->_latitude;
}
/**
* get the Longitude coordinate
*
* @return double
*/
public function getLongitude() {
return $this->_longitude;
}
}