web 使用navigator实现定位
This commit is contained in:
parent
25a280403b
commit
d01b4259be
|
|
@ -9,5 +9,8 @@ export declare class Location<ED extends EntityDict, Cxt extends Context<ED>, AD
|
||||||
latitude: number;
|
latitude: number;
|
||||||
longitude: number;
|
longitude: number;
|
||||||
}>;
|
}>;
|
||||||
refresh(): void;
|
refresh(): Promise<{
|
||||||
|
latitude: number;
|
||||||
|
longitude: number;
|
||||||
|
}>;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -11,11 +11,12 @@ var Location = /** @class */ (function (_super) {
|
||||||
Location.prototype.get = function (acceptableLatency) {
|
Location.prototype.get = function (acceptableLatency) {
|
||||||
if (acceptableLatency === void 0) { acceptableLatency = 10000; }
|
if (acceptableLatency === void 0) { acceptableLatency = 10000; }
|
||||||
return tslib_1.__awaiter(this, void 0, void 0, function () {
|
return tslib_1.__awaiter(this, void 0, void 0, function () {
|
||||||
var result;
|
var result, getGeolocation, result;
|
||||||
return tslib_1.__generator(this, function (_a) {
|
return tslib_1.__generator(this, function (_a) {
|
||||||
switch (_a.label) {
|
switch (_a.label) {
|
||||||
case 0:
|
case 0:
|
||||||
if (this.lastTimestamp && Date.now() - this.lastTimestamp < acceptableLatency) {
|
if (this.lastTimestamp &&
|
||||||
|
Date.now() - this.lastTimestamp < acceptableLatency) {
|
||||||
return [2 /*return*/, {
|
return [2 /*return*/, {
|
||||||
latitude: this.latitude,
|
latitude: this.latitude,
|
||||||
longitude: this.longitude,
|
longitude: this.longitude,
|
||||||
|
|
@ -23,7 +24,7 @@ var Location = /** @class */ (function (_super) {
|
||||||
}
|
}
|
||||||
if (!(process.env.OAK_PLATFORM === 'wechatMp')) return [3 /*break*/, 2];
|
if (!(process.env.OAK_PLATFORM === 'wechatMp')) return [3 /*break*/, 2];
|
||||||
return [4 /*yield*/, wx.getLocation({
|
return [4 /*yield*/, wx.getLocation({
|
||||||
type: 'gcj02'
|
type: 'gcj02',
|
||||||
})];
|
})];
|
||||||
case 1:
|
case 1:
|
||||||
result = _a.sent();
|
result = _a.sent();
|
||||||
|
|
@ -34,13 +35,68 @@ var Location = /** @class */ (function (_super) {
|
||||||
latitude: this.latitude,
|
latitude: this.latitude,
|
||||||
longitude: this.longitude,
|
longitude: this.longitude,
|
||||||
}];
|
}];
|
||||||
case 2: throw new Error('Method not implemented.');
|
case 2:
|
||||||
|
getGeolocation = function () {
|
||||||
|
return new Promise(function (resolve, reject) {
|
||||||
|
if (navigator.geolocation) {
|
||||||
|
navigator.geolocation.getCurrentPosition(function (position) {
|
||||||
|
// 1. coords.latitude:估计纬度
|
||||||
|
// 2. coords.longitude:估计经度
|
||||||
|
// 3. coords.altitude:估计高度
|
||||||
|
// 4. coords.accuracy:所提供的以米为单位的经度和纬度估计的精确度
|
||||||
|
// 5. coords.altitudeAccuracy:所提供的以米为单位的高度估计的精确度
|
||||||
|
// 6. coords.heading: 宿主设备当前移动的角度方向,相对于正北方向顺时针计算
|
||||||
|
// 7. coords.speed:以米每秒为单位的设备的当前对地速度
|
||||||
|
var coords = position.coords;
|
||||||
|
var location = {
|
||||||
|
accuracy: coords.accuracy,
|
||||||
|
altitude: coords.altitude,
|
||||||
|
altitudeAccuracy: coords.altitudeAccuracy,
|
||||||
|
heading: coords.heading,
|
||||||
|
latitude: coords.latitude,
|
||||||
|
longitude: coords.longitude,
|
||||||
|
speed: coords.speed,
|
||||||
|
};
|
||||||
|
resolve(location);
|
||||||
|
}, function (error) {
|
||||||
|
reject(error);
|
||||||
|
}, {
|
||||||
|
// 指示浏览器获取高精度的位置,默认为false
|
||||||
|
enableHighAccuracy: true,
|
||||||
|
// 指定获取地理位置的超时时间,默认不限时,单位为毫秒
|
||||||
|
timeout: 10000,
|
||||||
|
// 最长有效期,在重复获取地理位置时,此参数指定多久再次获取位置。
|
||||||
|
maximumAge: 3000,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
reject('Your browser does not support Geolocation!');
|
||||||
|
}
|
||||||
|
});
|
||||||
|
};
|
||||||
|
return [4 /*yield*/, getGeolocation()];
|
||||||
|
case 3:
|
||||||
|
result = (_a.sent());
|
||||||
|
this.lastTimestamp = Date.now();
|
||||||
|
this.latitude = result.latitude;
|
||||||
|
this.longitude = result.longitude;
|
||||||
|
return [2 /*return*/, {
|
||||||
|
latitude: this.latitude,
|
||||||
|
longitude: this.longitude,
|
||||||
|
}];
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
Location.prototype.refresh = function () {
|
Location.prototype.refresh = function () {
|
||||||
throw new Error('Method not implemented.');
|
return tslib_1.__awaiter(this, void 0, void 0, function () {
|
||||||
|
return tslib_1.__generator(this, function (_a) {
|
||||||
|
switch (_a.label) {
|
||||||
|
case 0: return [4 /*yield*/, this.get()];
|
||||||
|
case 1: return [2 /*return*/, _a.sent()];
|
||||||
|
}
|
||||||
|
});
|
||||||
|
});
|
||||||
};
|
};
|
||||||
tslib_1.__decorate([
|
tslib_1.__decorate([
|
||||||
Feature_1.Action
|
Feature_1.Action
|
||||||
|
|
|
||||||
|
|
@ -2,12 +2,19 @@ import { EntityDict, Aspect, Context } from 'oak-domain/lib/types';
|
||||||
import { Action, Feature } from '../types/Feature';
|
import { Action, Feature } from '../types/Feature';
|
||||||
import { CommonAspectDict } from 'oak-common-aspect';
|
import { CommonAspectDict } from 'oak-common-aspect';
|
||||||
|
|
||||||
export class Location<ED extends EntityDict, Cxt extends Context<ED>, AD extends CommonAspectDict<ED, Cxt>> extends Feature<ED, Cxt, AD> {
|
export class Location<
|
||||||
|
ED extends EntityDict,
|
||||||
|
Cxt extends Context<ED>,
|
||||||
|
AD extends CommonAspectDict<ED, Cxt>
|
||||||
|
> extends Feature<ED, Cxt, AD> {
|
||||||
private latitude?: number;
|
private latitude?: number;
|
||||||
private longitude?: number;
|
private longitude?: number;
|
||||||
private lastTimestamp?: number;
|
private lastTimestamp?: number;
|
||||||
async get(acceptableLatency: number = 10000) {
|
async get(acceptableLatency: number = 10000) {
|
||||||
if (this.lastTimestamp && Date.now() - this.lastTimestamp < acceptableLatency) {
|
if (
|
||||||
|
this.lastTimestamp &&
|
||||||
|
Date.now() - this.lastTimestamp < acceptableLatency
|
||||||
|
) {
|
||||||
return {
|
return {
|
||||||
latitude: this.latitude!,
|
latitude: this.latitude!,
|
||||||
longitude: this.longitude!,
|
longitude: this.longitude!,
|
||||||
|
|
@ -16,7 +23,7 @@ export class Location<ED extends EntityDict, Cxt extends Context<ED>, AD extends
|
||||||
if (process.env.OAK_PLATFORM === 'wechatMp') {
|
if (process.env.OAK_PLATFORM === 'wechatMp') {
|
||||||
// 小程序平台
|
// 小程序平台
|
||||||
const result = await wx.getLocation({
|
const result = await wx.getLocation({
|
||||||
type: 'gcj02'
|
type: 'gcj02',
|
||||||
});
|
});
|
||||||
this.latitude = result.latitude;
|
this.latitude = result.latitude;
|
||||||
this.longitude = result.longitude;
|
this.longitude = result.longitude;
|
||||||
|
|
@ -26,14 +33,61 @@ export class Location<ED extends EntityDict, Cxt extends Context<ED>, AD extends
|
||||||
latitude: this.latitude!,
|
latitude: this.latitude!,
|
||||||
longitude: this.longitude!,
|
longitude: this.longitude!,
|
||||||
};
|
};
|
||||||
|
} else {
|
||||||
|
const getGeolocation = () =>
|
||||||
|
new Promise((resolve, reject) => {
|
||||||
|
if (navigator.geolocation) {
|
||||||
|
navigator.geolocation.getCurrentPosition(
|
||||||
|
(position) => {
|
||||||
|
// 1. coords.latitude:估计纬度
|
||||||
|
// 2. coords.longitude:估计经度
|
||||||
|
// 3. coords.altitude:估计高度
|
||||||
|
// 4. coords.accuracy:所提供的以米为单位的经度和纬度估计的精确度
|
||||||
|
// 5. coords.altitudeAccuracy:所提供的以米为单位的高度估计的精确度
|
||||||
|
// 6. coords.heading: 宿主设备当前移动的角度方向,相对于正北方向顺时针计算
|
||||||
|
// 7. coords.speed:以米每秒为单位的设备的当前对地速度
|
||||||
|
const coords = position.coords;
|
||||||
|
const location = {
|
||||||
|
accuracy: coords.accuracy,
|
||||||
|
altitude: coords.altitude,
|
||||||
|
altitudeAccuracy: coords.altitudeAccuracy,
|
||||||
|
heading: coords.heading,
|
||||||
|
latitude: coords.latitude,
|
||||||
|
longitude: coords.longitude,
|
||||||
|
speed: coords.speed,
|
||||||
|
};
|
||||||
|
resolve(location);
|
||||||
|
},
|
||||||
|
(error) => {
|
||||||
|
reject(error);
|
||||||
|
},
|
||||||
|
{
|
||||||
|
// 指示浏览器获取高精度的位置,默认为false
|
||||||
|
enableHighAccuracy: true,
|
||||||
|
// 指定获取地理位置的超时时间,默认不限时,单位为毫秒
|
||||||
|
timeout: 10000,
|
||||||
|
// 最长有效期,在重复获取地理位置时,此参数指定多久再次获取位置。
|
||||||
|
maximumAge: 3000,
|
||||||
}
|
}
|
||||||
else {
|
);
|
||||||
throw new Error('Method not implemented.');
|
} else {
|
||||||
|
reject('Your browser does not support Geolocation!');
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
const result = (await getGeolocation()) as GeolocationCoordinates;
|
||||||
|
this.lastTimestamp = Date.now();
|
||||||
|
this.latitude = result.latitude!;
|
||||||
|
this.longitude = result.longitude!;
|
||||||
|
return {
|
||||||
|
latitude: this.latitude!,
|
||||||
|
longitude: this.longitude!,
|
||||||
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Action
|
@Action
|
||||||
refresh() {
|
async refresh() {
|
||||||
throw new Error('Method not implemented.');
|
return await this.get();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue