|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* Copyright (c) 2018 - present |
|
5
|
|
|
* Google Maps PHP - Geocoding.php |
|
6
|
|
|
* author: Roberto Belotti - [email protected] |
|
7
|
|
|
* web : robertobelotti.com, github.com/biscolab |
|
8
|
|
|
* Initial version created on: 5/9/2018 |
|
9
|
|
|
* MIT license: https://github.com/biscolab/google-maps-php/blob/master/LICENSE |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace Biscolab\GoogleMaps\Api; |
|
13
|
|
|
|
|
14
|
|
|
use Biscolab\GoogleMaps\Abstracts\Api; |
|
15
|
|
|
use Biscolab\GoogleMaps\Fields\GoogleMapsRequestFields; |
|
16
|
|
|
use Biscolab\GoogleMaps\Http\GoogleMapsResultsCollection; |
|
17
|
|
|
use Biscolab\GoogleMaps\Http\Result\GeocodingResultsCollection; |
|
18
|
|
|
use Biscolab\GoogleMaps\Object\LatLng; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* Class Geocoding |
|
22
|
|
|
* @package Biscolab\GoogleMaps\Api |
|
23
|
|
|
* |
|
24
|
|
|
* @see https://developers.google.com/maps/documentation/geocoding/start |
|
25
|
|
|
*/ |
|
26
|
|
|
class Geocoding extends Api |
|
27
|
|
|
{ |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* The language in which to return results |
|
31
|
|
|
* https://developers.google.com/maps/faq#languagesupport |
|
32
|
|
|
* |
|
33
|
|
|
* @var null|string |
|
34
|
|
|
*/ |
|
35
|
|
|
private $language = null; |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* @var string |
|
39
|
|
|
*/ |
|
40
|
|
|
const SERVICE_ENDPOINT = 'geocode'; |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* @var string |
|
44
|
|
|
*/ |
|
45
|
|
|
protected $result_collection_type = GeocodingResultsCollection::class; |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* Set the language in which to return results |
|
49
|
|
|
* |
|
50
|
|
|
* @param null|string $language |
|
51
|
|
|
* @return self |
|
52
|
|
|
*/ |
|
53
|
1 |
|
public function setLanguage(?string $language = null): self |
|
54
|
|
|
{ |
|
55
|
1 |
|
$this->language = $language; |
|
56
|
1 |
|
return $this; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* Add language to $params list if necessary |
|
61
|
|
|
* |
|
62
|
|
|
* @param array $params |
|
63
|
|
|
* @return array |
|
64
|
|
|
* @since 0.8.0 |
|
65
|
|
|
*/ |
|
66
|
1 |
|
public function prepareParams(array $params): array |
|
67
|
|
|
{ |
|
68
|
1 |
|
if ($this->language) { |
|
69
|
1 |
|
$params[GoogleMapsRequestFields::LANGUAGE] = $this->language; |
|
70
|
|
|
} |
|
71
|
1 |
|
return $params; |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* Geocoding (Address Lookup) |
|
76
|
|
|
* |
|
77
|
|
|
* @param string $literal_address |
|
78
|
|
|
* @param string $region |
|
79
|
|
|
* |
|
80
|
|
|
* @return GoogleMapsResultsCollection |
|
81
|
|
|
*/ |
|
82
|
|
|
public function getByAddress(string $literal_address, ?string $region = null): GoogleMapsResultsCollection |
|
83
|
|
|
{ |
|
84
|
|
|
$params = [ |
|
85
|
|
|
GoogleMapsRequestFields::ADDRESS => $literal_address |
|
86
|
|
|
]; |
|
87
|
|
|
if ($region) { |
|
88
|
|
|
$params[GoogleMapsRequestFields::REGION] = $region; |
|
89
|
|
|
} |
|
90
|
|
|
$params = $this->prepareParams($params); |
|
91
|
|
|
return $this->callApi($params); |
|
|
|
|
|
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* ALIAS: getByLatLng |
|
96
|
|
|
* Geocoding (Latitude/Longitude Lookup) |
|
97
|
|
|
* |
|
98
|
|
|
* @param LatLng $latlng |
|
99
|
|
|
* |
|
100
|
|
|
* @return GoogleMapsResultsCollection |
|
101
|
|
|
* @deprecated 0.8.0 |
|
102
|
|
|
*/ |
|
103
|
|
|
public function getReverse(LatLng $latlng): GoogleMapsResultsCollection |
|
104
|
|
|
{ |
|
105
|
|
|
return $this->getByLatLng($latlng); |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
/** |
|
109
|
|
|
* Geocoding (Latitude/Longitude Lookup) |
|
110
|
|
|
* |
|
111
|
|
|
* @param LatLng $latlng |
|
112
|
|
|
* |
|
113
|
|
|
* @return GoogleMapsResultsCollection |
|
114
|
|
|
* @since 0.8.0 |
|
115
|
|
|
*/ |
|
116
|
|
|
public function getByLatLng(LatLng $latlng): GoogleMapsResultsCollection |
|
117
|
|
|
{ |
|
118
|
|
|
$params = $this->prepareParams([ |
|
119
|
|
|
GoogleMapsRequestFields::LATLNG => $latlng |
|
120
|
|
|
]); |
|
121
|
|
|
return $this->callApi($params); |
|
|
|
|
|
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
/** |
|
125
|
|
|
* Retrieving an Address for a Place ID |
|
126
|
|
|
* |
|
127
|
|
|
* @param string $place_id |
|
128
|
|
|
* |
|
129
|
|
|
* @return GoogleMapsResultsCollection |
|
130
|
|
|
*/ |
|
131
|
|
|
public function getByPlaceId(string $place_id): GoogleMapsResultsCollection |
|
132
|
|
|
{ |
|
133
|
|
|
return $this->callApi([ |
|
|
|
|
|
|
134
|
|
|
GoogleMapsRequestFields::PLACE_ID => $place_id |
|
135
|
|
|
]); |
|
136
|
|
|
} |
|
137
|
|
|
} |
|
138
|
|
|
|