javascript - 获取用户当前位置的纬度和经度

标签 javascript jquery ssl https geolocation

我想获取用户当前位置的纬度和经度值。我的 asp.net web 应用程序在 HTTP 协议(protocol)上运行,根据 Google 的开发人员指南,应用程序必须在 HTTPS 协议(protocol)上运行才能使用 HTML5 GeoLocation 服务。我以某种方式找到了这个解决方案 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 连接的情况下使用地理定位

最佳答案

您可以使用我在 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);
}

关于javascript - 获取用户当前位置的纬度和经度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51055463/

相关文章:

javascript - 回调数据不被识别

javascript - 排序并合并具有匹配值的 JSON 键

ssl - 使用 letsencrypt 为本地服务器签署 SSL 证书

javascript - 有人认识这个 javascript 设计模式吗?

javascript - 将PHP字符串传递给HTML href“属性撇号问题

javascript - Phonegap + JQuery 无法连接到互联网

jquery - jquery 中的 .id 和 .attr ('id' ) 有什么区别

security - SSL 不再安全了吗?

ssl - Nginx : SSL_CTX_use_PrivateKey_file (. .) 失败

javascript - 无法理解以数组为键的对象