【问题标题】:Fetch latitude and longitude on user's current location获取用户当前位置的经纬度
【发布时间】:2018-06-27 05:56:57
【问题描述】:

我想获取用户当前位置的纬度和经度值。我的 asp.net Web 应用程序在 HTTP 协议上运行,并且根据 Google 的开发人员指南,该应用程序必须在 HTTPS 协议上运行才能使用 HTML5 GeoLocation Service。不知何故,我找到了这个解决方案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 连接的情况下使用地理定位

【问题讨论】:

  • 您期望的纬度:经度以及您得到的错误值是多少?
  • it always gives me incorrect Latitude and Longitude values - 哪个部分可以? navigator.geolocation.getCurrentPosition?还是jQuery.post("https://www.googleapis.com/geolocation/v1/geolocate?key=MyAPIKey
  • 预期值(根据我当前位置):28.4523609,77.067686,实际结果 28.6862738, 77.2217831 这是错误的

标签: javascript jquery ssl https geolocation


【解决方案1】:

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

【讨论】:

  • 感谢您的回复,但我之前已经尝试过,更重要的是,此示例仅适用于 HTTPS 协议。我的应用程序通过 HTTP 协议运行。
  • Geolocation API 从 Chrome 50 中的 Unsecured Origins 中删除 Google 已停止 API 支持 unsecured 即 http 网站探索下面的文章 developers.google.com/web/updates/2016/04/…
【解决方案2】:

你可以试试

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

【讨论】:

    猜你喜欢
    • 2017-10-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多