1
|
|
|
<?php namespace Arcanedev\GeoLocation\Google\Geocoding; |
2
|
|
|
|
3
|
|
|
use Arcanedev\GeoLocation\Contracts\Entities\Position as PositionContract; |
4
|
|
|
use Arcanedev\GeoLocation\Entities\Position; |
5
|
|
|
use Arcanedev\GeoLocation\Google\AbstractWebService; |
6
|
|
|
use GuzzleHttp\ClientInterface; |
7
|
|
|
use Psr\Http\Message\ResponseInterface; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Class GeocodingService |
11
|
|
|
* |
12
|
|
|
* @package Arcanedev\GeoLocation\Google\Geocoding |
13
|
|
|
* @author ARCANEDEV <[email protected]> |
14
|
|
|
* |
15
|
|
|
* @link https://developers.google.com/maps/documentation/geocoding/intro |
16
|
|
|
*/ |
17
|
|
|
class GeocodingService extends AbstractWebService |
18
|
|
|
{ |
19
|
|
|
/* ----------------------------------------------------------------- |
20
|
|
|
| Constants |
21
|
|
|
| ----------------------------------------------------------------- |
22
|
|
|
*/ |
23
|
|
|
|
24
|
|
|
const BASE_URL = 'https://maps.googleapis.com/maps/api/geocode/json'; |
25
|
|
|
|
26
|
|
|
/* ----------------------------------------------------------------- |
27
|
|
|
| Constructor |
28
|
|
|
| ----------------------------------------------------------------- |
29
|
|
|
*/ |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* GoogleDistanceMatrix constructor. |
33
|
|
|
* |
34
|
|
|
* @param \GuzzleHttp\ClientInterface $client |
35
|
|
|
*/ |
36
|
9 |
|
public function __construct(ClientInterface $client) |
37
|
|
|
{ |
38
|
9 |
|
parent::__construct($client); |
39
|
|
|
|
40
|
9 |
|
$this->setKey(getenv('GOOGLE_MAPS_GEOCODING_KEY')); |
41
|
9 |
|
} |
42
|
|
|
|
43
|
|
|
/* ----------------------------------------------------------------- |
44
|
|
|
| Main Methods |
45
|
|
|
| ----------------------------------------------------------------- |
46
|
|
|
*/ |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Get the geocoding response. |
50
|
|
|
* |
51
|
|
|
* @param string $address |
52
|
|
|
* @param array $options |
53
|
|
|
* |
54
|
|
|
* @return \Arcanedev\GeoLocation\Google\Geocoding\GeocodingResponse |
55
|
|
|
*/ |
56
|
3 |
|
public function geocode($address, array $options = []) |
57
|
|
|
{ |
58
|
3 |
|
$url = static::BASE_URL.'?'.$this->prepareQuery([ |
59
|
3 |
|
'address' => urlencode($address) |
60
|
1 |
|
]); |
61
|
|
|
|
62
|
3 |
|
return $this->get($url, $options); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Reverse geocoding (address lookup). |
67
|
|
|
* |
68
|
|
|
* @param \Arcanedev\GeoLocation\Contracts\Entities\Position $position |
69
|
|
|
* @param array $options |
70
|
|
|
* |
71
|
|
|
* @return \Arcanedev\GeoLocation\Google\Geocoding\GeocodingResponse |
72
|
|
|
*/ |
73
|
3 |
|
public function reverse(PositionContract $position, array $options = []) |
74
|
|
|
{ |
75
|
3 |
|
$url = static::BASE_URL.'?'.$this->prepareQuery([ |
76
|
3 |
|
'latlng' => $this->parsePosition($position), |
77
|
1 |
|
]); |
78
|
|
|
|
79
|
3 |
|
return $this->get($url, $options); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Reverse geocoding (address lookup & simplified). |
84
|
|
|
* |
85
|
|
|
* @param float $lat |
86
|
|
|
* @param float $long |
87
|
|
|
* @param array $options |
88
|
|
|
* |
89
|
|
|
* @return \Arcanedev\GeoLocation\Google\Geocoding\GeocodingResponse |
90
|
|
|
*/ |
91
|
3 |
|
public function reversePosition($lat, $long, array $options = []) |
92
|
|
|
{ |
93
|
3 |
|
return $this->reverse(Position::create($lat, $long), $options); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/* ----------------------------------------------------------------- |
97
|
|
|
| Other Methods |
98
|
|
|
| ----------------------------------------------------------------- |
99
|
|
|
*/ |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Get the default query params. |
103
|
|
|
* |
104
|
|
|
* @return array |
105
|
|
|
*/ |
106
|
6 |
|
protected function getDefaultQueryParams() |
107
|
|
|
{ |
108
|
|
|
return [ |
109
|
6 |
|
'key' => $this->key, |
110
|
2 |
|
]; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* Prepare the response. |
115
|
|
|
* |
116
|
|
|
* @param \Psr\Http\Message\ResponseInterface $response |
117
|
|
|
* |
118
|
|
|
* @return \Arcanedev\GeoLocation\Google\Geocoding\GeocodingResponse |
119
|
|
|
*/ |
120
|
6 |
|
protected function prepareResponse(ResponseInterface $response) |
121
|
|
|
{ |
122
|
6 |
|
return new GeocodingResponse( |
123
|
6 |
|
json_decode($response->getBody(), true) |
124
|
2 |
|
); |
125
|
|
|
} |
126
|
|
|
} |
127
|
|
|
|