如何使用WatchPosition()函数Geolocation来观察其他坐标
How to use WatchPosition() function Geolocation to watch other coords
你好,我想知道是否有一种方法可以为WatchPosition()函数提供从坐标数组提供的特定Lat/Lon。
我在创建一个使用默认coords之类的回调函数时遇到了问题。lat coords。长
换句话说,我想创建某种函数,它可以以类似的方式使用,但提供我的任意坐标集,以便watchPosition()可以在循环通过数组并设置不同位置时更新位置。
if(navigator.geolocation) {
navigator.geolocation.watchPosition(function(position) {
var pos = new google.maps.LatLng(position.coords.latitude,
position.coords.longitude);
}
我需要做同样的事情,所以我写了一个测试脚本,用mock对象替换navigator.geolocation对象。这是我的代码:
// Replace real geolocation with mock geolocation
delete navigator.geolocation;
navigator.geolocation = {
isMock: true,
paused: true,
delay: 100,
shouldFail: false,
failsAt: -1,
unFailsAt: -1,
errorMessage: "There was an error retrieving the position!",
currentTimeout: -1,
lastPosReturned: 0,
overrideAccuracy: 0, // accuracy in m to return for all positions (overrides any existing accuracies defined in the waypoints)
useOverrideAccuracy: false, // Whether to override acurracies defined in the waypoints with the above override accuracy
_geoCall: function(method, success, error, repeat) {
return this.currentTimeout = window[method].call(null, __bind(function() {
var nextPos;
if(!this.paused && this.lastPosReturned < this.waypoints.length - 1){
nextPos = this.lastPosReturned++;
}else{
this.lastPosReturned = nextPos = 0;
}
if(!this.shouldFail && nextPos == this.failsAt) this.shouldFail = true;
if(this.shouldFail && nextPos == this.unFailsAt) this.shouldFail = false;
if(repeat) this._geoCall("setTimeout", success, error, true);
if (this.shouldFail && (error != null)) {
return error(this.errorMessage);
}
else if (success != null && !this.paused) {
var result = this.waypoints[nextPos];
result.isMock = true;
if(this.useOverrideAccuracy) result.coords.accuracy = this.overrideAccuracy;
success(result);
return nextPos;
}
}, this), this.delay);
},
getCurrentPosition: function(success, error) {
return this._geoCall("setTimeout", success, error, false);
},
watchPosition: function(success, error) {
this._geoCall("setTimeout", success, error, true);
return this.currentTimeout;
},
clearWatch: function(id) {
return clearInterval(this.currentTimeout);
},
waypoints: []
};
然后我只需要用位置对象填充navigator.geolocation.waypoints数组。
相关文章:
- knockoutjs可观察数组
- 如何添加浮动和非浮动,其他
- 与其他库的jQuery.noConflict()
- 播放当前视频时如何停止其他视频?JavaScript
- 剑道UI内联编辑:如何在点击其他按钮时隐藏按钮
- 制作一个不带HTML a标记但在动画播放完毕后指向其他页面的超链接
- 除修剪外的其他功能
- 是什么让一个“;Uncaught RangeError:超过了最大调用堆栈大小“;错误(Chrome,在其他浏览器中显示
- 如何设置观察程序将文件复制到同一文件夹中的其他文件
- 观察其他场模糊事件
- Ember.js:一个模型如何观察其他模型
- 当可观察数组是其他模型的属性时,挖空不会更新 UI
- 如何在运行时向计算可观察量添加其他数组项
- 可由其他视图模型访问的挖空可观察/视图模型
- Rxjs 包装来自 D3 的其他库函数(可观察)
- 在观察者中发布其他游标
- 如何使用WatchPosition()函数Geolocation来观察其他坐标
- 基于其他可观察对象knockoutjs更新observableArray
- Javascript:观察其他脚本抛出的异常
- Angular 2的Array Subject .next()不会调用其他组件的观察者