Browse Source

GoogleLocationUtil.java 定位工具类修改

niusongtao@dl-cg.com 5 years ago
parent
commit
f708af62d4

+ 39 - 19
base_library/src/main/java/com/mgtech/base_library/util/GoogleLocationUtil.java

@@ -52,6 +52,8 @@ public class GoogleLocationUtil{
     private MyLocationListener myLocationListener = new MyLocationListener();
     private LocationManager locationManager;
 
+    private Location lastKnownLocation;
+
     private GPS gpsBean;
 
 
@@ -152,35 +154,52 @@ public class GoogleLocationUtil{
         criteria.setPowerRequirement(Criteria.POWER_MEDIUM);
         String provider = locationManager.getBestProvider(criteria, true);
 
-//        LogUtils.e("nst","getLocationInfo: -----" + provider);
         if (provider ==  null){
             locationInfoListener.locationFailed(context.getString(R.string.map_permission_failure));
             return;
         }
-        Location location = locationManager.getLastKnownLocation(provider);
-        if (location != null){
-//            ToastUtils.showShort("getLastKnownLocation:"+location.getLatitude()+"--------"+location.getLongitude());
+        lastKnownLocation = locationManager.getLastKnownLocation(provider);
+//        if (location != null){
+////            ToastUtils.showShort("getLastKnownLocation:"+location.getLatitude()+"--------"+location.getLongitude());
+//
+//            gpsBean = GPSConverterUtils.gps84_To_Gcj02(location.getLatitude(),location.getLongitude());
+//            if ( gpsBean == null)
+//                locationInfoListener.locationSuccess(location.getLatitude(),location.getLongitude());
+//            else
+//                locationInfoListener.locationSuccess(gpsBean.getLat(),gpsBean.getLon());
+//
+//            LogUtil.e("nst","getLastKnownLocation:"+location.getLatitude()+","+location.getLongitude());
+//            if (gpsBean != null)
+//                LogUtil.e("nst","getLastKnownLocation:"+gpsBean.getLat()+","+gpsBean.getLon());
+//        }else{
+//            //如果位置移动超过MIN_DISTANCE米那将会重新定位
+//            locationManager.requestLocationUpdates(provider, MIN_TIME, MIN_DISTANCE,myLocationListener);
+//        }
+
+        locationManager.requestLocationUpdates(provider, MIN_TIME, MIN_DISTANCE,myLocationListener);
+
+//        locationManager.requestLocationUpdates(provider, MIN_TIME, MIN_DISTANCE,listeners[0]);
+//        locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, MIN_TIME, MIN_DISTANCE,listeners[0]);
+//        locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, MIN_TIME, MIN_DISTANCE,listeners[1]);
+    }
+
 
-            gpsBean = GPSConverterUtils.gps84_To_Gcj02(location.getLatitude(),location.getLongitude());
+    private void getLastKnownLocation(){
+        if (lastKnownLocation != null){
+            gpsBean = GPSConverterUtils.gps84_To_Gcj02(lastKnownLocation.getLatitude(),lastKnownLocation.getLongitude());
             if ( gpsBean == null)
-                locationInfoListener.locationSuccess(location.getLatitude(),location.getLongitude());
+                locationInfoListener.locationSuccess(lastKnownLocation.getLatitude(),lastKnownLocation.getLongitude());
             else
                 locationInfoListener.locationSuccess(gpsBean.getLat(),gpsBean.getLon());
 
-            LogUtil.e("nst","getLastKnownLocation:"+location.getLatitude()+","+location.getLongitude());
+            LogUtil.e("nst","getLastKnownLocation:"+lastKnownLocation.getLatitude()+","+lastKnownLocation.getLongitude());
             if (gpsBean != null)
                 LogUtil.e("nst","getLastKnownLocation:"+gpsBean.getLat()+","+gpsBean.getLon());
-        }else{
-            //如果位置移动超过MIN_DISTANCE米那将会重新定位
-            locationManager.requestLocationUpdates(provider, MIN_TIME, MIN_DISTANCE,myLocationListener);
+        } else {
+            locationInfoListener.locationFailed(context.getString(R.string.map_location_failure));
         }
-
-//        locationManager.requestLocationUpdates(provider, MIN_TIME, MIN_DISTANCE,listeners[0]);
-//        locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, MIN_TIME, MIN_DISTANCE,listeners[0]);
-//        locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, MIN_TIME, MIN_DISTANCE,listeners[1]);
     }
 
-
     private class MyLocationListener implements LocationListener {
 
         @Override
@@ -188,9 +207,7 @@ public class GoogleLocationUtil{
             if (locationInfoListener == null)
                 return;
             if(location != null){
-//                ToastUtils.showShort("onLocationChanged:"+location.getLatitude()+"--------"+location.getLongitude());
-//                LogUtil.e("nst","onLocationChanged:"+location.getLatitude()+"--------"+location.getLongitude());
-//                locationInfoListener.locationSuccess(location.getLatitude(),location.getLongitude());
+                LogUtil.e("nst","onLocationChanged:"+location.getLatitude()+"--------"+location.getLongitude());
 
                 gpsBean = GPSConverterUtils.gps84_To_Gcj02(location.getLatitude(),location.getLongitude());
                 if ( gpsBean == null)
@@ -198,8 +215,11 @@ public class GoogleLocationUtil{
                 else
                     locationInfoListener.locationSuccess(gpsBean.getLat(),gpsBean.getLon());
             }else{
-                locationInfoListener.locationFailed(context.getString(R.string.map_location_failure));
+//                locationInfoListener.locationFailed(context.getString(R.string.map_location_failure));
+                getLastKnownLocation();
             }
+            if (gpsBean != null)
+                LogUtil.e("nst","onLocationChanged:"+gpsBean.getLat()+","+gpsBean.getLon());
         }
 
         @Override