Commit c55e6d9e by wukun

优化定位相关逻辑

parent a2495175
...@@ -83,45 +83,50 @@ public class LocationUtils1 { ...@@ -83,45 +83,50 @@ public class LocationUtils1 {
} }
private void getLocation() { private void getLocation() {
//1.获取位置管理器 try {
locationManager = (LocationManager) mContext.getSystemService(Context.LOCATION_SERVICE); //1.获取位置管理器
//添加用户权限申请判断 locationManager = (LocationManager) mContext.getSystemService(Context.LOCATION_SERVICE);
//添加用户权限申请判断
//2.获取位置提供器,GPS或是NetWork //2.获取位置提供器,GPS或是NetWork
// 获取所有可用的位置提供器 // 获取所有可用的位置提供器
List<String> providerList = locationManager.getProviders(true); List<String> providerList = locationManager.getProviders(true);
String locationProvider; String locationProvider;
if (providerList.contains(LocationManager.GPS_PROVIDER)) { if (providerList.contains(LocationManager.GPS_PROVIDER)) {
//GPS 定位的精准度比较高,但是非常耗电。 //GPS 定位的精准度比较高,但是非常耗电。
// System.out.println("=====GPS_PROVIDER====="); // System.out.println("=====GPS_PROVIDER=====");
locationProvider = LocationManager.GPS_PROVIDER; locationProvider = LocationManager.GPS_PROVIDER;
} else if (providerList.contains(LocationManager.NETWORK_PROVIDER)) {//Google服务被墙不可用 } else if (providerList.contains(LocationManager.NETWORK_PROVIDER)) {//Google服务被墙不可用
//网络定位的精准度稍差,但耗电量比较少。 //网络定位的精准度稍差,但耗电量比较少。
// System.out.println("=====NETWORK_PROVIDER====="); // System.out.println("=====NETWORK_PROVIDER=====");
locationProvider = LocationManager.NETWORK_PROVIDER; locationProvider = LocationManager.NETWORK_PROVIDER;
} else { } else {
// System.out.println("=====NO_PROVIDER====="); // System.out.println("=====NO_PROVIDER=====");
// 当没有可用的位置提供器时,弹出Toast提示用户 // 当没有可用的位置提供器时,弹出Toast提示用户
Intent intent = new Intent(); Intent intent = new Intent();
intent.setAction(Settings.ACTION_LOCATION_SOURCE_SETTINGS); intent.setAction(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
mContext.startActivity(intent); mContext.startActivity(intent);
return; return;
} }
//3.获取上次的位置,一般第一次运行,此值为null //3.获取上次的位置,一般第一次运行,此值为null
location = locationManager.getLastKnownLocation(locationProvider); location = locationManager.getLastKnownLocation(locationProvider);
if (location != null) { if (location != null) {
// 显示当前设备的位置信息 // 显示当前设备的位置信息
// System.out.println("==显示当前设备的位置信息=="); // System.out.println("==显示当前设备的位置信息==");
showLocation(); showLocation();
} else {//当GPS信号弱没获取到位置的时候可从网络获取 } else {//当GPS信号弱没获取到位置的时候可从网络获取
// System.out.println("==Google服务被墙的解决办法=="); // System.out.println("==Google服务被墙的解决办法==");
getLngAndLatWithNetwork();//Google服务被墙的解决办法 getLngAndLatWithNetwork();//Google服务被墙的解决办法
} }
// 监视地理位置变化,第二个和第三个参数分别为更新的最短时间minTime和最短距离minDistace // 监视地理位置变化,第二个和第三个参数分别为更新的最短时间minTime和最短距离minDistace
//LocationManager 每隔 5 秒钟会检测一下位置的变化情况,当移动距离超过 10 米的时候, //LocationManager 每隔 5 秒钟会检测一下位置的变化情况,当移动距离超过 10 米的时候,
// 就会调用 LocationListener 的 onLocationChanged() 方法,并把新的位置信息作为参数传入。 // 就会调用 LocationListener 的 onLocationChanged() 方法,并把新的位置信息作为参数传入。
// locationManager.requestLocationUpdates(locationProvider, 5000, 10, locationListener); // locationManager.requestLocationUpdates(locationProvider, 5000, 10, locationListener);
} catch (Exception e) {
e.printStackTrace();
getAddressError(0, 0);
}
} }
//获取经纬度 //获取经纬度
...@@ -146,12 +151,10 @@ public class LocationUtils1 { ...@@ -146,12 +151,10 @@ public class LocationUtils1 {
new Thread() { new Thread() {
@Override @Override
public void run() { public void run() {
if (!Geocoder.isPresent()){
getAddressError(latitude, longitude);
}
//Geocoder通过经纬度获取具体信息
Geocoder gc = new Geocoder(mContext, Locale.getDefault());
try { try {
//Geocoder通过经纬度获取具体信息
Geocoder gc = new Geocoder(mContext, Locale.getDefault());
List<Address> locationList = gc.getFromLocation(latitude, longitude, 1); List<Address> locationList = gc.getFromLocation(latitude, longitude, 1);
if (locationList != null) { if (locationList != null) {
...@@ -173,8 +176,10 @@ public class LocationUtils1 { ...@@ -173,8 +176,10 @@ public class LocationUtils1 {
// for(AddressCallback addressCallback:addressCallbacks){ // for(AddressCallback addressCallback:addressCallbacks){
// addressCallback.onGetAddress(address); // addressCallback.onGetAddress(address);
// } // }
}else{
getAddressError(latitude, longitude);
} }
} catch (IOException e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
getAddressError(latitude, longitude); getAddressError(latitude, longitude);
} }
...@@ -239,7 +244,12 @@ public class LocationUtils1 { ...@@ -239,7 +244,12 @@ public class LocationUtils1 {
LocationManager locationManager = (LocationManager) mContext.getSystemService(Context.LOCATION_SERVICE); LocationManager locationManager = (LocationManager) mContext.getSystemService(Context.LOCATION_SERVICE);
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 5000, 10, locationListener); locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 5000, 10, locationListener);
location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER); location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
showLocation(); if (location == null){
getAddressError(0,0);
}else{
showLocation();
}
} }
public interface AddressCallback { public interface AddressCallback {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment