Skip to content
This repository was archived by the owner on Nov 21, 2024. It is now read-only.

Commit

Permalink
Adds the ability to define the short/long name version of the geocode…
Browse files Browse the repository at this point in the history
…d object.
  • Loading branch information
dniccum committed Oct 7, 2020
1 parent a3f2d02 commit 35b5cde
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 6 deletions.
22 changes: 22 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@ AddressMetadata::make('long')->fromValue('longitude')->disabled(),
AddressMetadata::make('long')->fromValue('longitude')->invisible(),
```

### Combine Values

If you want to concatenate certain elements of the geocoded object that is returned by Google, using `{{` and `}}`, wrap the key like you would above; like so:

```php
Expand All @@ -91,6 +93,26 @@ So the value that would be rendered within the coordinates input would be someth
39.3315476, -94.9363912
```

### Define Short/Long Value

If you would like to use the **long_name** version of the geocoded object (Kansas versus KS), you can define the `GoogleAutocomplete` field values with dot notation followed with the name version you want to use; like so:

```php
use EmilianoTisato\GoogleAutocomplete\GoogleAutocomplete;

GoogleAutocomplete::make('Address')
->withValues([
'route.short_name',
'administrative_area_level_1.long_name',
]),
```

Which would return:

**route:** W 143rd St

**administrative_area_level_1:** Kansas

You can change the type of places that are returned by the autocomplete using the placeType() method. You can use any of the values listed at [https://developers.google.com/places/supported_types#table3](https://developers.google.com/places/supported_types#table3)

```php
Expand Down
2 changes: 1 addition & 1 deletion dist/js/field.js

Large diffs are not rendered by default.

24 changes: 19 additions & 5 deletions resources/js/components/GoogleAutocomplete/FormField.vue
Original file line number Diff line number Diff line change
Expand Up @@ -69,11 +69,25 @@ export default {
// Emmit events to by catch up for the other AddressMetadata fields
this.field.addressObject.forEach(element => {
if(addressData.hasOwnProperty(element)) {
retrievedAddress[element] = addressData[element];
}
if(placeResultData.hasOwnProperty(element)) {
retrievedAddress[element] = placeResultData[element];
if (element.indexOf('.') < 0) {
if(addressData.hasOwnProperty(element)) {
retrievedAddress[element] = addressData[element];
}
if(placeResultData.hasOwnProperty(element)) {
retrievedAddress[element] = placeResultData[element];
}
} else {
// Separates the type
let value = element.split(".")[0];
let type = element.split(".")[1]; // long_name or short_name
for(let i = 0; i < placeResultData.address_components.length; i++) {
let target = placeResultData.address_components[i];
if (target.types.includes(value)) {
retrievedAddress[value] = target[type];
}
}
}
});
Expand Down

0 comments on commit 35b5cde

Please sign in to comment.