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 = {
                event_name: 'autocomplete_address',
                event_category: 'Contact',
                event_action: 'Autocomplete Address',
                event_label: simplePlace.city + ', ' + simplePlace.state.abbr + ' ' + simplePlace.zip.code,
                place: place,
                simple_place: simplePlace
            };
    
            nebula.dom.document.trigger('nebula_address_selected', [place, simplePlace, jQuery(autocompleteInput)]);
            nebula.dom.document.trigger('nebula_event', thisEvent);
            gtag('event', thisEvent.event_name, nebula.gaEventObject(thisEvent));
    
            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. Remember: Some functionality is conditionally loaded via dynamic imports, so if your function is not overriding properly, try listening for a DOM event (described below).

    JavaScript

    For non-module import functions:

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


    For dynamically imported module function overrides:

    jQuery(window).on('load', function(){
        nebula.googleAddressAutocompleteCallback = function(){
            //Write your own code here, leave it blank, or return false.
        }
    });


    Custom Nebula DOM events do also exist, so you could also try the following if the Window Load listener does not work:

    jQuery(document).on('nebula_module_loaded', function(module){
        //Note that the module variable is also available to know which module specifically was imported
        if ( typeof nebula.googleAddressAutocompleteCallback === 'function' ){
            nebula.googleAddressAutocompleteCallback = function(){
                //Write your own code here, leave it blank, or return false.
            }
    	}
    });