加载纬度和经度时出现 JavaScript 错误

发布于 2025-01-07 23:08:07 字数 748 浏览 2 评论 0原文

我正在尝试使用此代码加载纬度和经度值,但收到此错误:

Uncaught ReferenceError: position is not defined
(anonymous function)

这是我在 html 头部使用的代码:

 <script src="http://maps.google.com/maps/api/js?sensor=true"></script>
<script type="text/javascript">

    $(document).ready(function(){
        get_location();
    });

    function get_location()
    {
        if(navigator.geolocation)
        {
            navigator.geolocation.getCurrentPosition(function(position){

                    var latitude = position.coords.latitude;
                    var longitude = position.coords.longitude;

            });
        }
    }

    console.log(position.coords);
</script>

我忘记了什么?

I'm trying to load latitude and longitude value using this code but I receive back this error:

Uncaught ReferenceError: position is not defined
(anonymous function)

here is the code I used in the head of html:

 <script src="http://maps.google.com/maps/api/js?sensor=true"></script>
<script type="text/javascript">

    $(document).ready(function(){
        get_location();
    });

    function get_location()
    {
        if(navigator.geolocation)
        {
            navigator.geolocation.getCurrentPosition(function(position){

                    var latitude = position.coords.latitude;
                    var longitude = position.coords.longitude;

            });
        }
    }

    console.log(position.coords);
</script>

I forgot something ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

故事和酒 2025-01-14 23:08:07
navigator.geolocation.getCurrentPosition(function(){...}

必须是

navigator.geolocation.getCurrentPosition(function(position){...}

您可以添加回调 errorHandle 来获取 errorCode

更多信息 http://dev .w3.org/geo/api/spec-source.html#geolocation

navigator.geolocation.getCurrentPosition(function(){...}

must be

navigator.geolocation.getCurrentPosition(function(position){...}

You can add callback errorHandle to get errorCode

More info http://dev.w3.org/geo/api/spec-source.html#geolocation

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文