Skip to Content
Menu

sanitizeGooglePlaceData()

Easily reorganize Google place data into a named object.

JavaScript February 7, 2021

Usage

JavaScript
sanitizeGooglePlaceData(place, uniqueID)

Parameters

place
(Required) (object) The Google Maps place data
Default: None

uniqueID
(Optional) (string) Use this to name the address info
Default: "unnamed"

Request or provide clarification »

Examples

Using the Google Maps autocomplete and sanitizing place data

JavaScript
customMapsAutocomplete = new google.maps.places.Autocomplete(
    jQuery('input#mailing-address')[0],
    {types: ['geocode']} //Restrict the search to geographical location types
);

google.maps.event.addListener(customMapsAutocomplete, 'place_changed', function(){ //When the user selects an address from the dropdown
    place = customMapsAutocomplete.getPlace(); //Get the place details
    simplePlace = sanitizeGooglePlaceData(place, 'mailing');
});
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 96.

    No Hooks

    This function does not have any filters or actions available. Request one?
    JavaScript
    nebula.sanitizeGooglePlaceData = function(place = false, uniqueID = 'unnamed'){
        if ( !place ){
            nebula.help('Place data is required for sanitization.', '/functions/sanitizegoogleplacedata/');
            return false;
        }
    
        if ( typeof nebula.user.address === 'undefined' ){
            nebula.user.address = {};
        }
    
        if ( !Array.isArray(nebula.user.address) ){
            nebula.user.address = [];
        }
    
        nebula.user.address[uniqueID] = {
            street: {
                number: null,
                name: null
            },
            city: null,
            county: null,
            state: {
                name: null,
                abbr: null
            },
            country: {
                name: null,
                abbr: null
            },
            zip: {
                code: null,
                suffix: null
            }
        };
    
        for ( let component of place.address_components ){
            //Lots of different address types. This function uses only the common ones: https://developers.google.com/maps/documentation/geocoding/#Types
            switch ( component.types[0] ){
                case 'street_number':
                    nebula.user.address[uniqueID].street.number = component.short_name; //123
                    break;
                case 'route':
                    nebula.user.address[uniqueID].street.name = component.long_name; //Street Name Rd.
                    break;
                case 'locality':
                    nebula.user.address[uniqueID].city = component.long_name; //Liverpool
                    break;
                case 'administrative_area_level_2':
                    nebula.user.address[uniqueID].county = component.long_name; //Onondaga County
                    break;
                case 'administrative_area_level_1':
                    nebula.user.address[uniqueID].state.name = component.long_name; //New York
                    nebula.user.address[uniqueID].state.abbr = component.short_name; //NY
                    break;
                case 'country':
                    nebula.user.address[uniqueID].country.name = component.long_name; //United States
                    nebula.user.address[uniqueID].country.abbr = component.short_name; //US
                    break;
                case 'postal_code':
                    nebula.user.address[uniqueID].zip.code = component.short_name; //13088
                    break;
                case 'postal_code_suffix':
                    nebula.user.address[uniqueID].zip.suffix = component.short_name; //4725
                    break;
                default:
                    //console.log('Address component ' + component.types[0] + ' not used.');
            }
        }
    
        if ( nebula.user.address[uniqueID].street.number && nebula.user.address[uniqueID].street.name ){
            nebula.user.address[uniqueID].street.full = nebula.user.address[uniqueID].street.number + ' ' + nebula.user.address[uniqueID].street.name;
        }
    
        if ( nebula.user.address[uniqueID].zip.code && nebula.user.address[uniqueID].zip.suffix ){
            nebula.user.address[uniqueID].zip.full = nebula.user.address[uniqueID].zip.code + '-' + nebula.user.address[uniqueID].zip.suffix;
        }
    
        return nebula.user.address[uniqueID];
    };
    

    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.sanitizeGooglePlaceData = function(place, uniqueID){
        //Write your own code here, leave it blank, or return false.
    }


    For dynamically imported module function overrides:

    jQuery(window).on('load', function(){
        nebula.sanitizeGooglePlaceData = function(place, uniqueID){
            //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.sanitizeGooglePlaceData === 'function' ){
            nebula.sanitizeGooglePlaceData = function(place, uniqueID){
                //Write your own code here, leave it blank, or return false.
            }
    	}
    });