Przeglądaj źródła

APIModuleGis 2.0.1 增加高德地图定位SDK

1037015548@qq.com 6 miesięcy temu
rodzic
commit
296934d74f

+ 12 - 0
moduleGis/build.gradle

@@ -23,6 +23,8 @@ android {
         flatDir {
             dirs 'libs'
         }
+        google()
+        mavenCentral()
     }
 
     compileOptions {
@@ -44,4 +46,14 @@ dependencies {
     compile(name:'gnssserver-1.1.20230214-053827-8.0.1',ext:'aar');
     compile(name:'gnsstoollib-1.1.20230214-054100-8.0.1',ext:'aar');
     compile(name:'sdk4a-v1.6.0.3958',ext:'aar');
+
+    //高德定位sdk
+    //定位功能
+    // 高德定位 SDK(检查版本号,确保为最新)
+//    implementation files('../app/libs/AMap_Location_V6.4.8_20241029.jar')
+//    implementation files('../app/libs/commons-collections4-4.4.jar')
+//    compileOnly files('../app/libs/AMap_Location_V6.4.8_20241029.jar')
+//    compileOnly files('../app/libs/commons-collections4-4.4.jar')
+    implementation 'com.amap.api:location:latest.integration'
+    implementation 'org.apache.commons:commons-collections4:4.4'
 }

+ 16 - 0
moduleGis/src/main/AndroidManifest.xml

@@ -30,6 +30,17 @@
 
     <uses-permission android:name="android.permission.ACCESS_LOCATION_EXTRA_COMMANDS"/>
 
+    <!--TODO 高德相关权限设置 -->
+    <!--用于进行网络定位-->
+    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"></uses-permission>
+    <!--用于访问GPS定位-->
+    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"></uses-permission>
+    <!--如果设置了target >= 28 如果需要启动后台定位则必须声明这个权限-->
+    <uses-permission android:name="android.permission.FOREGROUND_SERVICE"/>
+    <!--如果您的应用需要后台定位权限,且有可能运行在Android Q设备上,并且设置了target>28,必须增加这个权限声明-->
+    <uses-permission android:name="android.permission.ACCESS_BACKGROUND_LOCATION"/>
+
+
     <application android:allowBackup="true"
         android:label="@string/app_name"
         android:supportsRtl="true">
@@ -41,6 +52,11 @@
                 <action android:name="com.huace.gnssserver.GnssService"/>
             </intent-filter>
         </service>
+        <!--高德SDK服务-->
+        <service android:name="com.amap.api.location.APSService"></service>
+        <meta-data
+            android:name="com.amap.api.v2.apikey"
+            android:value="62e295bdad10e6ed51e5bc8f26e516ef"/>
 
     </application>
 

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

@@ -27,6 +27,10 @@ import android.widget.RelativeLayout;
 import android.widget.TextView;
 import android.widget.Toast;
 
+import com.amap.api.location.AMapLocation;
+import com.amap.api.location.AMapLocationClient;
+import com.amap.api.location.AMapLocationClientOption;
+import com.amap.api.location.AMapLocationListener;
 import com.example.moduleGis.gnsstest.DiffConnectManager;
 import com.example.moduleGis.gnsstest.GnssListener;
 import com.example.moduleGis.gnsstest.GnssServiceManager;
@@ -51,6 +55,7 @@ import com.uzmap.pkg.uzcore.uzmodule.ModuleResult;
 import com.uzmap.pkg.uzcore.uzmodule.UZModule;
 import com.uzmap.pkg.uzcore.uzmodule.UZModuleContext;
 
+import org.apache.commons.collections4.queue.CircularFifoQueue;
 import org.greenrobot.eventbus.EventBus;
 import org.greenrobot.eventbus.Subscribe;
 import org.greenrobot.eventbus.ThreadMode;
@@ -59,6 +64,7 @@ import org.json.JSONException;
 import org.json.JSONObject;
 
 import java.text.SimpleDateFormat;
+import java.util.ArrayList;
 import java.util.Date;
 import java.util.HashMap;
 import java.util.List;
@@ -67,6 +73,7 @@ import java.util.Map;
 import java.util.TimeZone;
 import java.util.Timer;
 import java.util.TimerTask;
+import java.util.concurrent.atomic.AtomicBoolean;
 
 /**
  * 该类映射至Javascript中moduleDemo对象<br><br>
@@ -1268,4 +1275,310 @@ public class APIModuleGis extends UZModule {
 			setGravity(Gravity.CENTER);
 		}
 	}
+
+	/**
+	 * TODO 高德地图服务相关
+	 */
+	//实时定位状态码
+	public volatile static Integer amapLocationErrorCode = 0;
+	//定位堆栈, 长度不可超过10 , 超过10 则把第一个顶掉,以此类推
+	public volatile static List<AMapLocation> aMapLocations = new ArrayList<>();
+	// 向aMapLocations集合中添加对象的方法
+	public static synchronized void addLocation(AMapLocation location) {
+		if (aMapLocations.size() >= 10) {
+			// 如果长度超过10,移除第一个(即索引为0的元素)
+			aMapLocations.remove(0);
+		}
+		// 添加新的元素
+		aMapLocations.add(location);
+	}
+
+	//声明AMapLocationClient类对象
+	public AMapLocationClient mLocationClient = null;
+	//声明AMapLocationClientOption对象
+	public AMapLocationClientOption mLocationOption = null;
+	//声明定位回调监听器
+	public AMapLocationListener mLocationListener = new AMapLocationListener() {
+		@Override
+		public void onLocationChanged(AMapLocation aMapLocation) {
+			if (aMapLocation != null) {
+				Log.i("参数时间3",""+(System.currentTimeMillis()));
+				AMapZidingyiLocation result = null;
+				if (aMapLocation.getErrorCode() == 0) {
+					result = new AMapZidingyiLocation(0, System.currentTimeMillis(), aMapLocation);
+				}else{
+					result = new AMapZidingyiLocation(aMapLocation.getErrorCode(), System.currentTimeMillis(), null);
+				}
+				synchronized (LATCH_COMM){
+					lastAMapLocations.add(result);
+				}
+			}
+		}
+	};
+	//TODO Lx 初始化定位对象
+	public ModuleResult jsmethod_initGdMap_sync(UZModuleContext moduleContext){
+		JSONObject res = new JSONObject();
+		String code = "1";
+		String msg = "error";
+		try{
+			//TODO 合规检查
+			AMapLocationClient.updatePrivacyShow(moduleContext.getContext(),true,true);
+			AMapLocationClient.updatePrivacyAgree(moduleContext.getContext(),true);
+			//TODO 启动服务相关判断
+			res.put("code",code);
+			res.put("msg",msg);
+			//初始化定位
+			mLocationClient = new AMapLocationClient(moduleContext.getContext());
+			//设置定位回调监听
+			mLocationClient.setLocationListener(mLocationListener);
+			code = "0";
+			msg = "success";
+			res.put("code",code);
+			res.put("msg",msg);
+			return new ModuleResult(res);
+		}catch(Exception ex){
+			code = "1";
+			msg = "errorException";
+			try{res.put("code",code);res.put("msg",msg);}catch(Exception e){}
+			Log.e(TAG,ex.getLocalizedMessage());
+			return new ModuleResult(res);
+		}
+	}
+
+	//TODO 高效率启动高德定位
+	private final byte[] LATCH_COMM = new byte[0];
+	AtomicBoolean mInitedAMapSdk = new AtomicBoolean(false);
+	AtomicBoolean mStartedAMapLocation = new AtomicBoolean(false);
+	class AMapZidingyiLocation{
+		int code = 1;                           //0--成功;非0--失败
+		long acceptTime = 0L;                   //接收高德定位回调的时间
+		AMapLocation data = null;         //高德定位结果
+
+		public AMapZidingyiLocation(){
+
+		}
+		public AMapZidingyiLocation(int code,long acceptTime,AMapLocation data){
+			this.code = code;
+			this.acceptTime = acceptTime;
+			this.data = data;
+		}
+		public String toString(){
+			return "{\"code\":"+code+",\"acceptTime\":"+acceptTime+",\"data\":"+ com.qx.wz.external.fastjson.JSONObject.toJSONString(data) +"}";
+		}
+	}
+	CircularFifoQueue<AMapZidingyiLocation> lastAMapLocations = new CircularFifoQueue<>(10);
+
+	public ModuleResult jsmethod_startAmapLocation_sync(UZModuleContext moduleContext){
+		JSONObject res = new JSONObject();
+		String code = "1";
+		String msg = "error";
+		boolean onceFlag = moduleContext.optBoolean("onceFlag");
+		try{
+			//TODO 启动服务相关判断
+			long callTm = System.currentTimeMillis();
+			if (mInitedAMapSdk.compareAndSet(false, true)){
+				//初始化SDK参数:1) 设置连续定位; 2) 关闭缓存策略; 3) 设置连续定位间隔为1000ms; 4) 其他参数均不填使用其默认配置
+				sendAMapGis();
+			}
+
+			if (onceFlag || mStartedAMapLocation.compareAndSet(false, true)){
+				synchronized (LATCH_COMM){
+					lastAMapLocations.clear();
+				}
+				//调用高德SDK开始定位函数
+				startAMapGis(onceFlag);
+			}
+			long ff = System.currentTimeMillis();
+			//Log.i("结束参数设置时间:",""+System.currentTimeMillis());
+
+			/*do{
+				long lastAcceptTm = 0L;
+				synchronized (LATCH_COMM){
+					if (lastAMapLocations.size() > 0)
+						lastAcceptTm = lastAMapLocations.get(lastAMapLocations.size()-1).acceptTime;
+				}
+
+				Log.i("参数时间1",(System.currentTimeMillis()-ff)+"   "+lastAcceptTm);
+
+				if (lastAcceptTm >= callTm){
+					break;
+				}else{
+//					Log.i("开始睡眠时间:",""+System.currentTimeMillis());
+					try{
+						Thread.sleep(200);
+					}catch (Exception e){
+						e.printStackTrace();
+					}
+//					Log.i("结束睡眠时间:",""+System.currentTimeMillis());
+				}
+			}while(mStartedAMapLocation.get() && (System.currentTimeMillis() - callTm) <= 20000);*/
+
+			Log.i("参数时间2:",""+(System.currentTimeMillis()-ff));
+			List<String> result = new ArrayList<>();
+			synchronized (LATCH_COMM){
+				for (AMapZidingyiLocation element : lastAMapLocations){
+					result.add(0, element.toString());
+				}
+			}
+			code = "0";
+			msg = "success";
+			res.put("code",code);
+			res.put("msg",msg);
+			res.put("data", result);
+			return new ModuleResult(res);
+		}catch(Exception ex){
+			code = "1";
+			msg = "errorException";
+			try{res.put("code",code);res.put("msg",msg);}catch(Exception e){}
+			Log.e(TAG,ex.getLocalizedMessage());
+			return new ModuleResult(res);
+		}
+	}
+	public ModuleResult jsmethod_stopAmapLocation_sync(UZModuleContext moduleContext){
+		JSONObject res = new JSONObject();
+		String code = "1";
+		String msg = "error";
+		try{
+			stopAMapGis();
+			code = "0";
+			msg = "success";
+			res.put("code",code);
+			res.put("msg",msg);
+			return new ModuleResult(res);
+		}catch(Exception ex){
+			code = "1";
+			msg = "errorException";
+			try{res.put("code",code);res.put("msg",msg);}catch(Exception e){}
+			Log.e(TAG,ex.getLocalizedMessage());
+			return new ModuleResult(res);
+		}
+	}
+
+
+	public ModuleResult jsmethod_getLocationList_sync(UZModuleContext moduleContext){
+		JSONObject res = new JSONObject();
+		String code = "1";
+		String msg = "error";
+		try{
+			code = "0";
+			msg = "success";
+			res.put("code",code);
+			res.put("msg",msg);
+			List<String> result = new ArrayList<>();
+			synchronized (LATCH_COMM){
+				for (AMapZidingyiLocation element : lastAMapLocations){
+					result.add(0, element.toString());
+				}
+			}
+			res.put("data",result);
+			return new ModuleResult(res);
+		}catch(Exception ex){
+			code = "1";
+			msg = "errorException";
+			try{res.put("code",code);res.put("msg",msg);}catch(Exception e){}
+			Log.e(TAG,ex.getLocalizedMessage());
+			return new ModuleResult(res);
+		}
+	}
+
+	public ModuleResult sendAMapGis(){
+		JSONObject res = new JSONObject();
+		String code = "1";
+		String msg = "error";
+		try{
+			//TODO 启动服务相关判断
+			res.put("code",code);
+			res.put("msg",msg);
+			//初始化AMapLocationClientOption对象
+			mLocationOption = new AMapLocationClientOption();
+			AMapLocationClientOption option = new AMapLocationClientOption();
+			/**
+			 * 设置定位场景,目前支持三种场景(签到、出行、运动,默认无场景)
+			 */
+			option.setLocationPurpose(AMapLocationClientOption.AMapLocationPurpose.SignIn);
+			if(null != mLocationClient){
+				mLocationClient.setLocationOption(option);
+				//设置场景模式后最好调用一次stop,再调用start以保证场景模式生效
+//				mLocationClient.stopLocation();
+//				mLocationClient.startLocation();
+
+				//设置定位模式为AMapLocationMode.Hight_Accuracy,高精度模式。
+				mLocationOption.setLocationMode(AMapLocationClientOption.AMapLocationMode.Hight_Accuracy);
+
+				//单位是毫秒,默认30000毫秒,建议超时时间不要低于8000毫秒。
+				mLocationOption.setHttpTimeOut(8000);
+
+				//关闭缓存机制
+				mLocationOption.setLocationCacheEnable(false);
+
+
+				//给定位客户端对象设置定位参数
+				mLocationClient.setLocationOption(mLocationOption);
+
+				code = "0";
+				msg = "success";
+				res.put("code",code);
+				res.put("msg",msg);
+			}
+
+			return new ModuleResult(res);
+		}catch(Exception ex){
+			code = "1";
+			msg = "errorException";
+			try{res.put("code",code);res.put("msg",msg);}catch(Exception e){}
+			Log.e(TAG,ex.getLocalizedMessage());
+			return new ModuleResult(res);
+		}
+	}
+	public ModuleResult startAMapGis(boolean onceFlag) {
+		JSONObject res = new JSONObject();
+		String code = "1";
+		String msg = "error";
+		try {
+			code = "0";
+			msg = "success";
+			res.put("code",code);
+			res.put("msg",msg);
+			if(onceFlag){
+				mStartedAMapLocation.set(true);
+				mLocationOption.setOnceLocation(true);
+				mLocationClient.startLocation();
+			}else {
+				//设置定位间隔,单位毫秒,默认为2000ms,最低1000ms。
+				mLocationOption.setInterval(1000);
+				//启动定位
+				mLocationClient.startLocation();
+			}
+			return new ModuleResult(res);
+		}catch(Exception ex){
+			code = "1";
+			msg = "errorException";
+			try{res.put("code",code);res.put("msg",msg);}catch(Exception e){}
+			Log.e(TAG,ex.getLocalizedMessage());
+			return new ModuleResult(res);
+		}
+	}
+	public ModuleResult stopAMapGis(){
+		JSONObject res = new JSONObject();
+		String code = "1";
+		String msg = "error";
+		try{
+			//TODO 启动服务相关判断
+			res.put("code",code);
+			res.put("msg",msg);
+			mLocationClient.stopLocation();//停止定位后,本地定位服务并不会被销毁
+			mStartedAMapLocation.set(false);
+			code = "0";
+			msg = "success";
+			res.put("code",code);
+			res.put("msg",msg);
+			return new ModuleResult(res);
+		}catch(Exception ex){
+			code = "1";
+			msg = "errorException";
+			try{res.put("code",code);res.put("msg",msg);}catch(Exception e){}
+			Log.e(TAG,ex.getLocalizedMessage());
+			return new ModuleResult(res);
+		}
+	}
 }