|
@@ -23,6 +23,7 @@ import androidx.core.content.ContextCompat;
|
|
|
|
|
|
|
|
import com.coorchice.library.utils.LogUtils;
|
|
import com.coorchice.library.utils.LogUtils;
|
|
|
import com.mgtech.base_library.R;
|
|
import com.mgtech.base_library.R;
|
|
|
|
|
+import com.mgtech.base_library.bean.GPS;
|
|
|
|
|
|
|
|
import java.util.List;
|
|
import java.util.List;
|
|
|
|
|
|
|
@@ -51,6 +52,10 @@ public class GoogleLocationUtil{
|
|
|
private MyLocationListener myLocationListener = new MyLocationListener();
|
|
private MyLocationListener myLocationListener = new MyLocationListener();
|
|
|
private LocationManager locationManager;
|
|
private LocationManager locationManager;
|
|
|
|
|
|
|
|
|
|
+ private Location lastKnownLocation;
|
|
|
|
|
+
|
|
|
|
|
+ private GPS gpsBean;
|
|
|
|
|
+
|
|
|
|
|
|
|
|
public GoogleLocationUtil(Activity context, LocationInfoListener locationInfoListener) {
|
|
public GoogleLocationUtil(Activity context, LocationInfoListener locationInfoListener) {
|
|
|
this.context = context;
|
|
this.context = context;
|
|
@@ -149,20 +154,29 @@ public class GoogleLocationUtil{
|
|
|
criteria.setPowerRequirement(Criteria.POWER_MEDIUM);
|
|
criteria.setPowerRequirement(Criteria.POWER_MEDIUM);
|
|
|
String provider = locationManager.getBestProvider(criteria, true);
|
|
String provider = locationManager.getBestProvider(criteria, true);
|
|
|
|
|
|
|
|
-// LogUtils.e("nst","getLocationInfo: -----" + provider);
|
|
|
|
|
if (provider == null){
|
|
if (provider == null){
|
|
|
locationInfoListener.locationFailed(context.getString(R.string.map_permission_failure));
|
|
locationInfoListener.locationFailed(context.getString(R.string.map_permission_failure));
|
|
|
return;
|
|
return;
|
|
|
}
|
|
}
|
|
|
- Location location = locationManager.getLastKnownLocation(provider);
|
|
|
|
|
- if (location != null){
|
|
|
|
|
-// ToastUtils.showShort("getLastKnownLocation:"+location.getLatitude()+"--------"+location.getLongitude());
|
|
|
|
|
- LogUtil.e("nst","getLastKnownLocation:"+location.getLatitude()+"--------"+location.getLongitude());
|
|
|
|
|
- locationInfoListener.locationSuccess(location.getLatitude(),location.getLongitude());
|
|
|
|
|
- }else{
|
|
|
|
|
- //如果位置移动超过MIN_DISTANCE米那将会重新定位
|
|
|
|
|
- locationManager.requestLocationUpdates(provider, MIN_TIME, MIN_DISTANCE,myLocationListener);
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ 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(provider, MIN_TIME, MIN_DISTANCE,listeners[0]);
|
|
|
// locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, MIN_TIME, MIN_DISTANCE,listeners[0]);
|
|
// locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, MIN_TIME, MIN_DISTANCE,listeners[0]);
|
|
@@ -170,6 +184,22 @@ public class GoogleLocationUtil{
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
+ private void getLastKnownLocation(){
|
|
|
|
|
+ if (lastKnownLocation != null){
|
|
|
|
|
+ gpsBean = GPSConverterUtils.gps84_To_Gcj02(lastKnownLocation.getLatitude(),lastKnownLocation.getLongitude());
|
|
|
|
|
+ if ( gpsBean == null)
|
|
|
|
|
+ locationInfoListener.locationSuccess(lastKnownLocation.getLatitude(),lastKnownLocation.getLongitude());
|
|
|
|
|
+ else
|
|
|
|
|
+ locationInfoListener.locationSuccess(gpsBean.getLat(),gpsBean.getLon());
|
|
|
|
|
+
|
|
|
|
|
+ LogUtil.e("nst","getLastKnownLocation:"+lastKnownLocation.getLatitude()+","+lastKnownLocation.getLongitude());
|
|
|
|
|
+ if (gpsBean != null)
|
|
|
|
|
+ LogUtil.e("nst","getLastKnownLocation:"+gpsBean.getLat()+","+gpsBean.getLon());
|
|
|
|
|
+ } else {
|
|
|
|
|
+ locationInfoListener.locationFailed(context.getString(R.string.map_location_failure));
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
private class MyLocationListener implements LocationListener {
|
|
private class MyLocationListener implements LocationListener {
|
|
|
|
|
|
|
|
@Override
|
|
@Override
|
|
@@ -177,12 +207,19 @@ public class GoogleLocationUtil{
|
|
|
if (locationInfoListener == null)
|
|
if (locationInfoListener == null)
|
|
|
return;
|
|
return;
|
|
|
if(location != null){
|
|
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)
|
|
|
|
|
+ locationInfoListener.locationSuccess(location.getLatitude(),location.getLongitude());
|
|
|
|
|
+ else
|
|
|
|
|
+ locationInfoListener.locationSuccess(gpsBean.getLat(),gpsBean.getLon());
|
|
|
}else{
|
|
}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
|
|
@Override
|