如何使PhoneGap应用程序保存当前地理位置更少? [英] How to make a PhoneGap application save current geolocation less often?

查看:235
本文介绍了如何使PhoneGap应用程序保存当前地理位置更少?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个应用程式,让你记录你所采取的路线。我使用这个来了解Google Maps API以及Phonegap Build,所以这些是我在这里使用的工具。

I'm working on an app for fun which allows you to record routes you take. I'm using this to learn about the Google Maps API as well as Phonegap Build, so those are the tools I'm using here.

我做了一个基本的应用程序将位置数据(lat,lng)收集到数组中,然后使用Google的API在地图上显示结果。

I made a rudimentary app which collects position data (lat,lng) into an array, then uses Google's API to display the results on a map. In the future I'd like to connect them with a line, allow comments, et cetera.

以下是我的社区周围的驱动器:

Here's what a drive around my neighborhood looks like:

很漂亮。但是,这里有些不对。更长的行程将试图映射许多坐标点时应用程序崩溃。这里在文档中,描述了选项散列。 Mine看起来像这样:

Pretty neat. However, something's wrong here. A longer trip will crash the app when attempting to map that many coordinate points. Here in the docs, the options hash is described. Mine looks like this:

var options = { maximumAge: 1000, timeout: 3000, enableHighAccuracy: true };

我不确定我是否完全理解说明。 maximumAge 似乎意味着,除了新的GPS或位置数据,它将简单地使用前一个,因此将其设置为9999999应该使得缓存的数据从未使用。

I'm not sure I entirely understand the explanation. maximumAge seems to mean that, barring new GPS or location data, it'll simply use the previous one, so setting it to 9999999 should make it so cached data is never used.

超时很麻烦。似乎位置信息不是保证,因此有时它会抛出一个错误。但是增加这个数字使得一个路线具有更少的绘图点?我在哪里可以找到一个GPS系统与手机配合使用?

Timeout is trickier. It seems that location information is not a guarantee and therefore sometimes it'll throw an error. But does increasing that number make for a route with less plot points? Where can I find out about a GPS system works in tandem with a phone?

推荐答案

您应该使用 navigator.geolocation.watchPosition
当检测到位置变化时,返回设备的当前位置。当设备检索新位置时,geolocationSuccess回调将执行一个Position对象作为参数。如果发生错误,geolocationError回调将以PositionError对象作为参数执行。

you should use navigator.geolocation.watchPosition It returns the device's current position when a change in position is detected. When the device retrieves a new location, the geolocationSuccess callback executes with a Position object as the parameter. If there is an error, the geolocationError callback executes with a PositionError object as the parameter.

语法

var watchId = navigator.geolocation.watchPosition(geolocationSuccess,
                                              [geolocationError],
                                              [geolocationOptions]);

示例

// onSuccess Callback
//   This method accepts a `Position` object, which contains
//   the current GPS coordinates
//
function onSuccess(position) {
    var element = document.getElementById('geolocation');
    element.innerHTML = 'Latitude: '  + position.coords.latitude      + '<br />' +
                        'Longitude: ' + position.coords.longitude     + '<br />' +
                        '<hr />'      + element.innerHTML;
}

// onError Callback receives a PositionError object
//
function onError(error) {
    alert('code: '    + error.code    + '\n' +
          'message: ' + error.message + '\n');
}

// Options: throw an error if no update is received every 30 seconds.
//
var watchID = navigator.geolocation.watchPosition(onSuccess, onError, { timeout: 3000 });

现在,只有当设备移动路线时,
根据您的要求更改超时。

now, this way location will be captured only if device is moved so route with less plot points. change timeout as per your requirement.

clearwatch 当您想停止观看设备位置的更改。

clearwatch when you want to stop watching for changes to the device's location.

这篇关于如何使PhoneGap应用程序保存当前地理位置更少?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