CosminV
4/13/2020 - 11:06 AM

code

<script>
    var newPageGenerator = !!window.__page_generator;
    var placeSearch, autocomplete;
    // $( 'input[name="'+ base64_encode( 'Address' ) +'"]')
    var componentForm = {
        // street_number: 'short_name',
        // route: 'long_name',
        City: 'long_name',
        State: 'short_name',
        // country: 'long_name',
        Zip: 'short_name'
    };
    function base64_encode(str) {
        if (newPageGenerator) {
            return str;
        } else {
            return btoa(str);
        }

    }
    function initMap() {
        // Create the autocomplete object, restricting the search predictions to
        // geographical location types.
        autocomplete = new google.maps.places.Autocomplete(
            document.getElementsByName(base64_encode( 'Address' ))[0], {types: ['geocode']});

        // Avoid paying for data that you don't need by restricting the set of
        // place fields that are returned to just the address components.
        autocomplete.setComponentRestrictions(
            {'country': ['za']});
        autocomplete.setFields(['address_component']);

        // When the user selects an address from the drop-down, populate the
        // address fields in the form.
        autocomplete.addListener('place_changed', fillInAddress);
    }

    function fillInAddress() {
        // Get the place details from the autocomplete object.
        var place = autocomplete.getPlace();

        for (var component in componentForm) {
            document.getElementsByName(base64_encode(component))[0].value = '';
            document.getElementsByName(base64_encode(component))[0].disabled = false;
        }


        for (var i = 0; i < place.address_components.length; i++) {
            var addressType = place.address_components[i].types[0];
            addressType === 'locality' ? addressType = 'City' : '';
            addressType === 'administrative_area_level_1' ? addressType = 'State' : '';
            addressType === 'postal_code' ? addressType = 'Zip' : '';

            if (componentForm[addressType]) {
                var val = place.address_components[i][componentForm[addressType]];
                document.getElementsByName(base64_encode(addressType))[0].value = val;
            }
        }
    }


    function geolocate() {
        if (navigator.geolocation) {
            navigator.geolocation.getCurrentPosition(function(position) {
                var geolocation = {
                    lat: position.coords.latitude,
                    lng: position.coords.longitude
                };
                var circle = new google.maps.Circle(
                    {center: geolocation, radius: position.coords.accuracy});
                autocomplete.setBounds(circle.getBounds());
            });
        }
    }
</script>