package cn.qiuxiang.react.baidumap.modules import com.baidu.location.BDAbstractLocationListener import com.baidu.location.BDLocation import com.baidu.location.LocationClient import com.facebook.react.bridge.* import com.facebook.react.modules.core.DeviceEventManagerModule.RCTDeviceEventEmitter import java.text.SimpleDateFormat import java.util.* @Suppress("unused") class BaiduMapLocationModule(context: ReactApplicationContext) : ReactContextBaseJavaModule(context) { private val client = LocationClient(context.applicationContext) private val emitter by lazy { context.getJSModule(RCTDeviceEventEmitter::class.java) } private val dateFormat = SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.CHINA) init { val option = client.locOption option.coorType = "bd09ll" option.openGps = true option.setEnableSimulateGps(false) option.setJudgeMockDisValue(100.0) option.setNeedRealLocWhenIsMock(true) option.setScanSpan(3000) client.locOption = option client.registerLocationListener(object : BDAbstractLocationListener() { override fun onReceiveLocation(location: BDLocation) { val isMock = location.isMockGps val realLocation = location.reallLocation if (isMock == BDLocation.MOCK_GPS_TYPE_TRUE && null != realLocation && (realLocation.locType==BDLocation.TypeGpsLocation||realLocation.locType==BDLocation.TypeOffLineLocation||realLocation.locType==BDLocation.TypeNetWorkLocation)) { val data = Arguments.createMap() data.putInt("timestamp", (dateFormat.parse(realLocation.time).time / 1000).toInt()) data.putString("coordinateType", realLocation.coorType) data.putDouble("accuracy", realLocation.radius.toDouble()) data.putDouble("latitude", realLocation.latitude) data.putDouble("longitude", realLocation.longitude) data.putDouble("altitude", realLocation.altitude) data.putDouble("speed", realLocation.speed.toDouble()) data.putDouble("direction", realLocation.direction.toDouble()) data.putInt("locationType", realLocation.locType) emitter.emit("baiduMapLocation", data) } else if (isMock != BDLocation.MOCK_GPS_TYPE_TRUE && (location.locType==BDLocation.TypeGpsLocation||location.locType==BDLocation.TypeOffLineLocation||location.locType==BDLocation.TypeNetWorkLocation)) { val data = Arguments.createMap() data.putInt("timestamp", (dateFormat.parse(location.time).time / 1000).toInt()) data.putString("coordinateType", location.coorType) data.putDouble("accuracy", location.radius.toDouble()) data.putDouble("latitude", location.latitude) data.putDouble("longitude", location.longitude) data.putDouble("altitude", location.altitude) data.putDouble("speed", location.speed.toDouble()) data.putDouble("direction", location.direction.toDouble()) data.putInt("locationType", location.locType) emitter.emit("baiduMapLocation", data) } else { val data = Arguments.createMap() data.putInt("error", location.locType) emitter.emit("baiduMapLocation", data) } } }) } override fun getName(): String { return "BaiduMapLocation" } override fun canOverrideExistingModule(): Boolean { return true } @ReactMethod fun setOptions(options: ReadableMap) { val option = client.locOption if (options.hasKey("gps")) { option.isOpenGps = options.getBoolean("gps") } if(options.hasKey("scanSpan")){ option.setScanSpan(options.getInt("scanSpan")) } if(options.hasKey("needLocationDescribe")){ option.setIsNeedLocationDescribe(options.getBoolean("needLocationDescribe")) } if(options.hasKey("needLocationPoiList")){ option.setIsNeedLocationPoiList(options.getBoolean("needLocationPoiList")) } client.locOption = option } @ReactMethod fun start() { client.start() } @ReactMethod fun stop() { client.stop() } }