Javascript `navigator.geolocation.watchPosition()`返回未定义的

Javascript `navigator.geolocation.watchPosition()`返回未定义的,javascript,angularjs,geolocation,w3c-geolocation,Javascript,Angularjs,Geolocation,W3c Geolocation,非常简单的问题:为什么下面的代码返回未定义的值?作用 GetLocation(location) { Lat = location.coords.latitude; Lon = location.coords.longitude; gCoords = "gCoords: " + Lat + "," + Lon; gLat = Lat; gLon = Lon; $timeout(function() { calculate();

非常简单的问题:为什么下面的代码返回未定义的值?作用

GetLocation(location) {
    Lat = location.coords.latitude;
    Lon = location.coords.longitude;
    gCoords = "gCoords: " + Lat + "," + Lon;
    gLat = Lat;
    gLon = Lon;
    $timeout(function() {
      calculate();
    });
    return [gCoords, gLat, gLon];
  }
  var GPSInfo = navigator.geolocation.watchPosition(GetLocation);
  var info1 = GPSInfo[0]
  var info2 = GPSInfo[1]
  var info3 = GPSInfo[2]
  console.log(
    "gCoords: " + info1 +
    "   gLat: " + info2 + 
    "   gLon: " + info3   )
控制台记录以下内容:
gCoords:undefined gLat:undefined gLon:undefined

有人对此有什么想法吗?

谢谢你的先进

这是一个异步API。当浏览器决定调用“GetLocation”函数时,将调用该函数,但这将在完成设置后的代码之后进行。从异步回调函数返回值通常没有意义。将您的
console.log()
调用放在“GetLocation”函数内。@Pointy我正在将
info2
info3
分配给日志后的不同变量,因此我需要
console.log()
外部
GetLocation()
我仍然需要返回数组。在处理异步系统时,这是不可能的。您别无选择,只能按照API的工作方式构建自己的代码。这就是JavaScript世界的生活。我明白了,好吧,谢谢。