使用 Cloudpebble 和 PebbleJs 访问 GPS/位置数据

Accessing GPS / location data using Cloudpebble and PebbleJs

尝试通过编写一个简单的脚本(参见下面的代码)在我的鹅卵石上显示我当前的 GPS 位置。

一切正常,但我无法访问 GPS 值。

代码基于 pebbles own geolocation example

我的问题是我只能在函数 locationSuccess() 中获取 GPS 数据。

如何在该函数之外访问 GPS 数据(即 myLatmyLong 的值)?我想将 GPS 数据放入此行:

text : 'GPS:\n',

我正在使用 CloudPebble 并且 Pebble.js

代码:

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

由于我无法向您的 post 添加评论,我将编辑我的第一个答案。抱歉我第一次回答的错误,我对地理定位功能不是太熟悉。

您在 结尾 02 没有收到任何回复,因为函数 locationSuccess() 还没有 运行,所以没有值被分配给 myLatmyLong.

您在 结尾 03 没有收到响应的原因是函数 navigator.geolocation.getCurrentPosition() 异步工作。当你的下一个 console.log 执行时它还没有完成 运行ning,所以你的变量的值仍然没有被设置。

我的建议是将使用地理定位的代码包装到 locationSuccess 函数中,因为无论如何,此成功函数是该类型代码的预期区域。

我所做的更改:

通过将成功代码移动到成功函数中,您不再需要初始化纬度和经度变量,因此我从下面的代码中删除了它们。我还修改了您原来的 GPS window 文本 infotext 以显示:“正在加载 GPS 数据...”。这样 infowindow 会立即加载,并在后台 navigator.geolocation.getCurrentPosition 运行 时显示加载屏幕。

一旦找到当前位置,它将 运行 成功函数。我在此函数的末尾添加了一行以更新 GPS window 中的文本以显示经纬度。我还在错误函数中添加了类似的一行,让用户知道是否出了问题。您不需要再次调用 infowindow.add(infotext);infowindow.show(); 来更新屏幕上的文本。我还将 navigator.geolocation.getCurrentPosition 移动到信息 window/text 的创建下方,以便 success/error 函数无法在创建文本 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'); 

/编辑

原答案:

变量myLatmyLong是函数locationSuccess中的局部变量。要解决此问题,您必须更改定义了 myLatmyLongscope

在下面的例子中,我全局定义了变量,这样所有的脚本和函数都可以引用它们。

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

有关变量范围的更多信息,this 是一个很好的参考。