Javascript - 返回变量失败

发布于 2024-09-26 03:05:28 字数 954 浏览 1 评论 0原文

我正在尝试做一些本该非常简单的事情,但它却让我费了心思。 我似乎无法从函数返回一个变量

var where;
if (navigator.geolocation) {
    where = navigator.geolocation.getCurrentPosition(function (position) {
    // okay we have a placement - BUT only show it if the accuracy is lower than 500 mtrs
    //if (position.coords.accuracy <= 500 ) {
        // put their lat lng into google
        var meLatLng = new google.maps.LatLng(position.coords.latitude, position.coords.longitude);
        var image = "images/map_blue.png";
        var myMarker = new google.maps.Marker({
          position: meLatLng,
          map: map,
          title: "Your location, as provided by your browser",
          icon: image,
          zIndex: 50
        }); 
    //} // end 500 mtrs test
            return meLatLng;        
    });
    alert("A: "+where);
}

我有一个名为 where 的全局变量,我试图用 navigator.geolocation 从浏览器接收到的 LatLng 信息填充它 - 它在地图上绘制用户 - 但是警报(哪里)总是返回未定义

我做错了什么?

I'm trying to do something that should be pretty simple - but it's doing my head in.
I can't seem to get a variable to return from a function

var where;
if (navigator.geolocation) {
    where = navigator.geolocation.getCurrentPosition(function (position) {
    // okay we have a placement - BUT only show it if the accuracy is lower than 500 mtrs
    //if (position.coords.accuracy <= 500 ) {
        // put their lat lng into google
        var meLatLng = new google.maps.LatLng(position.coords.latitude, position.coords.longitude);
        var image = "images/map_blue.png";
        var myMarker = new google.maps.Marker({
          position: meLatLng,
          map: map,
          title: "Your location, as provided by your browser",
          icon: image,
          zIndex: 50
        }); 
    //} // end 500 mtrs test
            return meLatLng;        
    });
    alert("A: "+where);
}

I have a global variable called where, I'm trying to fill it with LatLng information received from the browser using navigator.geolocation - it plots the user on the map - but alert(where) always returns undefined

What am I doing wrong?

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

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

发布评论

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

评论(1

烟沫凡尘 2024-10-03 03:05:28

简短的回答,什么都没有,

有点长的回答,嗯...你看,问题是你的匿名函数似乎将其值返回给 getCurrentPosition 函数,该函数似乎根本不返回任何内容。

为了使用返回的值,您应该使用回调或处理数据所在的位置...

只需尝试将 return meLatLng 替换为 customCallBack(meLatLng) ,然后定义customCallBack 沿线某处。

或者,由于您已经在全局范围内定义了 where,您可以尝试将 return meLatLng 替换为 where = meLatLng,然后删除 where = from where = navigatior.geoloc....

如果我怀疑是正确的,最后一个方法将不起作用,因为我相信 getCurrentPosition 是异步的,这意味着您一调用它就离开应用程序的标准流程。这就是为什么你首先要为其提供一个函数。

Short answer, nothing,

Bit longer answer, well... you see, the problem is that it would appear that your anonymous function returns its value to the getCurrentPosition function, which it seems doesnt return anything at all.

In order to use your returned value you should instead use a callBack or handle the data where it is...

Simply try replacing return meLatLng with customCallBack(meLatLng) and then defined customCallBack somewhere along the line.

Or, since you have already defined where in the global scope you could try and replace return meLatLng with where = meLatLng and then remove where = from where = navigatior.geoloc....

If what I suspect is right, the last method wont work, since I believe getCurrentPosition is asynchronous, meaning that you leave the standard flow of the application as soon as you call it. Which is why you supply a function to it in the first place.

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