Skip to Content

googleAddressAutocompleteCallback()

Actually queries the Google Maps address API.

JavaScript November 2, 2017

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.

Source File

Located in /assets/js/nebula.js on line 3033.

JavaScript
nebula.googleAddressAutocompleteCallback = function(autocompleteInput, uniqueID){
    if ( typeof uniqueID === 'undefined' || uniqueID === 'undefined' ){
        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.
        var place = window[uniqueID].getPlace(); //Get the place details from the window[uniqueID] object.
        var simplePlace = nebula.sanitizeGooglePlaceData(place, uniqueID);

        var 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.nv('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.
                var geolocation = new google.maps.LatLng(position.coords.latitude, position.coords.longitude);
                var circle = new google.maps.Circle({
                    center: geolocation,
                    radius: position.coords.accuracy
                });
                window[uniqueID].setBounds(circle.getBounds());
            });
        }
    }).on('keydown', function(e){
        if ( e.which === 13 && 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.
}