1
|
|
|
<?php namespace Arcanedev\GeoLocation\Google\Elevation; |
2
|
|
|
|
3
|
|
|
use Arcanedev\GeoLocation\Contracts\Entities\Coordinates\Position as PositionContract; |
4
|
|
|
use Arcanedev\GeoLocation\Google\AbstractService; |
5
|
|
|
use GuzzleHttp\ClientInterface; |
6
|
|
|
use Psr\Http\Message\ResponseInterface; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Class ElevationService |
10
|
|
|
* |
11
|
|
|
* @package Arcanedev\GeoLocation\Google\Elevation |
12
|
|
|
* @author ARCANEDEV <[email protected]> |
13
|
|
|
*/ |
14
|
|
|
class ElevationService extends AbstractService |
15
|
|
|
{ |
16
|
|
|
/* ----------------------------------------------------------------- |
17
|
|
|
| Constants |
18
|
|
|
| ----------------------------------------------------------------- |
19
|
|
|
*/ |
20
|
|
|
|
21
|
|
|
const BASE_URL = 'https://maps.googleapis.com/maps/api/elevation/json'; |
22
|
|
|
|
23
|
|
|
/* ----------------------------------------------------------------- |
24
|
|
|
| Constructor |
25
|
|
|
| ----------------------------------------------------------------- |
26
|
|
|
*/ |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* ElevationService constructor. |
30
|
|
|
* |
31
|
|
|
* @param \GuzzleHttp\ClientInterface $client |
32
|
|
|
*/ |
33
|
12 |
|
public function __construct(ClientInterface $client) |
34
|
|
|
{ |
35
|
12 |
|
parent::__construct($client); |
36
|
|
|
|
37
|
12 |
|
$this->setKey(getenv('GOOGLE_MAPS_ELEVATION_KEY')); |
38
|
12 |
|
} |
39
|
|
|
|
40
|
|
|
/* ----------------------------------------------------------------- |
41
|
|
|
| Main Methods |
42
|
|
|
| ----------------------------------------------------------------- |
43
|
|
|
*/ |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Get the geocoding response. |
47
|
|
|
* |
48
|
|
|
* @param \Arcanedev\GeoLocation\Contracts\Entities\Coordinates\Position $position |
49
|
|
|
* @param array $options |
50
|
|
|
* |
51
|
|
|
* @return \Arcanedev\GeoLocation\Google\Elevation\ElevationResponse |
52
|
|
|
*/ |
53
|
6 |
|
public function location(PositionContract $position, array $options = []) |
54
|
|
|
{ |
55
|
6 |
|
$url = static::BASE_URL.$this->prepareQuery([ |
56
|
6 |
|
'locations' => $this->parsePosition($position), |
57
|
2 |
|
]); |
58
|
|
|
|
59
|
6 |
|
return $this->get($url, $options); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Reverse geocoding (address lookup). |
64
|
|
|
* |
65
|
|
|
* @param array $positions |
66
|
|
|
* @param array $options |
67
|
|
|
* |
68
|
|
|
* @return \Arcanedev\GeoLocation\Google\Elevation\ElevationResponse |
69
|
|
|
*/ |
70
|
|
|
public function path(array $positions, array $options = []) |
71
|
|
|
{ |
72
|
3 |
|
$positions = array_map(function (PositionContract $position) { |
73
|
3 |
|
return $this->parsePosition($position); |
74
|
3 |
|
}, $positions); |
75
|
|
|
|
76
|
3 |
|
$url = static::BASE_URL.$this->prepareQuery([ |
77
|
3 |
|
'locations' => implode('|', $positions), |
78
|
1 |
|
]); |
79
|
|
|
|
80
|
3 |
|
return $this->get($url, $options); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/* ----------------------------------------------------------------- |
84
|
|
|
| Other Methods |
85
|
|
|
| ----------------------------------------------------------------- |
86
|
|
|
*/ |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Prepare the response. |
90
|
|
|
* |
91
|
|
|
* @param \Psr\Http\Message\ResponseInterface $response |
92
|
|
|
* |
93
|
|
|
* @return \Arcanedev\GeoLocation\Google\AbstractResponse |
94
|
|
|
*/ |
95
|
9 |
|
protected function prepareResponse(ResponseInterface $response) |
96
|
|
|
{ |
97
|
9 |
|
return new ElevationResponse( |
98
|
9 |
|
json_decode($response->getBody(), true) |
99
|
3 |
|
); |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
|