我需要创建循环线程或定时服务,以获得纬度和经度,尤其是在变化时。我应该如何继续这一方法。我目前正在使用Nativecript-Geolocation插件(https://www.npmjs.com/package/nativecript-geolocation)。
当您关注线程中的链接时,您将在github上看到整个示例代码。
您可以通过这样做:
获得当前位置var location = geolocation.getCurrentLocation({desiredAccuracy: 3, updateDistance: 10, maximumAge: 20000, timeout: 20000}).
then(function(loc) {
if (loc) {
// Your code here
}
}, function(e){
console.log("Error: " + e.message);
});
当您要继续更新时,可以使用:
var watchId = geolocation.watchLocation(
function (loc) {
if (loc) {
// Your code here
}
},
function(e){
console.log("Error: " + e.message);
},
{desiredAccuracy: 3, updateDistance: 10, updateTime: 1000 * 20}); // should update every 20 sec according to google documentation this is not so sure.