diff --git a/geolocation.libraries.yml b/geolocation.libraries.yml index 6dbfe2a..1de7dcc 100644 --- a/geolocation.libraries.yml +++ b/geolocation.libraries.yml @@ -44,4 +44,4 @@ geolocation.commonmap: https://maps.googleapis.com/maps/api/js: { type: external } js/geolocation-common-map.js: {} dependencies: - - core/jquery + - geolocation/geolocation.core diff --git a/js/geolocation.js b/js/geolocation.js index d49ace8..8d831da 100644 --- a/js/geolocation.js +++ b/js/geolocation.js @@ -151,6 +151,9 @@ geolocation.codeLatLng(results[0].geometry.location, map); // Set the map marker. geolocation.setMapMarker(results[0].geometry.location, map); + + // Populate fields provided by the address module. + geolocation.populateAddress(results[0]); } else { // Alert of the error geocoding. @@ -205,6 +208,72 @@ }); }; + geolocation.populateAddress = function(result) { + var addressLine1 = '', + addressLine2 = '', + postalTown = ''; + var $addressField = $('.field--type-address'); + + if ($addressField.length) { + for (var x in result.address_components) { + var component = result.address_components[x]; + var types = component.types; + + switch (types[0]) { + case 'country': + var countryCode = component.short_name; + break; + case 'postal_town': + postalTown = component.long_name; + break; + case 'postal_code': + var postalCode = component.long_name; + break; + case 'street_number': + var streetNumber = component.long_name; + break; + case 'premise': + var premise = component.long_name; + break; + case 'route': + var route = component.long_name; + break; + case 'locality': + var locality = component.long_name; + break; + case 'administrative_area_level_2': + var administrativeArea = component.long_name; + break; + } + } + + // Set the country. + $addressField.find('.country.form-select').val(countryCode).trigger('change'); + + if (!!streetNumber) { + addressLine1 = streetNumber + ' ' + route; + } + else { + addressLine1 = route; + } + + if (!!locality && locality != postalTown) { + addressLine2 = locality; + } + + $(document).ajaxComplete(function() { + var $addressDetails = $('.field--type-address .details-wrapper'); + // Populate the address fields, once they have been added to the DOM. + $addressDetails.find('.organization').val(premise); + $addressDetails.find('.address-line1').val(addressLine1); + $addressDetails.find('.address-line2').val(addressLine2); + $addressDetails.find('.locality').val(postalTown); + $addressDetails.find('.administrative-area').val(administrativeArea); + $addressDetails.find('.postal-code').val(postalCode); + }); + } + }; + /** * Set the latitude and longitude values to the input fields * And optionally update the address field @@ -261,6 +330,14 @@ } } + // Refresh the address field. + map.geocoder.geocode({'location': latLng}, function (results, status) { + if (status == google.maps.GeocoderStatus.OK) { + // Populate fields provided by the address module. + geolocation.populateAddress(results[0]); + } + }); + // Add a visual indicator. $(map.controls).children('.geolocation-map-indicator') .text(latLng.lat()+', '+latLng.lng())