Skip to content

Commit 15a74f1

Browse files
committed
new public method geocodeReverse and geocodeReverseAsync
1 parent f88670c commit 15a74f1

File tree

3 files changed

+40
-2
lines changed

3 files changed

+40
-2
lines changed

README.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ print_r($result);
5959

6060
```php
6161
$geocoder = new \OpenCage\Geocoder\Geocoder('YOUR-API-KEY');
62-
$result = $geocoder->geocode('43.831,4.360'); # latitude,longitude (y,x)
62+
$result = $geocoder->geocodeReverse(43.831, 4.360); # latitude, longitude
6363
print $result['results'][0]['formatted'];
6464
// 3 Rue de Rivarol, 30020 Nîmes, France
6565
```
@@ -101,6 +101,11 @@ $results = \GuzzleHttp\Promise\Utils::unwrap($promises);
101101
print $results['london']['results'][0]['formatted'];
102102
print $results['paris']['results'][0]['formatted'];
103103
print $results['tokyo']['results'][0]['formatted'];
104+
105+
// async reverse geocoding
106+
$promise = $geocoder->geocodeReverseAsync(43.831, 4.360);
107+
$result = $promise->wait();
108+
print $result['results'][0]['formatted'];
104109
```
105110

106111
### Set a proxy URL

src/Geocoder.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,21 @@ public function geocode(string $query, array $optParams = []): array
8282
return $ret;
8383
}
8484

85+
/**
86+
* @param array<string, string> $optParams
87+
* @return array{status: array{code: int, message: string}, results: list<mixed>, total_results: int, ...}
88+
*/
89+
public function geocodeReverse(float|string $latitude, float|string $longitude, array $optParams = []): array
90+
{
91+
return $this->geocode($latitude . ',' . $longitude, $optParams);
92+
}
93+
94+
/** @param array<string, string> $optParams */
95+
public function geocodeReverseAsync(float|string $latitude, float|string $longitude, array $optParams = []): PromiseInterface
96+
{
97+
return $this->geocodeAsync($latitude . ',' . $longitude, $optParams);
98+
}
99+
85100
/** @param array<string, string> $optParams */
86101
public function geocodeAsync(string $query, array $optParams = []): PromiseInterface
87102
{

tests/GeocoderTest.php

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,11 +112,29 @@ public function testWithOptionalParams(): void
112112
public function testReverseGeocode(): void
113113
{
114114
$geocoder = new Geocoder(self::OPENCAGE_TEST_APIKEY_200);
115-
$result = $geocoder->geocode('51.5227,-0.1025');
115+
$result = $geocoder->geocodeReverse(51.5227, -0.1025);
116116
$this->assertEquals(200, $result['status']['code']);
117117
$this->assertGreaterThan(0, $result['total_results']);
118118
}
119119

120+
public function testReverseGeocodeWithStrings(): void
121+
{
122+
$geocoder = new Geocoder(self::OPENCAGE_TEST_APIKEY_200);
123+
$result = $geocoder->geocodeReverse('51.5227', '-0.1025');
124+
$this->assertEquals(200, $result['status']['code']);
125+
$this->assertGreaterThan(0, $result['total_results']);
126+
}
127+
128+
public function testReverseGeocodeAsync(): void
129+
{
130+
$geocoder = new Geocoder(self::OPENCAGE_TEST_APIKEY_200);
131+
$promise = $geocoder->geocodeReverseAsync(51.5227, -0.1025);
132+
/** @var array{status: array{code: int, message: string}} $result */
133+
$result = $promise->wait();
134+
135+
$this->assertEquals(200, $result['status']['code']);
136+
}
137+
120138
public function testInvalidProxy(): void
121139
{
122140
$geocoder = new Geocoder(self::OPENCAGE_TEST_APIKEY_200);

0 commit comments

Comments
 (0)