1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Copyright (c) 2018 - present |
4
|
|
|
* Google Maps PHP - Elevation.php |
5
|
|
|
* author: Roberto Belotti - [email protected] |
6
|
|
|
* web : robertobelotti.com, github.com/biscolab |
7
|
|
|
* Initial version created on: 28/9/2018 |
8
|
|
|
* MIT license: https://github.com/biscolab/google-maps-php/blob/master/LICENSE |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace Biscolab\GoogleMaps\Api; |
12
|
|
|
|
13
|
|
|
use Biscolab\GoogleMaps\Abstracts\Api; |
14
|
|
|
use Biscolab\GoogleMaps\Exception\InvalidArgumentException; |
15
|
|
|
use Biscolab\GoogleMaps\Fields\GoogleMapsRequestFields; |
16
|
|
|
use Biscolab\GoogleMaps\Http\GoogleMapsResultsCollection; |
17
|
|
|
use Biscolab\GoogleMaps\Http\Result\ElevationResultsCollection; |
18
|
|
|
use Biscolab\GoogleMaps\Object\LatLng; |
19
|
|
|
use Biscolab\GoogleMaps\Object\Path; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Class Elevation |
23
|
|
|
* @package Biscolab\GoogleMaps\Api |
24
|
|
|
* |
25
|
|
|
* @since 0.3.0 |
26
|
|
|
* @see https://developers.google.com/maps/documentation/elevation/start |
27
|
|
|
*/ |
28
|
|
|
class Elevation extends Api |
29
|
|
|
{ |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var string |
33
|
|
|
*/ |
34
|
|
|
const SERVICE_ENDPOINT = 'elevation'; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @var string |
38
|
|
|
*/ |
39
|
|
|
protected $result_collection_type = ElevationResultsCollection::class; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Positional Requests |
43
|
|
|
* |
44
|
|
|
* @param LatLng|string|array $locations |
45
|
|
|
* This parameter takes either a single location or multiple locations passed as an array or as an encoded polyline |
46
|
|
|
* |
47
|
|
|
* @return GoogleMapsResultsCollection |
48
|
|
|
* |
49
|
|
|
* @since 0.3.0 |
50
|
|
|
*/ |
51
|
|
|
public function getByLocations($locations): GoogleMapsResultsCollection |
52
|
|
|
{ |
53
|
|
|
|
54
|
|
|
$locations = $this->parseLocations($locations); |
55
|
|
|
|
56
|
|
|
return $this->callApi([ |
|
|
|
|
57
|
|
|
GoogleMapsRequestFields::LOCATIONS => $locations |
58
|
|
|
]); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @param array|string $locations |
63
|
|
|
* |
64
|
|
|
* @return string |
65
|
|
|
* |
66
|
|
|
* @since 0.3.0 |
67
|
|
|
*/ |
68
|
2 |
|
public function parseLocations($locations): string |
69
|
|
|
{ |
70
|
|
|
|
71
|
2 |
|
if ($locations instanceof Path) { |
|
|
|
|
72
|
|
|
$locations = $locations->toArray(); |
73
|
|
|
} |
74
|
|
|
|
75
|
2 |
|
if (is_array($locations)) { |
76
|
|
|
$locations = implode('|', array_map(function ($item) { |
77
|
|
|
|
78
|
1 |
|
return (string)$item; |
79
|
1 |
|
}, $locations)); |
80
|
|
|
} |
81
|
|
|
|
82
|
2 |
|
return (string)$locations; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Sampled Path Requests |
87
|
|
|
* |
88
|
|
|
* @param array|string $path |
89
|
|
|
* This parameter takes either a multiple locations passed as an array or as an encoded polyline |
90
|
|
|
* |
91
|
|
|
* @param int $samples |
92
|
|
|
* This will be the number of results as well |
93
|
|
|
* |
94
|
|
|
* @throws InvalidArgumentException |
95
|
|
|
* @return GoogleMapsResultsCollection |
96
|
|
|
* |
97
|
|
|
* @since 0.4.0 |
98
|
|
|
*/ |
99
|
3 |
|
public function getBySampledPath($path, int $samples): GoogleMapsResultsCollection |
100
|
|
|
{ |
101
|
|
|
|
102
|
3 |
|
if ((is_array($path) && count($path) < 2) || |
103
|
3 |
|
$path instanceof Path && $path->count() < 2) { |
104
|
3 |
|
throw new InvalidArgumentException('The number of items provided in the path must be greater than 1 (One)'); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
if ($samples <= 0) { |
108
|
|
|
throw new InvalidArgumentException('The number of samples must be greater than 0 (Zero)'); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
$path = $this->parseLocations($path); |
112
|
|
|
|
113
|
|
|
return $this->callApi([ |
|
|
|
|
114
|
|
|
GoogleMapsRequestFields::PATH => $path, |
115
|
|
|
GoogleMapsRequestFields::SAMPLES => $samples, |
116
|
|
|
]); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
} |