簡體   English   中英

獲取用戶當前位置的經緯度

[英]Fetch latitude and longitude on user's current location

我想獲取用戶當前位置的緯度和經度值。 我的 asp.net web 應用程序在HTTP協議上運行,根據 Google 的開發人員指南,該應用程序必須在 HTTPS 協議上運行才能使用 HTML5 地理定位服務。 不知何故我找到了這個解決方案https://stackoverflow.com/a/39367531/7057220我試過了

 <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>  

問題是每當我運行這段代碼時,它總是給我不正確的緯度和經度值。 我想知道我做錯了什么。 那么,如何在沒有 SSL 連接的情況下使用 Geolocation

您可以使用我在 W3schools 上找到的簡單 Javascript 代碼

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

你可以試試

<!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>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM