Skip to content

Commit

Permalink
Merge pull request #25 from wychoong/patch-1
Browse files Browse the repository at this point in the history
use autocomplete place.address_components instead of reverse geocode
  • Loading branch information
cheesegrits authored May 10, 2023
2 parents 17809ca + ec88a9e commit 0ef971f
Showing 1 changed file with 23 additions and 26 deletions.
49 changes: 23 additions & 26 deletions resources/js/filament-google-maps.js
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ window.filamentGoogleMaps = ($wire, config) => {
this.marker.setPosition(place.geometry.location);
this.markerLocation = place.geometry.location;
this.setCoordinates(place.geometry.location);
this.updateGeocode(this.markerLocation);
this.updateGeocode(place.address_components);
});
}
}
Expand Down Expand Up @@ -336,7 +336,7 @@ window.filamentGoogleMaps = ($wire, config) => {
this.markerLocation = event.latLng.toJSON();
this.setCoordinates(this.markerLocation);
this.updateAutocomplete(this.markerLocation);
this.updateGeocode(this.markerLocation);
this.updateGeocodeFromLocation(this.markerLocation);
// this.updateMap(this.markerLocation);
this.map.panTo(this.markerLocation);
},
Expand All @@ -353,32 +353,29 @@ window.filamentGoogleMaps = ($wire, config) => {
this.marker.setPosition(position);
this.map.panTo(position);
},
updateGeocode: function (position) {
if (Object.keys(this.config.reverseGeocodeFields).length > 0) {
this.geocoder
.geocode({location: position})
.then((response) => {
if (response.results[0]) {
//$wire.set(config.autocomplete, response.results[0].formatted_address);
const replacements = this.getReplacements(response.results[0].address_components);
updateGeocode: function (address_components) {
const replacements = this.getReplacements(address_components);

for (const field in this.config.reverseGeocodeFields) {
let replaced = this.config.reverseGeocodeFields[field];

for (const replacement in replacements) {
replaced = replaced.split(replacement).join(replacements[replacement]);
}

for (const symbol in this.symbols) {
replaced = replaced.split(symbol).join('');
}
for (const field in this.config.reverseGeocodeFields) {
let replaced = this.config.reverseGeocodeFields[field];
for (const replacement in replacements) {
replaced = replaced.split(replacement).join(replacements[replacement]);
}

replaced = replaced.trim();
$wire.set(field, replaced)
}
for (const symbol in this.symbols) {
replaced = replaced.split(symbol).join('');
}

}
})
replaced = replaced.trim();
$wire.set(field, replaced)
}
},
updateGeocodeFromLocation: function (location) {
if (Object.keys(this.config.reverseGeocodeFields).length > 0) {
this.geocoder
.geocode({ location })
.then((response) => response.results[0].address_components)
.then((address_components) => this.updateGeocode(address_components))
.catch((error) => {
console.log(error.message);
})
Expand Down Expand Up @@ -416,7 +413,7 @@ window.filamentGoogleMaps = ($wire, config) => {
};
this.setCoordinates(this.markerLocation);
this.updateAutocomplete(this.markerLocation);
this.updateGeocode(this.markerLocation);
this.updateGeocodeFromLocation(this.markerLocation);
this.map.panTo(this.markerLocation);
});
},
Expand Down

0 comments on commit 0ef971f

Please sign in to comment.