UNPKG

react-native-anavi

Version:

react native amap navigation component, Android + iOS

200 lines (147 loc) 4.54 kB
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; } import { NativeModules, NativeEventEmitter, AppState } from 'react-native'; const { AMapLocation } = NativeModules; const eventEmitter = new NativeEventEmitter(AMapLocation); export class ALocationProxy { constructor(listener) { _defineProperty(this, "listener", void 0); this.listener = listener; } stop() { const index = listeners.indexOf(this.listener); if (index > -1) { listeners.splice(index, 1); } } } class ALocationListner { constructor(once, backgroud, callback) { _defineProperty(this, "once", void 0); _defineProperty(this, "background", void 0); _defineProperty(this, "callback", void 0); _defineProperty(this, "callbackTarget", void 0); this.once = once; this.background = backgroud; this.callback = callback; } apply(location) { if (this.callback) { this.callback(location); } } } export class ALocationClientOptions { constructor() { _defineProperty(this, "httpTimeout", void 0); _defineProperty(this, "interval", void 0); _defineProperty(this, "cacheEnabled", void 0); _defineProperty(this, "mode", void 0); _defineProperty(this, "protocol", void 0); _defineProperty(this, "mockEnabled", void 0); _defineProperty(this, "needAddress", void 0); _defineProperty(this, "sensorEnable", void 0); _defineProperty(this, "wifiScan", void 0); } } /** 当前是否在后台运行 */ var isBackground = false; /** 当前是否在后台定位 */ var isBackgroundLocation = false; var listeners = []; var locationOptions = {}; /** 是否已经开始定位*/ var isStarted = false; const start = () => { if (isStarted) { return; } isStarted = true; AMapLocation.start(locationOptions); }; const stop = () => { isStarted = false; AMapLocation.stop(); }; const appStateChangeListner = status => { isBackground = status !== 'active'; console.log("------>AMapLocation: The application's status is ".concat(status)); if (!isBackground) { console.log("------>AMapLocation: enter forground disable background location"); AMapLocation.enabledBackground(false); } else if (isBackgroundLocation) { console.log("------>AMapLocation: enter background, enabled background location"); AMapLocation.enabledBackground(true); } }; const amapLocationListner = location => { for (const listener of listeners) { if (!listener.background && isBackground) { continue; } listener.apply(location); } // 删除once的listener listeners = listeners.filter(listener => !listener.once); // 没有后台监听 if (isBackgroundLocation && !listeners.find(l => l.background)) { isBackgroundLocation = false; console.log("------>AMapLoaction: There has't background location callback, will stop backgroud location"); } // 已经没有位置监听,自动关闭位置服务 if (!listeners.length) { console.log("------>AMapLoaction: There has't location callback, will auto stop the location service"); stop(); } }; /** * 高德定位服务 */ export class ALocationClient { constructor() { AppState.addEventListener('change', appStateChangeListner); eventEmitter.addListener('amap_location', amapLocationListner); } /** * 获取ALoaction的单例 */ static getInstace() { if (!ALocationClient.instance) { ALocationClient.instance = new ALocationClient(); } return ALocationClient.instance; } /** * 当前是否已启用后台定位 */ get isBackground() { return isBackgroundLocation; } /** * 获取或设置定位选项 */ set options(options) { locationOptions = options; } get options() { return locationOptions; } location(callback, background = false) { this.checkIsAutoStop(); if (background && !isBackgroundLocation) { isBackgroundLocation = true; } const listener = new ALocationListner(false, background, callback); listeners.push(listener); return new ALocationProxy(listener); } locationOnce(callback) { this.checkIsAutoStop(); listeners.push(new ALocationListner(true, false, callback)); } checkIsAutoStop() { if (!isStarted) { start(); } } } _defineProperty(ALocationClient, "instance", void 0); //# sourceMappingURL=location.js.map