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