简体   繁体   English

从getCurrentPosition返回“位置”数据。 Phonegap,HTML5,Javascript

[英]Return the “position” data from getCurrentPosition. Phonegap,HTML5, Javascript

Ok sorry for all the logging Ive been trying to figure out how to return the whole object position. 抱歉,我一直在尝试找出如何返回整个对象位置的所有日志记录。 So I can access the items in it later on in the function I called the getCurrentPosition from. 因此,稍后我可以从调用getCurrentPosition的函数中访问其中的项目。 Here is a mock of what I'm trying to do. 这是我正在尝试做的模拟。 Thanks for any help! 谢谢你的帮助!

 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");
}

The call to getCurrentLocation isn't a synchronous operation so you won't be able to do what you're trying. getCurrentLocation的调用不是同步操作,因此您将无法执行正在尝试的操作。 However, you're passing in a onGeoSuccess function and presumably getting a valid position back. 但是,您传入的是onGeoSuccess函数,并且大概会获得有效位置。 Can you not perform what you need to in that function (as you are doing, in fact)? 您是否无法执行该功能所需的操作(实际上,您正在执行的操作)?

[Update based on new info] Ok, so if you need to something with some extra variables in your success function then try the following: [根据新信息进行更新]好的,因此,如果您需要在成功函数中添加一些其他变量的内容,请尝试以下操作:

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
            }
        );
    }
}

I hope that helps, and more importantly, I hope it works. 我希望这会有所帮助,更重要的是,我希望它会起作用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何返回到我刚才在HTML5和JavaScript中点击的位置? - How to return to the position I clicked just now in HTML5 and JavaScript? 联系表格PhoneGap API + Javascript + HTML5 - Contact Form PhoneGap API + Javascript + HTML5 如何获取javascript / html5按钮以返回上一页? (浏览器/ PhoneGap) - How to get a javascript/html5 button to return to previous page? (Browser/PhoneGap) 从Cordova中的getCurrentPosition返回值 - Return value from getCurrentPosition in Cordova HTML5从iFrame调用父JavaScript函数的方法 - 除了使用postMessage之外? 或PhoneGap应用程序的解决方案? - HTML5 way of invoking parent JavaScript function from iFrame — besides using postMessage? Or solution for PhoneGap apps? 如何在HTML5 Javascript Canvas Phonegap游戏中从文本文件读取和写入? - How to read and write from text file in an HTML5 Javascript Canvas Phonegap game? Javascript HTML5数组搜索和“从HTML5返回”文本框(如果位于数组中) - Javascript HTML5 Array search and Return from HTML5 text box if in array 放下位置。 纯Javascript / HTML5 - Drop on the position. Pure Javascript/ HTML5 Javascript和HTML5地理位置-如何存储位置? - Javascript and HTML5 Geolocation - how to store the position? 如何在javascript中确定HTML5画布的大小和位置 - how to size and position a html5 canvas in javascript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM