Skip to content

Commit bc6472c

Browse files
hellozachfreekmurze
authored andcommitted
Add bounds to request payload (spatie#42)
* Add bounds param to request payload * Add API key .env support in tests * Add bounds param to request payload
1 parent c71af0d commit bc6472c

File tree

5 files changed

+37
-1
lines changed

5 files changed

+37
-1
lines changed

README.md

+7
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,13 @@ return [
9292
*/
9393
'region' => '',
9494

95+
/*
96+
* The bounds param used to finetune the geocoding process.
97+
*
98+
* More info: https://developers.google.com/maps/documentation/geocoding/intro#Viewports
99+
*/
100+
'bounds' => '',
101+
95102
];
96103
```
97104

config/geocoder.php

+7
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,11 @@
2222
*/
2323
'region' => '',
2424

25+
/*
26+
* The bounds param used to finetune the geocoding process.
27+
*
28+
* More info: https://developers.google.com/maps/documentation/geocoding/intro#Viewports
29+
*/
30+
'bounds' => '',
31+
2532
];

src/Geocoder.php

+11
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@ class Geocoder
2424
/** @var string */
2525
protected $region;
2626

27+
/** @var string */
28+
protected $bounds;
29+
2730
public function __construct(Client $client)
2831
{
2932
$this->client = $client;
@@ -50,6 +53,13 @@ public function setRegion(string $region)
5053
return $this;
5154
}
5255

56+
public function setBounds(string $bounds)
57+
{
58+
$this->bounds = $bounds;
59+
60+
return $this;
61+
}
62+
5363
public function getCoordinatesForAddress(string $address): array
5464
{
5565
if (empty($address)) {
@@ -119,6 +129,7 @@ protected function getRequestPayload(array $parameters): array
119129
'key' => $this->apiKey,
120130
'language' => $this->language,
121131
'region' => $this->region,
132+
'bounds' => $this->bounds,
122133
], $parameters);
123134

124135
return ['query' => $parameters];

src/GeocoderServiceProvider.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@ public function register()
2222
return (new Geocoder(new Client))
2323
->setApiKey(config('geocoder.key'))
2424
->setLanguage(config('geocoder.language'))
25-
->setRegion(config('geocoder.region'));
25+
->setRegion(config('geocoder.region'))
26+
->setBounds(config('geocoder.bounds'));
2627
});
2728
}
2829
}

tests/GeocoderTest.php

+10
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,16 @@ public function it_can_translate_coordinates_to_an_address()
8181
$this->assertEquals('277 Bedford Ave, Brooklyn, NY 11211, Verenigde Staten', $result['formatted_address']);
8282
}
8383

84+
/** @test */
85+
public function it_should_prefer_a_neighborhood_inside_of_payload_bounds()
86+
{
87+
$results = $this->geocoder
88+
->setBounds('34.172684,-118.604794|34.236144,-118.500938')
89+
->getCoordinatesForAddress('Winnetka');
90+
91+
$this->assertEquals('Winnetka, Los Angeles, CA, USA', $results['formatted_address']);
92+
}
93+
8494
protected function emptyResponse(): array
8595
{
8696
return [

0 commit comments

Comments
 (0)