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