js
class $mapPosition {
constructor(arg) {
this.getLocation = arg.use.getLocation || '';
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) {
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;
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();
}