由于我无法在您的帖子中添加评论,因此我将编辑我的第一个答案。抱歉我的第一个答案中的错误,我对地理定位功能不太熟悉。
您在02 结束时没有收到任何响应,因为该函数locationSuccess()
尚未运行,因此没有为myLat
和分配任何值myLong
。
您在03 结尾没有收到响应的原因是该函数navigator.geolocation.getCurrentPosition()
异步工作。当你下一次执行时它还没有完成运行,console.log
所以你的变量的值还没有设置。
我的建议是将使用地理位置的代码包装到您的locationSuccess
函数中,因为这个成功函数无论如何都是该类型代码的预期区域。
我所做的更改:
通过将成功代码移动到成功函数中,您不再需要初始化纬度和经度变量,因此我从下面的代码中删除了这些变量。我还修改了您原来的 GPS 窗口文本infotext
以显示:“正在加载 GPS 数据...”。这样会立即加载,并在后台运行时infowindow
显示加载屏幕。navigator.geolocation.getCurrentPosition
一旦找到当前位置,它将运行成功函数。我在这个函数的末尾添加了一行来更新 GPS 窗口中的文本以显示纬度和经度。我还在错误函数中添加了类似的行,让用户知道是否出现问题。您不需要再次调用infowindow.add(infotext);
或infowindow.show();
再次更新屏幕上的文本。我还将信息窗口/文本的创建移到navigator.geolocation.getCurrentPosition
了下方,因此成功/错误函数无法在创建文本 UI 之前尝试更新文本。
更新代码:
var UI = require('ui');
var Vector2 = require('vector2');
var locationOptions = {
enableHighAccuracy: true,
maximumAge: 10000,
timeout: 10000
};
// Get the location
function locationSuccess(pos) {
console.log('\n****** START ******\nhere I am:\nlat= ' + pos.coords.latitude + ' lon= ' + pos.coords.longitude + '\n'); // Just to se that it works fine
var myLat = pos.coords.latitude;
var myLong = pos.coords.longitude;
console.log('My location\n' + myLat + ' and ' + myLong + '\n****** THE END 01 ******\n'); // This does work fine
//modify the text within infotext to show GPS data
infotext.text('GPS:\n' + myLat + '\n' + myLong);
}
function locationError(err) {
console.log('location error (' + err.code + '): ' + err.message);
//modify the text within infotext to alert user of error
infotext.text('Error Loading GPS Data!');
}
console.log('My location\n' + myLat + ' and ' + myLong + '\n****** THE END 02 ******\n');
// Create a text object
var infotext = new UI.Text(
{
position: new Vector2(0, 0),
size: new Vector2(144, 168),
text : 'Retrieving GPS Data...',
font: 'Gothic 28',
color: 'white',
textAlign: 'center'
});
// Create and show the location (vars x and y are empty - why?)
var infowindow = new UI.Window();
infowindow.add(infotext);
infowindow.show();
// Make an asynchronous request
navigator.geolocation.getCurrentPosition(locationSuccess, locationError,locationOptions);
console.log('My location\n' + myLat + ' and ' + myLong + '\n****** THE END 03 ******\n');
/编辑
原答案:
变量myLat
和myLong
是函数中的局部变量locationSuccess
。要解决此问题,您必须更改定义和的范围。myLat
myLong
在下面的示例中,我全局定义了变量,以便所有脚本和函数都可以引用它们。
var UI = require('ui');
var Vector2 = require('vector2');
//define variables globally
var myLat;
var myLong;
var locationOptions = {
enableHighAccuracy: true,
maximumAge: 10000,
timeout: 10000
};
// Get the location
function locationSuccess(pos) {
console.log('\n****** START ******\nhere I am:\nlat= ' + pos.coords.latitude + ' lon= ' + pos.coords.longitude + '\n'); // Just to se that it works fine
//assign value to globally scoped variables
myLat = pos.coords.latitude;
myLong = pos.coords.longitude;
console.log('My location\n' + myLat + ' and ' + myLong + '\n****** THE END 02 ******\n'); // This does also work fine
}
function locationError(err) {
console.log('location error (' + err.code + '): ' + err.message);
}
// Make an asynchronous request
navigator.geolocation.getCurrentPosition(locationSuccess, locationError, locationOptions);
// Create a text object
var infotext = new UI.Text(
{
position: new Vector2(0, 0),
size: new Vector2(144, 168),
text : 'GPS:\n',
font: 'Gothic 28',
color: 'white',
textAlign: 'center'
});
// Create and show the location (vars x and y are empty - why?)
var infowindow = new UI.Window();
infowindow.add(infotext);
infowindow.show();
有关变量范围的更多信息,这是一个很好的参考。