Răsfoiți Sursa

1.修改Android Apex Drivers定位区分系统定位和Google定位。

Pen Li 7 ani în urmă
părinte
comite
d75507a0a1

+ 148 - 0
ApexDrivers/RAUtilsLibrary/src/main/java/com/usai/redant/rautils/location/GeoUtils.java

@@ -0,0 +1,148 @@
+package com.usai.redant.rautils.location;
+
+public class GeoUtils {
+
+    static double x_pi = 3.14159265358979324 * 3000.0 / 180.0;
+    // π
+    static double pi = 3.1415926535897932384626;
+    // 长半轴
+    static double a = 6378245.0;
+    // 扁率
+    static double ee = 0.00669342162296594323;
+
+    public static double[] wgs84togcj02(double lng, double lat) {
+        if (false) {
+            return new double[] { lng, lat };
+        }
+        double dlat = transformlat(lng - 105.0, lat - 35.0);
+        double dlng = transformlng(lng - 105.0, lat - 35.0);
+        double radlat = lat / 180.0 * pi;
+        double magic = Math.sin(radlat);
+        magic = 1 - ee * magic * magic;
+        double sqrtmagic = Math.sqrt(magic);
+        dlat = (dlat * 180.0) / ((a * (1 - ee)) / (magic * sqrtmagic) * pi);
+        dlng = (dlng * 180.0) / (a / sqrtmagic * Math.cos(radlat) * pi);
+        double mglat = lat + dlat;
+        double mglng = lng + dlng;
+        return new double[] { mglng, mglat };
+    }
+
+    /**
+     * 纬度转换
+     *
+     * @param lng
+     * @param lat
+     * @return
+     */
+    public static double transformlat(double lng, double lat) {
+        double ret = -100.0 + 2.0 * lng + 3.0 * lat + 0.2 * lat * lat + 0.1 * lng * lat + 0.2 * Math.sqrt(Math.abs(lng));
+        ret += (20.0 * Math.sin(6.0 * lng * pi) + 20.0 * Math.sin(2.0 * lng * pi)) * 2.0 / 3.0;
+        ret += (20.0 * Math.sin(lat * pi) + 40.0 * Math.sin(lat / 3.0 * pi)) * 2.0 / 3.0;
+        ret += (160.0 * Math.sin(lat / 12.0 * pi) + 320 * Math.sin(lat * pi / 30.0)) * 2.0 / 3.0;
+        return ret;
+    }
+
+    /**
+     * 经度转换
+     *
+     * @param lng
+     * @param lat
+     * @return
+     */
+    public static double transformlng(double lng, double lat) {
+        double ret = 300.0 + lng + 2.0 * lat + 0.1 * lng * lng + 0.1 * lng * lat + 0.1 * Math.sqrt(Math.abs(lng));
+        ret += (20.0 * Math.sin(6.0 * lng * pi) + 20.0 * Math.sin(2.0 * lng * pi)) * 2.0 / 3.0;
+        ret += (20.0 * Math.sin(lng * pi) + 40.0 * Math.sin(lng / 3.0 * pi)) * 2.0 / 3.0;
+        ret += (150.0 * Math.sin(lng / 12.0 * pi) + 300.0 * Math.sin(lng / 30.0 * pi)) * 2.0 / 3.0;
+        return ret;
+    }
+
+
+    /**
+     * WGS坐标转百度坐标系(BD-09)
+     * */
+    public static double[] wgs84tobd09(double lng, double lat) {
+        double[] gcj = wgs84togcj02(lng, lat);
+        double[] bd09 = gcj02tobd09(gcj[0], gcj[1]);
+        return bd09;
+    }
+
+    /**
+     * 火星坐标系(GCJ-02)转百度坐标系(BD-09)
+     * */
+    public static double[] gcj02tobd09(double lng, double lat) {
+        double z = Math.sqrt(lng * lng + lat * lat) + 0.00002 * Math.sin(lat * x_pi);
+        double theta = Math.atan2(lat, lng) + 0.000003 * Math.cos(lng * x_pi);
+        double bd_lng = z * Math.cos(theta) + 0.0065;
+        double bd_lat = z * Math.sin(theta) + 0.006;
+        return new double[] { bd_lng, bd_lat };
+    }
+
+    /**
+     * 百度坐标系(BD-09)转WGS坐标
+     *
+     * @param lng 百度坐标纬度
+     * @param lat 百度坐标经度
+     * @return WGS84坐标数组
+     */
+    public static double[] bd09towgs84(double lng, double lat) {
+        double[] gcj = bd09togcj02(lng, lat);
+        double[] wgs84 = gcj02towgs84(gcj[0], gcj[1]);
+        return wgs84;
+    }
+
+    /**
+     * 百度坐标系(BD-09)转火星坐标系(GCJ-02)
+     *
+     */
+    public static double[] bd09togcj02(double bd_lon, double bd_lat) {
+        double x = bd_lon - 0.0065;
+        double y = bd_lat - 0.006;
+        double z = Math.sqrt(x * x + y * y) - 0.00002 * Math.sin(y * x_pi);
+        double theta = Math.atan2(y, x) - 0.000003 * Math.cos(x * x_pi);
+        double gg_lng = z * Math.cos(theta);
+        double gg_lat = z * Math.sin(theta);
+        return new double[] { gg_lng, gg_lat };
+    }
+
+    /**
+     * GCJ02(火星坐标系)转GPS84
+     *
+     * @param lng 火星坐标系的经度
+     * @param lat 火星坐标系纬度
+     * @return WGS84坐标数组
+     */
+    public static double[] gcj02towgs84(double lng, double lat) {
+        if (out_of_china(lng, lat)) {
+            return new double[] { lng, lat };
+        }
+        double dlat = transformlat(lng - 105.0, lat - 35.0);
+        double dlng = transformlng(lng - 105.0, lat - 35.0);
+        double radlat = lat / 180.0 * pi;
+        double magic = Math.sin(radlat);
+        magic = 1 - ee * magic * magic;
+        double sqrtmagic = Math.sqrt(magic);
+        dlat = (dlat * 180.0) / ((a * (1 - ee)) / (magic * sqrtmagic) * pi);
+        dlng = (dlng * 180.0) / (a / sqrtmagic * Math.cos(radlat) * pi);
+        double mglat = lat + dlat;
+        double mglng = lng + dlng;
+        return new double[] { lng * 2 - mglng, lat * 2 - mglat };
+    }
+
+    /**
+     * 判断是否在国内,不在国内不做偏移
+     *
+     * @param lng
+     * @param lat
+     * @return
+     */
+    public static boolean out_of_china(double lng, double lat) {
+        if (lng < 72.004 || lng > 137.8347) {
+            return true;
+        } else if (lat < 0.8293 || lat > 55.8271) {
+            return true;
+        }
+        return false;
+    }
+
+}

