简体   繁体   中英

Fetch latitude and longitude on user's current location

I want to fetch user's Lattitude and Longitude value on user's current location. My asp.net web application runs on HTTP protocol and as per the Google's developer guide, the application must be running on HTTPS protocol to use HTML5 GeoLocation Service. Somehow I found this solution https://stackoverflow.com/a/39367531/7057220 and I tried this

 <script type="text/javascript">
        $(document).ready(function () {
            debugger;
            var apiGeolocationSuccess = function (position) {
                alert("API geolocation success!\n\nlat = " + position.coords.latitude + "\nlng = " + position.coords.longitude);
            };
            var tryAPIGeolocation = function () {
                debugger;
                jQuery.post("https://www.googleapis.com/geolocation/v1/geolocate?key=MyAPIKey", function (success) {
                    apiGeolocationSuccess({ coords: { latitude: success.location.lat, longitude: success.location.lng } });
                })
              .fail(function (err) {
                  alert("API Geolocation error! \n\n" + err);
              });
            };

            var browserGeolocationSuccess = function (position) {
                debugger;
                alert("Browser geolocation success!\n\nlat = " + position.coords.latitude + "\nlng = " + position.coords.longitude);
            };

            var browserGeolocationFail = function (error) {
                switch (error.code) {
                    case error.TIMEOUT:
                        alert("Browser geolocation error !\n\nTimeout.");
                        break;
                    case error.PERMISSION_DENIED:
                        if (error.message.indexOf("Only secure origins are allowed") == 0) {
                            tryAPIGeolocation();
                        }
                        break;
                    case error.POSITION_UNAVAILABLE:
                        alert("Browser geolocation error !\n\nPosition unavailable.");
                        break;
                }
            };
            var tryGeolocation = function () {
                debugger;
                if (navigator.geolocation) {
                    navigator.geolocation.getCurrentPosition(
                        browserGeolocationSuccess,
                      browserGeolocationFail,
                      { maximumAge: 50000, timeout: 20000, enableHighAccuracy: true });
                }
            };
            tryGeolocation();
        });
    </script>  

The problem is whenever I run this code it always gives me incorrect Latitude and Longitude values. I want to know that what I'm doing wrong in it. So, how can I use Geolocation without SSL Connection

you can use the simple Javascript code which I found on W3schools

function getLocation() {
    if (navigator.geolocation) {
        navigator.geolocation.getCurrentPosition(showPosition);
    } else { 
        x.innerHTML = "Geolocation is not supported by this browser.";
    }
}

function showPosition(position) {
 alert('Latitude: '+position.coords.latitude +'Longitude: '+ position.coords.longitude);
}

you can try

<!DOCTYPE html>
<html>
<body>

<p>Click the button to get your coordinates.</p>

<button onclick="getLocation()">Try It</button>

<p id="demo"></p>

<script>
var x = document.getElementById("demo");


 
function getLocation() {
    if (navigator.geolocation) {
        navigator.geolocation.getCurrentPosition(showPosition);
    } else { 
        x.innerHTML = "Geolocation is not supported by this browser.";
    }
}

function showPosition(position) {
 var a = position.coords.latitude + ',' + position.coords.longitude;

 alert(a);
}
</script>

</body>
</html>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM