Javascript - 返回变量失败
我正在尝试做一些本该非常简单的事情,但它却让我费了心思。 我似乎无法从函数返回一个变量
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
简短的回答,什么都没有,
有点长的回答,嗯...你看,问题是你的匿名函数似乎将其值返回给 getCurrentPosition 函数,该函数似乎根本不返回任何内容。
为了使用返回的值,您应该使用回调或处理数据所在的位置...
只需尝试将
return meLatLng
替换为customCallBack(meLatLng)
,然后定义customCallBack
沿线某处。或者,由于您已经在全局范围内定义了
where
,您可以尝试将return meLatLng
替换为where = meLatLng
,然后删除where =
fromwhere = 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
withcustomCallBack(meLatLng)
and then definedcustomCallBack
somewhere along the line.Or, since you have already defined
where
in the global scope you could try and replacereturn meLatLng
withwhere = meLatLng
and then removewhere =
fromwhere = 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.