+ 203 - 0
ApexDrivers/RAUtilsLibrary/src/main/java/com/usai/redant/rautils/location/SystemLocation.java

@@ -0,0 +1,203 @@
+package com.usai.redant.rautils.location;
+
+import android.Manifest;
+import android.content.Context;
+import android.content.DialogInterface;
+import android.content.Intent;
+import android.content.pm.PackageManager;
+import android.location.Location;
+import android.location.LocationListener;
+import android.location.LocationManager;
+import android.os.Bundle;
+import android.provider.Settings;
+import android.support.v4.app.ActivityCompat;
+import android.support.v7.app.AlertDialog;
+import android.util.Log;
+
+public class SystemLocation {
+
+    private Context mCtx;
+    private LocationManager mLocationManager;
+    private LocationListener mLocationListener;
+    private SystemLocationChangeCallback mCallback;
+
+    public boolean adjustLocation = false;
+
+    public interface SystemLocationChangeCallback {
+
+        void onLocationChanged(Location location);
+    }
+
+    private static volatile SystemLocation systemLocationInstance;
+
+    private SystemLocation() {
+
+    }
+
+    public static SystemLocation sharedLocation() {
+
+        if (systemLocationInstance == null) {
+            synchronized (SystemLocation.class) {
+                if (systemLocationInstance == null) {
+                    systemLocationInstance = new SystemLocation();
+                }
+            }
+        }
+
+        return systemLocationInstance;
+    }
+
+
+    public boolean requestLocation(Context context, SystemLocationChangeCallback callback) {
+
+        if (context == null) {
+            return false;
+        }
+
+        if (ActivityCompat.checkSelfPermission(context, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(mCtx, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
+
+            return false;
+        }
+
+        mCtx = context;
+        mCallback = callback;
+
+        //获取一个地址管理者,获取的方法比较特殊,不是直接new出来的
+        LocationManager locationManager = (LocationManager) context.getSystemService(context.LOCATION_SERVICE);
+
+        mLocationManager = locationManager;
+
+        //使用GPS获取上一次的地址,这样获取到的信息需要多次,才能够显示出来,所以后面有动态的判断
+        Location location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
+
+        handleMyLocation(location);
+
+        //判断是否用户打开了GPS开关,这个和获取权限没关系
+        boolean gpsEnable = GPSisopen(locationManager);
+
+        startListenLocationUpdate();
+
+        if (!gpsEnable) {
+            openLocationSetting();
+        }
+
+        return true;
+    }
+
+    public void stopRequestLocation() {
+        stopListenLocationUpdate();
+    }
+
+    private void startListenLocationUpdate() {
+
+        if (ActivityCompat.checkSelfPermission(mCtx, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(mCtx, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
+
+            return;
+        }
+
+        mLocationListener = new LocationListener() {
+
+            @Override
+            /*当地理位置发生改变的时候调用*/
+            public void onLocationChanged(Location location) {
+
+                if (location != null) {
+
+                    handleMyLocation(location);
+                }
+
+            }
+
+            /* 当状态发生改变的时候调用*/
+            @Override
+            public void onStatusChanged(String s, int i, Bundle bundle) {
+                Log.d("GPS_SERVICES", "状态信息发生改变");
+
+            }
+
+            /*当定位者启用的时候调用*/
+            @Override
+            public void onProviderEnabled(String s) {
+                Log.d("TAG", "onProviderEnabled: ");
+
+            }
+
+            @Override
+            public void onProviderDisabled(String s) {
+                Log.d("TAG", "onProviderDisabled: ");
+            }
+        };
+
+        //获取时时更新,第一个是Provider,第二个参数是更新时间1000ms,第三个参数是更新半径,第四个是监听器
+        mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 8, mLocationListener);
+    }
+
+    private void stopListenLocationUpdate() {
+        if (mLocationListener != null && mLocationManager != null) {
+            mLocationManager.removeUpdates(mLocationListener);
+        }
+        mLocationListener = null;
+        mLocationManager = null;
+        mCtx = null;
+    }
+
+    private void openLocationSetting() {
+
+        final AlertDialog.Builder dialog = new AlertDialog.Builder(mCtx);
+        dialog.setTitle("请打开GPS连接");
+        dialog.setMessage("为了获取定位服务,请先打开GPS");
+        dialog.setPositiveButton("设置", new android.content.DialogInterface.OnClickListener() {
+
+            @Override
+            public void onClick(DialogInterface dialogInterface, int i) {
+                //界面跳转
+                Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
+                mCtx.startActivity(intent);
+            }
+        });
+        dialog.setNegativeButton("取消", new android.content.DialogInterface.OnClickListener() {
+
+            @Override
+            public void onClick(DialogInterface dialogInterface, int i) {
+                dialogInterface.dismiss();
+            }
+        });
+        //调用显示方法!
+        dialog.show();
+    }
+
+
+
+    //判断是否用户打开GPS开关,并作指导性操作!
+    private boolean GPSisopen(LocationManager locationManager) {
+        if (!locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
+
+            return false;
+        }
+        return true;
+    }
+
+    private void handleMyLocation(Location location) {
+
+        if (location == null)
+            return;
+
+        if (adjustLocation) {
+
+            double lat = location.getLatitude();
+            double lon = location.getLongitude();
+
+            double p[] = GeoUtils.wgs84togcj02(lon,lat);
+            lon = p[0];
+            lat = p[1];
+
+            location.setLatitude(lat);
+            location.setLongitude(lon);
+        }
+
+        if (mCallback != null) {
+            mCallback.onLocationChanged(location);
+        }
+    }
+
+}

+ 115 - 46
ApexDrivers/RAUtilsLibrary/src/main/java/com/usai/redant/rautils/service/RAService.java

@@ -32,6 +32,7 @@ import com.google.android.gms.location.LocationServices;
 import com.google.android.gms.tasks.OnCompleteListener;
 import com.google.android.gms.tasks.Task;
 import com.usai.redant.rautils.R;
+import com.usai.redant.rautils.location.SystemLocation;
 import com.usai.redant.rautils.receiver.RABroadcast;
 import com.usai.redant.rautils.receiver.RABroadcastReceiver;
 import com.usai.redant.rautils.upload.RAUploadManager;
@@ -59,6 +60,8 @@ public abstract class RAService extends Service {
     public static final int FLAG_SERVICE_UPLOAD = 1 << 3;
 //    public static final int FLAG_LOCATION_SERVICE = 1<<1;
 
+    private static final boolean System_Location_Flag = true;
+
     // service setup
     protected int service_flag = FLAG_SERVICE_NONE;
     private IntentFilter msgFilter = new IntentFilter();
@@ -128,23 +131,35 @@ public abstract class RAService extends Service {
      * Sets the location request parameters.
      */
     private void createLocationRequest() {
-        mLocationRequest = new LocationRequest();
-        mLocationRequest.setInterval(DEFAULT_LOCATION_TIMEINTERVAL);
-        mLocationRequest.setFastestInterval(DEFAULT_LOCATION_TIMEINTERVAL/2);
-        mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
+
+        if (System_Location_Flag) {
+
+        } else {
+
+            mLocationRequest = new LocationRequest();
+            mLocationRequest.setInterval(DEFAULT_LOCATION_TIMEINTERVAL);
+            mLocationRequest.setFastestInterval(DEFAULT_LOCATION_TIMEINTERVAL/2);
+            mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
+        }
 
     }
 
     private void requestLocationUpdates() {
         Log.i(TAG, "Requesting location updates");
-//        Utils.setRequestingLocationUpdates(this, true);
+
+        if (System_Location_Flag) {
+
+        } else {
+
+            //        Utils.setRequestingLocationUpdates(this, true);
 //        startService(new Intent(getApplicationContext(), LocationUpdatesService.class));
-        try {
-            mFusedLocationClient.requestLocationUpdates(mLocationRequest,
-                    mLocationCallback, null);
-        } catch (SecurityException unlikely) {
+            try {
+                mFusedLocationClient.requestLocationUpdates(mLocationRequest,
+                        mLocationCallback, null);
+            } catch (SecurityException unlikely) {
 //            Utils.setRequestingLocationUpdates(this, false);
-            Log.e(TAG, "Lost location permission. Could not request updates. " + unlikely);
+                Log.e(TAG, "Lost location permission. Could not request updates. " + unlikely);
+            }
         }
     }
 
@@ -154,31 +169,49 @@ public abstract class RAService extends Service {
      */
     private void removeLocationUpdates() {
         Log.i(TAG, "Removing location updates");
-        try {
-            mFusedLocationClient.removeLocationUpdates(mLocationCallback);
+
+        if (System_Location_Flag) {
+
+            SystemLocation.sharedLocation().stopRequestLocation();
+
+        } else {
+
+            try {
+                mFusedLocationClient.removeLocationUpdates(mLocationCallback);
 //            Utils.setRequestingLocationUpdates(this, false);
-            stopSelf();
-        } catch (SecurityException unlikely) {
+                stopSelf();
+            } catch (SecurityException unlikely) {
 //            Utils.setRequestingLocationUpdates(this, true);
-            Log.e(TAG, "Lost location permission. Could not remove updates. " + unlikely);
+                Log.e(TAG, "Lost location permission. Could not remove updates. " + unlikely);
+            }
+
         }
+
     }
     private void getLastLocation() {
-        try {
-            mFusedLocationClient.getLastLocation()
-                    .addOnCompleteListener(new OnCompleteListener<Location>() {
-                        @Override
-                        public void onComplete(@NonNull Task<Location> task) {
-                            if (task.isSuccessful() && task.getResult() != null) {
-                                mLocation = task.getResult();
-                            } else {
-                                Log.w(TAG, "Failed to get location.");
+
+        if (System_Location_Flag) {
+
+        } else {
+
+            try {
+                mFusedLocationClient.getLastLocation()
+                        .addOnCompleteListener(new OnCompleteListener<Location>() {
+                            @Override
+                            public void onComplete(@NonNull Task<Location> task) {
+                                if (task.isSuccessful() && task.getResult() != null) {
+                                    mLocation = task.getResult();
+                                } else {
+                                    Log.w(TAG, "Failed to get location.");
+                                }
                             }
-                        }
-                    });
-        } catch (SecurityException unlikely) {
-            Log.e(TAG, "Lost location permission." + unlikely);
+                        });
+            } catch (SecurityException unlikely) {
+                Log.e(TAG, "Lost location permission." + unlikely);
+            }
+
         }
+
     }
     // sub function Notification
     protected String url_checknotification = null;
@@ -574,29 +607,44 @@ public abstract class RAService extends Service {
 
     }
 
-public void initLocation()
-    {
-        if(mFusedLocationClient!=null)
-            return;
-        mFusedLocationClient = LocationServices.getFusedLocationProviderClient(this);
+    public void initLocation() {
 
-        mLocationCallback = new LocationCallback() {
-            @Override
-            public void onLocationResult(LocationResult locationResult) {
-                super.onLocationResult(locationResult);
+        if (System_Location_Flag) {
+
+            SystemLocation.sharedLocation().adjustLocation = true;
+            SystemLocation.sharedLocation().requestLocation(getApplicationContext(), new SystemLocation.SystemLocationChangeCallback() {
+                @Override
+                public void onLocationChanged(Location location) {
+
+                    Log.d(TAG, "onLocationChanged: " + location);
+                }
+            });
 
-                Log.i(TAG, "onLocationResult: " + locationResult.getLastLocation());
+        } else {
+
+            if(mFusedLocationClient!=null)
+                return;
+            mFusedLocationClient = LocationServices.getFusedLocationProviderClient(this);
+
+            mLocationCallback = new LocationCallback() {
+                @Override
+                public void onLocationResult(LocationResult locationResult) {
+                    super.onLocationResult(locationResult);
+
+                    Log.i(TAG, "onLocationResult: " + locationResult.getLastLocation());
 //                    onNewLocation(locationResult.getLastLocation());
-            }
-        };
+                }
+            };
 
-        createLocationRequest();
-        getLastLocation();
+            createLocationRequest();
+            getLastLocation();
 
+        }
 
 
-//        enable_locationTracing();
     }
+
+
     @Override
     public void onDestroy() {
         Log.e("_SERVICE", "onDestroy: ");
@@ -810,8 +858,16 @@ public void initLocation()
 
 
     void disable_locationTracing() {
-        removeLocationUpdates();
+
+        if (System_Location_Flag) {
+
+
+        } else {
+
+
+            removeLocationUpdates();
 //        locationManager.removeUpdates(locationListener);
+        }
     }
 
 
@@ -828,8 +884,12 @@ public void initLocation()
             return;
         }
 
-        initLocation();
-        requestLocationUpdates();
+        if (System_Location_Flag) {
+
+        } else {
+
+            initLocation();
+            requestLocationUpdates();
 //        locationManager.requestLocationUpdates(
 //                "fused", timeInterval, distance,
 //                locationListener);
@@ -847,6 +907,9 @@ public void initLocation()
 //                    locationListener);
 //
 //        }
+
+        }
+
     }
 
     protected Location request_cachedlocation() {
@@ -864,6 +927,12 @@ public void initLocation()
     }
 
     protected void request_location(final String receiverID) {
+
+        if (System_Location_Flag) {
+
+            return;
+        }
+
         if (receiverID == null)
             throw new IllegalArgumentException("receiverID can't be null");