Skip to Content

sanitizeGooglePlaceData()

Easily reorganize Google place data into a named object.

JavaScript November 2, 2017

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');
});

Source File

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

JavaScript
nebula.sanitizeGooglePlaceData = function(place, uniqueID){
    if ( !place ){
        console.error('Place data is required for sanitization.');
        return false;
    }

    if ( !uniqueID ){
        uniqueID = 'unnamed';
    }

    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 ( var i = 0; i < place.address_components.length; i++ ){
        //Lots of different address types. This function uses only the common ones: https://developers.google.com/maps/documentation/geocoding/#Types
        switch ( place.address_components[i].types[0] ){
            case "street_number":
                nebula.user.address[uniqueID].street.number = place.address_components[i].short_name; //123
                break;
            case "route":
                nebula.user.address[uniqueID].street.name = place.address_components[i].long_name; //Street Name Rd.
                break;
            case "locality":
                nebula.user.address[uniqueID].city = place.address_components[i].long_name; //Liverpool
                break;
            case "administrative_area_level_2":
                nebula.user.address[uniqueID].county = place.address_components[i].long_name; //Onondaga County
                break;
            case "administrative_area_level_1":
                nebula.user.address[uniqueID].state.name = place.address_components[i].long_name; //New York
                nebula.user.address[uniqueID].state.abbr = place.address_components[i].short_name; //NY
                break;
            case "country":
                nebula.user.address[uniqueID].country.name = place.address_components[i].long_name; //United States
                nebula.user.address[uniqueID].country.abbr = place.address_components[i].short_name; //US
                break;
            case "postal_code":
                nebula.user.address[uniqueID].zip.code = place.address_components[i].short_name; //13088
                break;
            case "postal_code_suffix":
                nebula.user.address[uniqueID].zip.suffix = place.address_components[i].short_name; //4725
                break;
            default:
                //console.log('Address component ' + place.address_components[i].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.

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