andyliu пре 6 месеци
родитељ
комит
cfaaf9c35d
1 измењених фајлова са 229 додато и 0 уклоњено
  1. 229 0
      moduleGis/src/main/java/com/example/moduleGis/APIModuleGis.java

+ 229 - 0
moduleGis/src/main/java/com/example/moduleGis/APIModuleGis.java

@@ -1630,4 +1630,233 @@ public class APIModuleGis extends UZModule {
 			return new ModuleResult(res);
 		}
 	}
+
+	///////////////////////以下为百度定位//////////////////////
+	class BMapLocationResult{
+		int code = 1;                           //0--成功;非0--失败
+		long acceptTime = 0L;                   //接收高德定位回调的时间
+		Map<String,Object> data = null;         //百度定位结果
+
+		public BMapLocationResult(){
+
+		}
+		public BMapLocationResult(int code, long acceptTime, BDLocation data){
+			this.code = code;
+			this.acceptTime = acceptTime;
+			this.data = new HashMap<>();
+			if (data != null) {
+				this.data.put("locType", data.getLocType());//获取定位类型、定位错误返回码
+				this.data.put("coorType", data.getCoorType());//获取经纬度坐标类型
+				this.data.put("latitude", data.getLatitude());//纬度
+				this.data.put("longitude", data.getLongitude());//经度
+				this.data.put("radius", data.getRadius());//精度,默认为0.0f
+				this.data.put("address", data.getAddrStr());
+				this.data.put("country", data.getCountry());
+				this.data.put("province", data.getProvince());
+				this.data.put("city", data.getCity());
+				this.data.put("district", data.getDistrict());
+				this.data.put("street", data.getStreet());
+				this.data.put("adcode", data.getAdCode());
+				this.data.put("town", data.getTown());
+				this.data.put("describe", data.getLocationDescribe());
+			}
+		}
+		public String toString(){
+			return "{\"code\":"+this.code+",\"acceptTime\":"+this.acceptTime+",\"data\":"+ (this.data==null?"{}": com.alibaba.fastjson.JSONObject.toJSONString(this.data)) +"}";
+		}
+	}
+	private LocationClient mBMapClient = null;
+	private LocationClientOption mBMapOption = null;
+	private final byte[] BMAP_LATCH_COMM = new byte[0];
+	private AtomicBoolean mInitedBMapSdk = new AtomicBoolean(false);
+	private AtomicBoolean mStartedBMapLocation = new AtomicBoolean(false);
+	private AtomicBoolean mWaitBMapSingleBack = new AtomicBoolean(false);
+	private CircularFifoQueue<BMapLocationResult> lastBMapLocations = new CircularFifoQueue<>(10);
+
+	private BDAbstractLocationListener mBMapListener = new BDAbstractLocationListener() {
+		@Override
+		public void onReceiveLocation(BDLocation location) {
+			if (null != location) {
+				int locationSize = 0;
+				BMapLocationResult result = new BMapLocationResult(0, System.currentTimeMillis(), location);
+				synchronized (LATCH_COMM){
+					lastBMapLocations.add(result);
+					locationSize = lastBMapLocations.size();
+				}
+				if (mWaitBMapSingleBack.compareAndSet(true, false)){
+					if (mBMapClient != null && locationSize >= 10){
+						mBMapClient.stop();
+					}
+				}
+			}
+		}
+
+		@Override
+		public void onConnectHotSpotMessage(String s, int i) {
+			super.onConnectHotSpotMessage(s, i);
+		}
+
+		@Override
+		public void onLocDiagnosticMessage(int locType, int diagnosticType, String diagnosticMessage) {
+			super.onLocDiagnosticMessage(locType, diagnosticType, diagnosticMessage);
+		}
+	};
+
+	public void initBMapFun(UZModuleContext moduleContext){
+		if (mBMapClient != null) {
+			mBMapClient.stop();
+
+			mInitedBMapSdk.set(false);
+			mStartedBMapLocation.set(false);
+			mWaitBMapSingleBack.set(false);
+		}
+
+		LocationClient.setAgreePrivacy(true);
+		mBMapClient = new LocationClient(moduleContext.getContext());
+		mBMapClient.registerLocationListener(mBMapListener);
+
+		mBMapOption = new LocationClientOption();
+		mBMapOption.setCoorType("gcj02" );
+		mBMapOption.setIsNeedAddress(true);
+		mBMapOption.setIsNeedLocationDescribe(true);
+		mBMapOption.setNeedDeviceDirect(false);
+		mBMapOption.setIgnoreKillProcess(true);
+		mBMapOption.setIsNeedLocationPoiList(false);
+		mBMapOption.SetIgnoreCacheException(false);
+		mBMapOption.setLocationMode(LocationMode.Hight_Accuracy);
+		mBMapOption.setIsNeedAltitude(false);
+		mBMapOption.setFirstLocType(LocationClientOption.FirstLocType.SPEED_IN_FIRST_LOC);
+		mBMapOption.setOpenGnss(true);
+		mBMapOption.setLocationNotify(true);
+		mBMapOption.setWifiCacheTimeOut(1000);
+		mBMapOption.setScanSpan(1000);
+
+		mBMapClient.setLocOption(mBMapOption);
+		mInitedBMapSdk.set(true);
+	}
+
+	public void switchBMapLocFun(boolean onceFlag){
+		if (mBMapClient == null)
+			return;
+
+		if (onceFlag){
+			mBMapOption.setScanSpan(0);
+			mBMapOption.setLocationNotify(false);
+			mBMapOption.setWifiCacheTimeOut(10000);
+			mBMapOption.setFirstLocType(LocationClientOption.FirstLocType.ACCUARACY_IN_FIRST_LOC);
+
+			mBMapClient.stop();
+			mBMapClient.setLocOption(mBMapOption);
+			mBMapClient.start();
+		}else {
+			mBMapOption.setScanSpan(1000);
+			mBMapOption.setLocationNotify(true);
+			mBMapOption.setWifiCacheTimeOut(1000);
+			mBMapOption.setFirstLocType(LocationClientOption.FirstLocType.SPEED_IN_FIRST_LOC);
+
+			mBMapClient.stop();
+			mBMapClient.setLocOption(mBMapOption);
+			mBMapClient.start();
+		}
+	}
+
+	public ModuleResult jsmethod_initBMap_sync(UZModuleContext moduleContext){
+		JSONObject res = new JSONObject();
+		try{
+			if (mInitedBMapSdk.compareAndSet(false, true)){
+				initBMapFun(moduleContext);
+			}
+			res.put("code", "0");
+			res.put("msg", "success");
+			return new ModuleResult(res);
+		}catch(Exception ex){
+			try{
+				res.put("code", "1");
+				res.put("msg", ex.getLocalizedMessage());}catch(Exception e){}
+			Log.e(TAG,ex.getLocalizedMessage());
+			return new ModuleResult(res);
+		}
+	}
+
+	public ModuleResult jsmethod_startBMapLocation_sync(UZModuleContext moduleContext){
+		JSONObject res = new JSONObject();
+		Boolean onceFlag = moduleContext.optBoolean("onceFlag");
+		try{
+			if (mInitedBMapSdk.compareAndSet(false, true)){
+				initBMapFun(moduleContext);
+			}
+
+			if ((onceFlag == null || onceFlag) || mStartedBMapLocation.compareAndSet(false, true)){
+				synchronized (BMAP_LATCH_COMM){
+					lastBMapLocations.clear();
+				}
+				switchBMapLocFun(onceFlag==null?true:onceFlag);
+				mWaitBMapSingleBack.set(onceFlag==null?true:onceFlag);
+			}
+
+			List<String> result = new ArrayList<>();
+			synchronized (BMAP_LATCH_COMM){
+				for (BMapLocationResult element : lastBMapLocations){
+					result.add(0, element==null?"{}":element.toString());
+				}
+			}
+
+			res.put("code", "0");
+			res.put("msg", "success");
+			res.put("data", result);
+			return new ModuleResult(res);
+		}catch(Exception ex){
+			try{
+				res.put("code", "1");
+				res.put("msg", ex.getLocalizedMessage());
+				res.put("data", "[]");
+			}catch(Exception e){}
+			Log.e(TAG,ex.getLocalizedMessage());
+			return new ModuleResult(res);
+		}
+	}
+
+	public ModuleResult jsmethod_stopBMapLocation_sync(UZModuleContext moduleContext){
+		JSONObject res = new JSONObject();
+		try{
+			if (mBMapClient != null)
+				mBMapClient.stop();
+			mStartedBMapLocation.set(false);
+			mWaitBMapSingleBack.set(false);
+			res.put("code", "0");
+			res.put("msg", "success");
+			return new ModuleResult(res);
+		}catch(Exception ex){
+			try{
+				res.put("code", "1");
+				res.put("msg", ex.getLocalizedMessage());
+			}catch(Exception e){}
+			Log.e(TAG,ex.getLocalizedMessage());
+			return new ModuleResult(res);
+		}
+	}
+
+	public ModuleResult jsmethod_getBMapResultList_sync(UZModuleContext moduleContext){
+		JSONObject res = new JSONObject();
+		try {
+			List<String> result = new ArrayList<>();
+			synchronized (BMAP_LATCH_COMM){
+				for (BMapLocationResult element : lastBMapLocations){
+					result.add(0, element.toString());
+				}
+			}
+			res.put("code", "0");
+			res.put("msg", "success");
+			res.put("data", result);
+			return new ModuleResult(res);
+		}catch(Exception ex){
+			try{
+				res.put("code", "1");
+				res.put("msg", ex.getLocalizedMessage());
+				res.put("data", "[]");
+			}catch(Exception e){}
+			Log.e(TAG,ex.getLocalizedMessage());
+			return new ModuleResult(res);
+		}
+	}
 }