Commit c55e6d9e by wukun

优化定位相关逻辑

parent a2495175
...@@ -83,6 +83,7 @@ public class LocationUtils1 { ...@@ -83,6 +83,7 @@ public class LocationUtils1 {
} }
private void getLocation() { private void getLocation() {
try {
//1.获取位置管理器 //1.获取位置管理器
locationManager = (LocationManager) mContext.getSystemService(Context.LOCATION_SERVICE); locationManager = (LocationManager) mContext.getSystemService(Context.LOCATION_SERVICE);
//添加用户权限申请判断 //添加用户权限申请判断
...@@ -93,14 +94,14 @@ public class LocationUtils1 { ...@@ -93,14 +94,14 @@ public class LocationUtils1 {
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);
...@@ -112,16 +113,20 @@ public class LocationUtils1 { ...@@ -112,16 +113,20 @@ public class LocationUtils1 {
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()){ try {
getAddressError(latitude, longitude);
}
//Geocoder通过经纬度获取具体信息 //Geocoder通过经纬度获取具体信息
Geocoder gc = new Geocoder(mContext, Locale.getDefault()); Geocoder gc = new Geocoder(mContext, Locale.getDefault());
try {
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,9 +244,14 @@ public class LocationUtils1 { ...@@ -239,9 +244,14 @@ 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);
if (location == null){
getAddressError(0,0);
}else{
showLocation(); showLocation();
} }
}
public interface AddressCallback { public interface AddressCallback {
void onGetAddress(Address address); void onGetAddress(Address address);
......
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