2013-02-22 32 views
1

好的抱歉,我一直在试图找出如何返回整个对象位置的所有日志记录。所以我可以稍后在我称之为getCurrentPosition的函数中访问它中的项目。这是我想要做的一个模拟。谢谢你的帮助!从getCurrentPosition返回“位置”数据。 Phonegap,HTML5,Javascript

function aFunctionName(){ 
    pos = navigator.geolocation.getCurrentPosition(onGeoSuccess, geoFail); 
    cameraLat = pos.coords.latitude; 
    \\cannot read property coords 
    console.log(cameraLat); 
    cameraLong = pos.coords.longitude; 
    \\cannot read property coords 
    console.log(cameraLong); 
    cameraTimestamp = pos.timestamp; 
    console.log(cameraTimestamp) 
    \\Cant read property timestamp 
} 

function onGeoSuccess(position) { 
    cameraLati = position.coords.latitude; 
    console.log(cameraLati); 
    \\works fine and displays the lat 
    cameraLongi = position.coords.longitude; 
    console.log(cameraLongi); 
    \\works fine and displays the long 
    cameraTimestampi = position.timestamp; 
    console.log(cameraTimestampi); 
    \\works fine and displays the timestamp 
    return position; 
} 
function onGeofail() { 
    console.log("error"); 
} 

回答

0

的调用getCurrentLocation不是同步操作,所以你不能做你想要什么。但是,您正在传递一个onGeoSuccess函数,并且可能会获得有效位置。你能不能执行你需要的功能(事实上,你正在这么做)?

[更新基于新的信息] 好了,如果你需要的东西,在你成功函数一些额外的变量,然后尝试以下方法:

function saveDataWithPosition(data1, data2, data3, etc.){ 
    if (navigator.geolocation) { 
     navigator.geolocation.getCurrentPosition(
      function (position) { 
       // Insert data 
       // Here you'll have access to your data params 
       // and the position obj 
      }, function (error) {    
       // Handle the error how you wish 
      }, { 
       enableHighAccuracy: true 
      } 
     ); 
    } 
} 

我希望帮助,更重要的是,我希望它能起作用。

+1

问题是我试图挽救这些线以后使用。它适用于我的移动应用程序,我将地理位置和照片信息存储在数据库中。问题是由于独特的关键约束,我需要强制执行插入语句一次完成,我不能做一个插入然后更新,所以我不能预先形成该动作内部的GeoSuccess因为我没有办法传递在我的数据库的照片信息。 – user1857654 2013-02-22 18:00:02

+0

如果我错过了某些东西,但是为什么不能在“成功”功能中插入? – 2013-02-22 18:10:02

+0

我需要传递已经在“aFunctionName”函数中的图像数据和一堆其他信息。你认为我应该将所有这些信息(大约8个变量)传递给onGeoSuccess函数吗? navigator.geolocation.getCurrentPosition(onGeoSuccess(var1,var2,var3,var4,var5,var6,var7,var8),geoFail); ? – user1857654 2013-02-22 18:26:56