LocationManager定位封装
This commit is contained in:
parent
9db4b16e24
commit
212d12db47
@ -0,0 +1,101 @@
|
||||
package com.arpa.hndahesudintocctmsdriver.util.location;
|
||||
|
||||
import static com.arpa.hndahesudintocctmsdriver.util.location.LocationGDUtil.RES;
|
||||
|
||||
import android.Manifest;
|
||||
import android.content.Context;
|
||||
import android.content.pm.PackageManager;
|
||||
import android.location.Address;
|
||||
import android.location.Criteria;
|
||||
import android.location.Geocoder;
|
||||
import android.location.Location;
|
||||
import android.location.LocationListener;
|
||||
import android.location.LocationManager;
|
||||
import android.os.Handler;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.core.app.ActivityCompat;
|
||||
|
||||
import com.arpa.hndahesudintocctmsdriver.util.cache.CacheGroup;
|
||||
import com.arpa.hndahesudintocctmsdriver.util.msg.MsgUtil;
|
||||
import com.dahe.mylibrary.utils.ToastUtils;
|
||||
import com.google.gson.Gson;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
|
||||
/**
|
||||
* @ClassName LocationManagerUtil
|
||||
* @Author john
|
||||
* @Date 2024/7/25 15:21
|
||||
* @Description LocationManager本地定位
|
||||
*/
|
||||
public class LocationManagerUtil {
|
||||
|
||||
public static LocationManagerUtil mInstance = null;
|
||||
|
||||
public static LocationManagerUtil getInstance() {
|
||||
if (null == mInstance) {
|
||||
synchronized (LocationManagerUtil.class) {
|
||||
if (null == mInstance) {
|
||||
mInstance = new LocationManagerUtil();
|
||||
}
|
||||
}
|
||||
}
|
||||
return mInstance;
|
||||
}
|
||||
|
||||
private LocationManagerUtil() {
|
||||
}
|
||||
|
||||
public void getLocation(Context ctx, Handler hd) {
|
||||
LocationManager locationManager = (LocationManager) ctx.getSystemService(Context.LOCATION_SERVICE);
|
||||
|
||||
//创建定位条件器
|
||||
Criteria criteria = new Criteria();
|
||||
//设置精度,Criteria.ACCURACY_FINE表示精确,Criteria.ACCURACY_COARSE表示粗略
|
||||
criteria.setAccuracy(Criteria.ACCURACY_FINE);
|
||||
//设置是否需要海拔信息
|
||||
criteria.setAltitudeRequired(true);
|
||||
//设置是否需要方位信息
|
||||
criteria.setBearingRequired(true);
|
||||
//设置是否允许运营商扣费
|
||||
criteria.setCostAllowed(true);
|
||||
//设置对电源的需求
|
||||
criteria.setPowerRequirement(Criteria.POWER_LOW);
|
||||
|
||||
String bestProvider = locationManager.getBestProvider(criteria, true);
|
||||
if (bestProvider != null) {
|
||||
if (ActivityCompat.checkSelfPermission(ctx, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(ctx, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
|
||||
ToastUtils.showToast(ctx, "请查看定位权限");
|
||||
return;
|
||||
}
|
||||
locationManager.requestLocationUpdates(bestProvider, 300, 0, new LocationListener() {
|
||||
@Override
|
||||
public void onLocationChanged(@NonNull Location location) {
|
||||
try {
|
||||
Geocoder geocoder = new Geocoder(ctx, Locale.getDefault());
|
||||
List<Address> fromLocation = geocoder.getFromLocation(location.getLatitude(), location.getLongitude(), 1);
|
||||
if (fromLocation != null && !fromLocation.isEmpty()) {
|
||||
Address address = fromLocation.get(0);
|
||||
String addressLine = address.getAddressLine(0);
|
||||
String adminArea = address.getAdminArea();//省
|
||||
String locality = address.getLocality();//市
|
||||
String subLocality = address.getSubLocality();//县
|
||||
String featureName = address.getFeatureName();//详细地址
|
||||
CacheGroup.cacheList.put("getLocation", new Gson().toJson(address));
|
||||
MsgUtil.addHdMsgWat(hd, RES);
|
||||
locationManager.removeUpdates(this);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
});
|
||||
} else {
|
||||
ToastUtils.showToast(ctx, "定位失败,请查看定位权限");
|
||||
}
|
||||
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user