Skip to Content
Menu

googleAddressAutocompleteCallback()

Actually queries the Google Maps address API.

JavaScript February 7, 2021

Usage

This function runs automatically, so it is not called manually. Is this incorrect?

Demo


Demo file is missing! Please report this error.

Additional Notes

Instead of calling this function directly, please use nebulaAddressAutocomplete() so that the Google Maps library can be loaded as needed.

Was this page helpful? Yes No


    A feedback message is required to submit this form.


    Please check that you have entered a valid email address.

    Enter your email address if you would like a response.

    Thank you for your feedback!

    Source File

    Located in /assets/js/modules/location.js on line 34.

    No Hooks

    This function does not have any filters or actions available. Request one?
    JavaScript
    nebula.googleAddressAutocompleteCallback = function(autocompleteInput, uniqueID = 'unnamed'){
        window[uniqueID] = new google.maps.places.Autocomplete(
            jQuery(autocompleteInput)[0],
            {types: ['geocode']} //Restrict the search to geographical location types
        );
    
        google.maps.event.addListener(window[uniqueID], 'place_changed', function(){ //When the user selects an address from the dropdown, populate the address fields in the form.
            let place = window[uniqueID].getPlace(); //Get the place details from the window[uniqueID] object.
            let simplePlace = nebula.sanitizeGooglePlaceData(place, uniqueID);
    
            let thisEvent = {
                category: 'Contact',
                action: 'Autocomplete Address',
                intent: 'Intent',
                place: place,
                simplePlace: simplePlace
            };
    
            nebula.dom.document.trigger('nebula_address_selected', [place, simplePlace, jQuery(autocompleteInput)]);
            nebula.dom.document.trigger('nebula_event', thisEvent);
            ga('set', nebula.analytics.dimensions.contactMethod, thisEvent.action);
            ga('send', 'event', 'Contact', 'Autocomplete Address', simplePlace.city + ', ' + simplePlace.state.abbr + ' ' + simplePlace.zip.code);
    
            nebula.crm('identify', {
                'street_number': simplePlace.street.number,
                'street_name': simplePlace.street.name,
                'street_full': simplePlace.street.full,
                'city': simplePlace.city,
                'county': simplePlace.county,
                'state': simplePlace.state.name,
                'country': simplePlace.country.name,
                'zip': simplePlace.zip.code,
                'address': simplePlace.street.full + ', ' + simplePlace.city + ', ' + simplePlace.state.abbr + ' ' + simplePlace.zip.code
            });
        });
    
        jQuery(autocompleteInput).on('focus', function(){
            if ( nebula.site.protocol === 'https' && navigator.geolocation ){
                navigator.geolocation.getCurrentPosition(function(position){ //Bias to the user's geographical location.
                    let geolocation = new google.maps.LatLng(position.coords.latitude, position.coords.longitude);
                    let circle = new google.maps.Circle({
                        center: geolocation,
                        radius: position.coords.accuracy
                    });
                    window[uniqueID].setBounds(circle.getBounds());
                });
            }
        }).on('keydown', function(e){
            if ( e.key === 'Enter' && jQuery('.pac-container:visible').length ){ //Prevent form submission when enter key is pressed while the "Places Autocomplete" container is visbile
                return false;
            }
        });
    
        if ( autocompleteInput === '#address-autocomplete' ){
            nebula.dom.document.on('nebula_address_selected', function(){
                //do any default stuff here.
            });
        }
    };
    

    Override

    To override or disable this JavaScript function, simply redeclare it with the exact same function name.

    JavaScript
    nebula.googleAddressAutocompleteCallback = function(){
        //Write your own code here, leave it blank, or return false.
    }