便利贴--24{js原生持续输出地理位置类}

32 篇文章 0 订阅
27 篇文章 0 订阅

便利贴--24{js原生持续输出地理位置类}

代码

class $getPositions {
	constructor(arg) {
		this.id = arg.id || 0; //任务id
		this.fn = arg.fn ? function(data) {
			arg.fn(data);
		} : function(data) {
			console.log("未设置输出回调")
			return data;
		};
		this.ourPosition = []; //全部位置
		this.nowPosition = []; //当前位置
		this.onceNowPosition = []; //当前一次位置
		this.getOnceSPosition = null;
		this.state = false;
		this.isContinued = false;
		this.isContinuedTimeInt = null;
		this.isContinuedTimeOut = null;
		return this;
	}
	//开始获取
	init() {
		//判断权限
		return this;

	}
	continued() {
		//开始持续获取定位
		console.log("开始持续定位")
		let that = this;
		this.isContinued = true;
		if (this.isContinuedTimeInt) {
			console.log("重复开始持续定位")
			return;
		} else {
			that.once(function(res) {
				if (res.state == false) {
					console.log(res.intlat)
				}
			})
			this.isContinuedTimeInt = setInterval(() => {
				that.once(function(res) {
					if (res.state == false) {
						console.log(res.intlat)
					}
				})
			}, 5000)
		}
	}
	continuedTime(val) {
		//开始持续获取定位
		let that = this;
		if (val != 1) {
			// console.log(that.id,"开始持续定位")
			this.isContinued = true;
			that.once(function(res) {
				if (res.state == false) {
					// console.log(res.intlat)
					that.outData();
				}
			})
			if (this.isContinuedTimeOut) {
				// console.log("重复开始持续定位")
				return;
			}
		}
		this.isContinuedTimeOut = setTimeout(() => {
			that.once(function(res) {
				if (res.state == false) {
					// console.log(res.intlat)
					that.outData();
					if (res.ok) {
						//返回成功
						that.continuedTime(1);
					} else {
						//返回失败
						that.continuedTime(1);
					}
				}
			})
		}, 30000)

	}
	overContinued() {
		// console.log("结束持续定位")
		let that = this;
		if (this.isContinued) {
			this.isContinued = false;
			if (this.isContinuedTimeInt) {
				clearInterval(that.isContinuedTimeInt);
				this.isContinuedTimeInt = null;
			}
			if (this.isContinuedTimeOut) {
				clearTimeout(that.isContinuedTimeOut);
				this.isContinuedTimeOut = null;
			}
		}
		this.isContinued = false;
		that.outData('out');
	}
	once(fn) {
		// console.log("开始获取一次定位");
		this.state = true;
		let ok = true;
		let that = this,
			fns = fn || '',
			outit = function() {
				if (fns) {
					fns({
						state: that.state,
						intlat: that.onceNowPosition,
						ok: ok
					})
				} else {
					that.outData();
				}
			};
		outit();
		let success = function(position) {
			// console.log(position);
			var lat = position.coords.longitude;//经度
			var lon = position.coords.latitude;//纬度
			// console.log("成功返回经纬度信息");
			// console.log(lon, lat, "成功");
			that.onceNowPosition = [lat, lon];
			that.state = false;
			ok = true;
			outit();
		};
		let error = function(error) {
			// console.log(error, "失败");
			let msg = "none";
			switch (error.code) {
				case error.PERMISSION_DENIED:
					msg = "User denied the request for Geolocation.";
					break;
				case error.POSITION_UNAVAILABLE:
					msg = "Location information is unavailable.";
					break;
				case error.TIMEOUT:
					msg = "The request to get user location timed out.";
					break;
				case error.UNKNOWN_ERROR:
					msg = "An unknown error occurred.";
					break;
			}
			that.onceNowPosition = msg;
			that.state = false;
			ok = false;
			outit();
		};
		let options = {
			enableHighAccuracy: true,
			// timeout: 5000,
			timeout: Infinity,
			maximumAge: 0,
		};
		this.getOnceSPosition = navigator.geolocation.getCurrentPosition(
			success,
			error,
			options
		);
	}
	//结束获取
	outInit() {

	}
	//抛出数据
	outData(val) {
		let d = {
			state: this.state,
			intlat: this.onceNowPosition
		}
		if (val) {
			d[val] = val
		}
		this.fn(d)
	}
}

export default $getPositions;


export default positions

使用

new $getPositions({
					id: id,
					fn: function(res) {
						// console.log(res);
						state.isOnTaskValue[id] = {
							id: id,
							intlat: res.intlat
						}
						$store.commit("setMsg", {
							id: id,
							intlat: res.intlat
						})
					}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

轻动琴弦

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值