Search code examples
javascriptasp.net-mvccity

Get the previous found geolocation address


I've got a script that gets user location. Problem is it takes some time to load, sometimes the string doesn't show up in the page. Since I'm new to javascript I don't know what to do from here. I'm now trying to add some function that sets a timeout and if I can't load the geolocation it shows the last geolocation that was shown before (possibly saved in browser or something)

<head>
    <script type="text/javascript" src="http://maps.googleapis.com/maps/api/js?sensor=false"></script>
    <script type="text/javascript">
        function initialize() {
            geocoder = new google.maps.Geocoder();
        }
        var geocoder;
        if (navigator.geolocation) {
            navigator.geolocation.getCurrentPosition(function (position) {
                var lat = position.coords.latitude;
                var lng = position.coords.longitude;
                geocodeLatLng(lat, lng);
            }, function (error) {
                alert("Erro interno.");
            });
        }
        function geocodeLatLng(lat, lng) {
            var latlng = new google.maps.LatLng(lat, lng);
            geocoder.geocode({ 'latLng': latlng }, function (results, status) {
                if (status == google.maps.GeocoderStatus.OK) {
                    console.log(results)
                    if (results[1]) {
                        //var add = results[0].formatted_address;
                        //document.getElementById("address").innerHTML = add;
                        for (var i = 0; i < results[0].address_components.length; i++) {
                            var addr = results[0].address_components[i];
                            if (addr.types[0] == ['locality']) {    // City
                                document.getElementById("locality").innerHTML = addr.long_name;
                            }
                        }
                    } else
                        alert("Sem resultados.");
                } else
                    alert("Geocoder falhou devido ao seguinto erro: " + status);               
            });
        }
    </script>
</head>
<body onload="initialize()">
    <h4 id="locality"></h4>
</body>


Solution

  • <head>
        <script type="text/javascript" src="http://maps.googleapis.com/maps/api/js?sensor=false"></script>
        <script type="text/javascript">
            function initialize() {
                geocoder = new google.maps.Geocoder();
                var geocoder;
                if (navigator.geolocation) {
                    navigator.geolocation.getCurrentPosition(function (position) {
                        var lat = position.coords.latitude;
                        var lng = position.coords.longitude;
                        geocodeLatLng(lat, lng);
                    }, function (error) {
                        alert("Erro interno.");
                    });
                }
                function geocodeLatLng(lat, lng) {
                    var latlng = new google.maps.LatLng(lat, lng);
                    geocoder.geocode({ 'latLng': latlng }, function (results, status) {
                        if (status == google.maps.GeocoderStatus.OK) {
                            console.log(results)
                            if (results[1]) {
                                //var add = results[0].formatted_address;
                                //document.getElementById("address").innerText = add;
                                for (var i = 0; i < results[0].address_components.length; i++) {
                                    var addr = results[0].address_components[i];
                                    if (addr.types[0] == ['locality']) {    // City
                                        document.getElementById("locality").innerText = addr.long_name;
                                    }
                                }
                            } else
                                alert("Sem resultados.");
                        } else
                            alert("Geocoder falhou devido ao seguinto erro: " + status);
                    });
                }
            }
        </script>
    </head>
    <body onload="initialize()">
    </body>
    <label id="locality"></label>

    Made some changes to the code and it's working fine now... Sorry for anything hehe