【发布时间】:2014-03-26 03:28:02
【问题描述】:
我不知道为什么会得到这个结果,我从来没有掌握过 JavaScript,因为与 Python 或 Java 相比,我总是被代码行为的不稳定所吓倒。
我需要将变量lng_ 设置为当前的经度,一旦我发现我将对lat_ 做同样的事情。这是我的代码:
var lng_ = navigator.geolocation.getCurrentPosition(GetLong);
var lat_ = navigator.geolocation.getCurrentPosition(GetLat);
function GetLong(location)
{
console.log(location.coords.longitude);
var ret = location.coords.longitude;
return ret;
}
console.log(lng_);
function GetLat(location)
{
return(location.coords.latitude);
}
如果您查看function GetLong(location),它应该返回经度值并包含一个 JavaScript 日志以显示它是否正常工作。
根据我的 JavaScript 日志,我得到了正确的结果
-122.15616500000002
但是当我将此值存储在ret 中并返回时,最终的console.log() 会出现undefined
JavaScript 怎么知道它返回的对象是字符串还是 int?我的问题的解决方案是什么?
【问题讨论】:
-
navigator.geolocation.getCurrentPosition()不返回任何内容。它将“返回值”传递给回调。
标签: javascript