【问题标题】:Not able to access current location using simple HTML code无法使用简单的 HTML 代码访问当前位置
【发布时间】:2018-08-23 22:24:33
【问题描述】:

我已编写以下代码来访问我的位置,但此代码未显示我当前的位置。

我需要做出哪些改变。因为我是初学者,知道的不多。

提前致谢。

<body>
    <a href="#" id="get_location">Get location</a>
    <div id="map">
        <iframe id="google_map" width="425" height="350" frameborder="0" scrolling="no" marginheight="0" marginwidth="0" src="https://maps.google.co.in?output=embed"></iframe> 
    </div>
    <script>
        var c= function(pos)
        {
            var lat= pos.coords.latitude, long= pos.coords.longitude, coords= lat + ',' + long;
            document.getElementById('google_map').setAttribute('src','https://maps.google.co.in/?q=' + coords + '&z=60&output=embed');
        }
        document.getElementById('get_loaction').onclick = function()
        {   
            navigator.geolocation.getCurrentPosition(c);
            return false;
        }
    </script>
</body>

【问题讨论】:

标签: javascript html google-maps geolocation


【解决方案1】:

首先,您需要检查浏览器是否支持地理定位:

// check for Geolocation support
if (navigator.geolocation) {
   console.log('Geolocation is supported!');
     navigator.geolocation.getCurrentPosition(function(position) {

        // Get the coordinates of the current possition.
        // here u got your location Latitude & Longitude
        var lat = position.coords.latitude;
        var lng = position.coords.longitude;
     }
}
else {
   console.log('Geolocation is not supported for this Browser/OS.');
}

这里有一个很好的例子:https://jsfiddle.net/ubrvm4ao/672/

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-06-01
    • 1970-01-01
    • 2021-03-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多