%PDF- %PDF-
Mini Shell

Mini Shell

Direktori : /home/casasmonvl/coronamillennial/Scripts/address/
Upload File :
Create Path :
Current File : /home/casasmonvl/coronamillennial/Scripts/address/autocomplete.js

var placeSearch, autocomplete;
    var componentForm = {
        street_number: 'short_name',
        route: 'long_name',
        locality: 'long_name',
        administrative_area_level_1: 'short_name', //Code territory de google
        country: 'short_name', //long_name = Eg. España or Spain, short_name = Eg. ES (ISO-3166-1)
        postal_code: 'short_name',
    };

    function initAutocomplete() {

        // Create the autocomplete object, restricting the search to geographical
        // location types.

        autocomplete = new google.maps.places.Autocomplete(
            (document.getElementById('autocomplete')),
            { types: ['geocode'] }); //address

        //$('addressForm').clearForm();

        //Per només cities: http://stackoverflow.com/questions/28170511/get-only-city-names-with-google-maps-api-autocompletion
        //var autocomplete = new google.maps.places.Autocomplete(yourHTMLElement, { types: ['(cities)'] });

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

    function fillInAddress() {

        //Solució per ERROR 06/06/17
        //Forçar reset de territory
        var territory = '';

        $("#success_warning").hide();

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

        //Recuperem place_id (csierra 21/11/16)
        $("#place_id").val(place.place_id);
        //Recuperem coordenades lat, lng
        $("#location_lat").val(place.geometry.location.lat);
        $("#location_lng").val(place.geometry.location.lng);
        $("#formatted_address").val(place.formatted_address);


        //1- Neteja els valors del form
        for (var component in componentForm) {
            document.getElementById(component).value = '';
            //document.getElementById(component).disabled = false; //false = dona possibilitat d'editar el textbox
            document.getElementById(component).readOnly = true;
        }

        //2- Reompla els valors del form
        // Get each component of the address from the place details
        // and fill the corresponding field on the form.
        for (var i = 0; i < place.address_components.length; i++) {
            var addressType = place.address_components[i].types[0];
            if (componentForm[addressType]) {
                var val = place.address_components[i][componentForm[addressType]];
                document.getElementById(addressType).value = val;
            }
            //Afegit csierra 24/11/16 http://stackoverflow.com/questions/4013606/google-maps-how-to-get-country-state-province-region-city-given-a-lat-long-va
            //Per recuperar long_name i short_name de qualsevol component: country, territory // addressType == place.address_components[i].types[0]

            //ERROR 06/06/17 Detectat:
            //Si no tenim valor ID=administrative_area_level_1 que és el codi de la regió/estat "territory.in_aut_state"
            //NO entra aquí i per tant NO es resetejarà el valor ID=territory_name !!!!
            //Solució, defineixo 'var territory' i l'inicialitzo a null
            if (addressType == "administrative_area_level_1") {
                //recuperem region/territory
                territory = place.address_components[i];
                //alert("hola");
            }
            if (addressType == "country") {
                //recuperem país
                country = place.address_components[i];
            }
        }

        if (territory != null) {
            $("#territory_name").val(territory.long_name);
            //alert($("#territory_name").val());
        }
        if (country != null) {
            $("#country_name").val(country.long_name);
        }

        //alert($("#territory_name").val()); //

        //for afegit csierra 14/11/16 per tractar cas *DEIXAR OMPLIR CAMPS que NO ha recuperat dades de places
        //Així, impedim editar les dades recuperades de google, i només deixem editar les que manquin
        //Si s'elimina, es permetran editar TOTS els camps del LoadAddress
        var incomplet = 0; //variable que se usara para comprovar si falta algun campo a rellenar, inicialmente a 0,
        //indicando que se preve una recuperacion automatica con exito
        var missatge = "Requiere rellenar datos restantes:";

        for (var component in componentForm) {
            var x = document.getElementById(component);

            if (x.value == '') {

                //Cas que falti el numero del carrer i bé el carrer, li permetrem escriure-ho a l'usuari 
                if (x.id == 'street_number') {
                    //document.getElementById(component).readOnly = false; //Eliminat 15/12/16
                    //alert("Street Number NONE");
                    incomplet = incomplet + 1;
                }
                if (x.id == 'route') {
                    //document.getElementById(component).readOnly = false; //Eliminat 15/12/16
                    //alert("Street NONE");
                    incomplet = incomplet + 1;
                }

                //x.style.background = "#E9D98C";
                //alert(x.id); //mostra id
                //x.name: street_num, cp_num
                //x.id: street_number, postal_code
                //x.value = ""

                //document.getElementById(component).disabled = false; //false = dona possibilitat d'editar el textbox
                //document.getElementById(component).readOnly = false; //Inhabilitem la escriptura
                //Afegit per controlar si s'han d'editar o no...

                //http://stackoverflow.com/questions/1355728/values-of-disabled-inputs-will-not-be-submited
                //problema: All browsers do not should not submit the disabled inputs, as they are read-only.
                //Disabled controls cannot be successful, and a successful control is "valid" for submission. This is the reason why disabled controls dont submit with the form.
                //If a field is disabled, the value of the field is not sent to the server when the form is submitted. If a field is readonly, the value is sent to the server.
            }
        }

        //Afegit csierra 15/11/16
        if (incomplet > 0) {
            $("#incomplet_edited").val(true);
            $("#success_warning").show();
        }
        else //si la recuperacion automatica ha concluido con exito
        {
            $("#incomplet_edited").val(false);
            $("#success_alert").alert();
            $("#success_alert").fadeTo(2000, 500).slideUp(500, function () {
                $("#success_alert").slideUp(500);
            });
        }

    }

    // Bias the autocomplete object to the user's geographical location,
    // as supplied by the browser's 'navigator.geolocation' object.
    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());
            });
        }
    }

Zerion Mini Shell 1.0