简体   繁体   English

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

[英]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.我的 asp.net web 应用程序在HTTP协议上运行,根据 Google 的开发人员指南,该应用程序必须在 HTTPS 协议上运行才能使用 HTML5 地理定位服务。 Somehow I found this solution https://stackoverflow.com/a/39367531/7057220 and I tried this不知何故我找到了这个解决方案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>  

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那么,如何在没有 SSL 连接的情况下使用 Geolocation

you can use the simple Javascript code which I found on W3schools您可以使用我在 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);
}

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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 查找当前位置的纬度和经度 - Find latitude and longitude of current location 计算当前位置经纬度 - Calculate the current location longitude and latitude 如何在OpenLayers中获取用户的当前位置以及纬度和经度 - how to get the current location of the user along with latitude and longitude in OpenLayers 访问时获取用户的位置(纬度,经度) - Get user's location (latitude, longitude) when visited 如何获取当前位置(经度和纬度)并将其发送到反向地理编码API? - How to get current location (latitude and longitude) and send it to an reverse geocoding API? 如何使用谷歌地图查找当前位置(经度和纬度)? - How to find the current location(longitude and latitude) using Google Map? 检测到当前位置后如何在mapbox中获取经度和纬度? - How to get longitude and latitude in mapbox after detect current location? 通过电报机器人获取用户位置经纬度 - Get user location latitude and longitude via telegram bot 开放层中当前位置的纬度和经度 - latitude and longitude of current position in openlayer 如何获取我当前位置的经度和纬度并显示在两个单独的 HTML 表格上 - How to get longitude and latitude of my current location and display on two separate HTML form
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM