|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Geocoder; |
|
4
|
|
|
|
|
5
|
|
|
class Geocoder |
|
6
|
|
|
{ |
|
7
|
|
|
const API_URL = 'http://maps.google.com/maps/api/geocode'; |
|
8
|
|
|
const VALID_OUTPUT_FORMAT = ['json', 'xml']; |
|
9
|
|
|
|
|
10
|
|
|
private $apiKey; |
|
11
|
|
|
|
|
12
|
9 |
|
public function __construct($apiKey = '') |
|
13
|
|
|
{ |
|
14
|
9 |
|
$this->setApiKey($apiKey); |
|
15
|
9 |
|
} |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* @return string |
|
19
|
|
|
*/ |
|
20
|
8 |
|
public function getApiKey() |
|
21
|
|
|
{ |
|
22
|
8 |
|
return $this->apiKey; |
|
23
|
|
|
} |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* @param string $apiKey |
|
27
|
|
|
*/ |
|
28
|
9 |
|
public function setApiKey($apiKey) |
|
29
|
|
|
{ |
|
30
|
9 |
|
$this->apiKey = $apiKey; |
|
31
|
9 |
|
} |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* @param $address |
|
35
|
|
|
* @param string $region |
|
36
|
|
|
* @param string $outputFormat |
|
37
|
|
|
* |
|
38
|
|
|
* @throws \Exception |
|
39
|
|
|
* |
|
40
|
|
|
* @return string |
|
41
|
|
|
*/ |
|
42
|
9 |
|
public function geocode($address, $region = '', $outputFormat = 'json') |
|
43
|
|
|
{ |
|
44
|
9 |
|
if ($this->validateOutputFormat($outputFormat) !== true) { |
|
45
|
1 |
|
throw new \Exception("'{$outputFormat}' is not a valid format"); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
8 |
|
return file_get_contents($this->generateRequestUrl($address, $region, $outputFormat)); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* @param $address |
|
53
|
|
|
* |
|
54
|
|
|
* @return array |
|
55
|
|
|
*/ |
|
56
|
3 |
|
public function getLatLng($address) |
|
57
|
|
|
{ |
|
58
|
3 |
|
$result = $this->geocode($address); |
|
59
|
3 |
|
$result = json_decode($result, true); |
|
60
|
|
|
|
|
61
|
3 |
|
if ($result['status'] !== 'OK') { |
|
62
|
1 |
|
return; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
2 |
|
$latLng = []; |
|
66
|
2 |
|
foreach ($result['results'] as $result) { |
|
67
|
2 |
|
$latLng[] = [ |
|
68
|
2 |
|
'lat' => $result['geometry']['location']['lat'], |
|
69
|
2 |
|
'lng' => $result['geometry']['location']['lng'], |
|
70
|
|
|
]; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
2 |
|
return $latLng; |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* @param $format |
|
78
|
|
|
* |
|
79
|
|
|
* @return bool |
|
80
|
|
|
*/ |
|
81
|
9 |
|
private function validateOutputFormat($format) |
|
82
|
|
|
{ |
|
83
|
9 |
|
if (in_array($format, self::VALID_OUTPUT_FORMAT)) { |
|
84
|
8 |
|
return true; |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
1 |
|
return false; |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* @param $address |
|
92
|
|
|
* @param string $region |
|
93
|
|
|
* @param string $outputFormat |
|
94
|
|
|
* @param bool $sensor |
|
95
|
|
|
* |
|
96
|
|
|
* @return string |
|
97
|
|
|
*/ |
|
98
|
8 |
|
private function generateRequestUrl($address, $region = '', $outputFormat = 'json', $sensor = false) |
|
99
|
|
|
{ |
|
100
|
8 |
|
$baseUrl = self::API_URL.'/'.$outputFormat.'?address='.urlencode($address).'&sensor='.$sensor; |
|
101
|
|
|
|
|
102
|
8 |
|
if (!empty($region)) { |
|
103
|
2 |
|
$baseUrl .= "®ion={$region}"; |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
8 |
|
if (!empty($this->getApiKey())) { |
|
107
|
1 |
|
$baseUrl .= '&key='.$this->getApiKey(); |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
8 |
|
return $baseUrl; |
|
111
|
|
|
} |
|
112
|
|
|
} |
|
113
|
|
|
|