便利贴--26{uni持续获取输出地理位置类}

便利贴--26{uni持续获取输出地理位置类}

js

class $mapPosition {
	constructor(arg) {
		this.getLocation = arg.use.getLocation || ''; //定位模块 use为使用的哪个系统 比如传 uni
		this.type = arg.type || 'wgs84'; //坐标
		this.isHighAccuracy = arg.isHighAccuracy || true; //是否开启精准定位
		this.fn = arg.fn ? function(data) { // 回调
			arg.fn(data);
		} : function(data) {
			console.log("未设置输出回调")
			return data;
		};
		this.ourPosition = []; //全部位置
		this.nowPosition = []; //当前位置
		this.state = false; //持续获取定位的状态
		this.onceState = false; //单次获取定位的状态
		this.getPositionTime = null; //定时实例
		return this;
	}
	//开始获取
	init() {
		//判断权限
		this.state = true; //持续获取定位的状态
		this.continuedTime();
		return this;
	}
	continuedTime() {
		//开始持续获取定位
		if (this.getPositionTime) {
			return;
		} else {
			this.uniOnce();
			this.getPositionTime = setTimeout(() => {
				clearTimeout(this.getPositionTime);
				this.getPositionTime = null;
				this.continuedTime();
			}, 3000)
		}
	}
	uniOnce(fn) {
		// console.log("开始获取一次定位");
		let that = this;
		this.onceState = false; //单次获取定位的状态
		this.getLocation({
			type: that.type,
			isHighAccuracy: that.isHighAccuracy,
			success(res) {
				let latitude = res.latitude;
				let longitude = res.longitude;
				this.onceState = true; //单次获取定位的状态
				console.log(latitude, longitude);
				let onceD = [latitude, longitude];
				if (fn) { //若一次获取有单独的调用和回调函数,直接回调
					fn(onceD);
					return
				}
				//持续获取定位
				//添加至当前位置中
				that.nowPosition = onceD;
				//判断是否运动
				let lat = onceD.latitude + '',
					long = onceD.latitude + '',
					oldLat = that.ourPosition[that.ourPosition.length - 1] ?
					that.ourPosition[that.ourPosition.length - 1].latitude + '' :
					1,
					oldLong = that.ourPosition[that.ourPosition.length - 1] ?
					that.ourPosition[that.ourPosition.length - 1].latitude + '' :
					1;
				// +''隐转字符串再进行比较  因为有小数点,怕精度缺失等数字比较问题 NAN
				if (lat != oldLat && long != oldLong) {
					that.ourPosition.push(onceD)
					//持续输出  输出有效位置
					that.outData();
				}
			}
		})
	}
	over() {
		//结束获取
		if (this.getPositionTime) {
			clearTimeout(this.getPositionTime);
		}
		this.getPositionTime = null;
		this.state = false; //持续获取定位的状态
		this.outData('out');//结束输出
	}
	//抛出数据
	outData(val) {
		let d = {
			onceData: this.nowPosition,
			ourData: this.ourPosition
		}
		if (val) {
			d = val
		}
		this.fn(d)
	}
}
export default $mapPosition;

使用.

this.getOption = new $mapPosition({
				use: uni,
				fn: that.backGetPosition
			})
				if (this.cc) {
					this.getOption.init();
				} else {
					this.getOption.over();
				}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

轻动琴弦

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

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

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

打赏作者

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

抵扣说明:

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

余额充值