cuốn sách gpt4 ai đã làm

javascript - 从 getCurrentPosition 返回 "position"数据。 Phonegap、HTML5、Javascript

In lại Tác giả: Walker 123 更新时间:2023-11-28 09:20:45 hai mươi bốn 4
mua khóa gpt4 Nike

好吧,抱歉所有的日志记录我一直在试图找出如何返回整个对象的位置。因此,我可以稍后在调用 getCurrentPosition 的函数中访问其中的项目。这是我正在尝试做的事情的模拟。感谢您的帮助!

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

câu trả lời hay nhất

Phải 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
}
);
}
}

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

关于javascript - 从 getCurrentPosition 返回 "position"数据。 Phonegap、HTML5、Javascript,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15028550/

hai mươi bốn 4 0
Chứng chỉ ICP Bắc Kinh số 000000
Hợp tác quảng cáo: 1813099741@qq.com 6ren.com
Xem sitemap của VNExpress