mockLocationSource.ts 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. import { type LocationSample, type LocationSource, type LocationSourceCallbacks } from './locationSource'
  2. export class MockLocationSource implements LocationSource {
  3. callbacks: LocationSourceCallbacks
  4. active: boolean
  5. lastSample: LocationSample | null
  6. constructor(callbacks: LocationSourceCallbacks) {
  7. this.callbacks = callbacks
  8. this.active = false
  9. this.lastSample = null
  10. }
  11. get mode(): 'mock' {
  12. return 'mock'
  13. }
  14. start(): void {
  15. if (this.active) {
  16. this.callbacks.onStatus('模拟定位进行中')
  17. return
  18. }
  19. this.active = true
  20. this.callbacks.onStatus('模拟定位已启动,等待外部输入')
  21. }
  22. stop(): void {
  23. if (!this.active) {
  24. this.callbacks.onStatus('模拟定位未启动')
  25. return
  26. }
  27. this.active = false
  28. this.callbacks.onStatus('模拟定位已停止')
  29. }
  30. destroy(): void {
  31. this.active = false
  32. this.lastSample = null
  33. }
  34. pushSample(sample: LocationSample): void {
  35. this.lastSample = sample
  36. if (!this.active) {
  37. return
  38. }
  39. this.callbacks.onLocation(sample)
  40. }
  41. }