[英]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.