|
|
@@ -0,0 +1,246 @@
|
|
|
+package com.sheep.gamegroup.util;
|
|
|
+
|
|
|
+import android.Manifest;
|
|
|
+import android.content.Context;
|
|
|
+import android.content.pm.PackageManager;
|
|
|
+import android.location.Criteria;
|
|
|
+import android.location.Location;
|
|
|
+import android.location.LocationListener;
|
|
|
+import android.location.LocationManager;
|
|
|
+import android.os.Bundle;
|
|
|
+import android.support.annotation.NonNull;
|
|
|
+import android.support.v4.app.ActivityCompat;
|
|
|
+import android.text.TextUtils;
|
|
|
+import android.widget.Toast;
|
|
|
+
|
|
|
+import com.sheep.jiuyan.samllsheep.SheepApp;
|
|
|
+
|
|
|
+import java.util.HashMap;
|
|
|
+
|
|
|
+/**
|
|
|
+ * Created by ljy on 2018/5/11.
|
|
|
+ */
|
|
|
+
|
|
|
+public class LocationUtils {
|
|
|
+
|
|
|
+ private static final long REFRESH_TIME = 5000L;
|
|
|
+ private static final float METER_POSITION = 0.0f;
|
|
|
+ private static ILocationListener mLocationListener;
|
|
|
+ String logitudeLStr;
|
|
|
+ private static LocationUtils locationUtils;
|
|
|
+
|
|
|
+ public static LocationUtils getInstance(){
|
|
|
+ if(locationUtils == null){
|
|
|
+ locationUtils = new LocationUtils();
|
|
|
+ }
|
|
|
+ return locationUtils;
|
|
|
+ }
|
|
|
+ private static LocationListener listener = new MyLocationListener();
|
|
|
+
|
|
|
+ private static class MyLocationListener implements LocationListener {
|
|
|
+ @Override
|
|
|
+ public void onLocationChanged(Location location) {//定位改变监听
|
|
|
+ if (mLocationListener != null) {
|
|
|
+ mLocationListener.onSuccessLocation(location);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void onStatusChanged(String provider, int status, Bundle extras) {//定位状态监听
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void onProviderEnabled(String provider) {//定位状态可用监听
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void onProviderDisabled(String provider) {//定位状态不可用监听
|
|
|
+
|
|
|
+ }
|
|
|
+ }
|
|
|
+ /**
|
|
|
+ * GPS获取定位方式
|
|
|
+ */
|
|
|
+ public static Location getGPSLocation(@NonNull Context context) {
|
|
|
+ Location location = null;
|
|
|
+ LocationManager manager = getLocationManager(context);
|
|
|
+ //高版本的权限检查
|
|
|
+ if (ActivityCompat.checkSelfPermission(context, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ if (manager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {//是否支持GPS定位
|
|
|
+ //获取最后的GPS定位信息,如果是第一次打开,一般会拿不到定位信息,一般可以请求监听,在有效的时间范围可以获取定位信息
|
|
|
+ location = manager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
|
|
|
+ }
|
|
|
+ return location;
|
|
|
+ }
|
|
|
+ /**
|
|
|
+ * network获取定位方式
|
|
|
+ */
|
|
|
+ public static Location getNetWorkLocation(Context context) {
|
|
|
+ Location location = null;
|
|
|
+ LocationManager manager = getLocationManager(context);
|
|
|
+ //高版本的权限检查
|
|
|
+ if (ActivityCompat.checkSelfPermission(context, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ if (manager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)) {//是否支持Network定位
|
|
|
+ //获取最后的network定位信息
|
|
|
+ location = manager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
|
|
|
+ }
|
|
|
+ return location;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取最好的定位方式
|
|
|
+ */
|
|
|
+ public static Location getBestLocation(Context context, Criteria criteria) {
|
|
|
+ Location location;
|
|
|
+ LocationManager manager = getLocationManager(context);
|
|
|
+ if (criteria == null) {
|
|
|
+ criteria = new Criteria();
|
|
|
+ }
|
|
|
+ String provider = manager.getBestProvider(criteria, true);
|
|
|
+ if (TextUtils.isEmpty(provider)) {
|
|
|
+ //如果找不到最适合的定位,使用network定位
|
|
|
+ location = getNetWorkLocation(context);
|
|
|
+ } else {
|
|
|
+ //高版本的权限检查
|
|
|
+ if (ActivityCompat.checkSelfPermission(context, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED
|
|
|
+ && ActivityCompat.checkSelfPermission(context, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ //获取最适合的定位方式的最后的定位权限
|
|
|
+ location = manager.getLastKnownLocation(provider);
|
|
|
+ }
|
|
|
+ return location;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 定位监听
|
|
|
+ */
|
|
|
+ public static void addLocationListener(Context context, String provider, ILocationListener locationListener) {
|
|
|
+
|
|
|
+ addLocationListener(context, provider, REFRESH_TIME, METER_POSITION, locationListener);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 定位监听
|
|
|
+ */
|
|
|
+ public static void addLocationListener(Context context, String provider, long time, float meter, ILocationListener locationListener) {
|
|
|
+ if (locationListener != null) {
|
|
|
+ mLocationListener = locationListener;
|
|
|
+ }
|
|
|
+ if (listener == null) {
|
|
|
+ listener = new MyLocationListener();
|
|
|
+ }
|
|
|
+ LocationManager manager = getLocationManager(context);
|
|
|
+ if (ActivityCompat.checkSelfPermission(context, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED
|
|
|
+ && ActivityCompat.checkSelfPermission(context, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ manager.requestLocationUpdates(provider, time, meter, listener);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 取消定位监听
|
|
|
+ */
|
|
|
+ public static void unRegisterListener(Context context) {
|
|
|
+ if (listener != null) {
|
|
|
+ LocationManager manager = getLocationManager(context);
|
|
|
+ if (ActivityCompat.checkSelfPermission(context, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED
|
|
|
+ && ActivityCompat.checkSelfPermission(context, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ //移除定位监听
|
|
|
+ manager.removeUpdates(listener);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private static LocationManager getLocationManager(@NonNull Context context) {
|
|
|
+ return (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 自定义接口
|
|
|
+ */
|
|
|
+ public interface ILocationListener {
|
|
|
+ void onSuccessLocation(Location location);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Longitude:经度
|
|
|
+ * Latitude:纬度
|
|
|
+ * 通过GPS获取定位信息
|
|
|
+ */
|
|
|
+ public String getGPSLocations(final Context context) {
|
|
|
+ logitudeLStr = null;
|
|
|
+ Location gps = getGPSLocation(SheepApp.mContext);
|
|
|
+ if (gps == null) {
|
|
|
+ //设置定位监听,因为GPS定位,第一次进来可能获取不到,通过设置监听,可以在有效的时间范围内获取定位信息
|
|
|
+ LocationUtils.addLocationListener(context, LocationManager.GPS_PROVIDER, new LocationUtils.ILocationListener() {
|
|
|
+ @Override
|
|
|
+ public void onSuccessLocation(Location location) {
|
|
|
+ if (location != null) {
|
|
|
+ logitudeLStr = location.getLongitude() + "," +location.getLatitude();
|
|
|
+ Toast.makeText(context, "gps onSuccessLocation location: lat==" + location.getLatitude() + " lng==" + location.getLongitude(), Toast.LENGTH_SHORT).show();
|
|
|
+ } else {
|
|
|
+// Toast.makeText(context, "gps location is null", Toast.LENGTH_SHORT).show();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ });
|
|
|
+ } else {
|
|
|
+ Toast.makeText(context, "gps location: lat==" + gps.getLatitude() + " lng==" + gps.getLongitude(), Toast.LENGTH_SHORT).show();
|
|
|
+ }
|
|
|
+ return logitudeLStr;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 通过网络等获取定位信息
|
|
|
+ */
|
|
|
+ private String getNetworkLocations() {
|
|
|
+ logitudeLStr = null;
|
|
|
+ Location net = LocationUtils.getNetWorkLocation(SheepApp.mContext);
|
|
|
+ if (net == null) {
|
|
|
+ } else {
|
|
|
+ logitudeLStr = net.getLongitude() + "," +net.getLatitude();
|
|
|
+ }
|
|
|
+ return logitudeLStr;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 采用最好的方式获取定位信息
|
|
|
+ */
|
|
|
+ private String getBestLocations() {
|
|
|
+ logitudeLStr = null;
|
|
|
+ Criteria c = new Criteria();//Criteria类是设置定位的标准信息(系统会根据你的要求,匹配最适合你的定位供应商),一个定位的辅助信息的类
|
|
|
+ c.setPowerRequirement(Criteria.POWER_LOW);//设置低耗电
|
|
|
+ c.setAltitudeRequired(true);//设置需要海拔
|
|
|
+ c.setBearingAccuracy(Criteria.ACCURACY_COARSE);//设置COARSE精度标准
|
|
|
+ c.setAccuracy(Criteria.ACCURACY_LOW);//设置低精度
|
|
|
+ //... Criteria 还有其他属性,就不一一介绍了
|
|
|
+ Location best = LocationUtils.getBestLocation(SheepApp.mContext, c);
|
|
|
+ if (best == null) {
|
|
|
+
|
|
|
+ } else {
|
|
|
+ logitudeLStr = best.getLongitude() + "," +best.getLatitude();
|
|
|
+ }
|
|
|
+ return logitudeLStr;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Longitude:经度
|
|
|
+ * Latitude:纬度
|
|
|
+ * 获取经纬度
|
|
|
+ */
|
|
|
+ public String getLongitudeLatitude(Context context){
|
|
|
+ logitudeLStr = getGPSLocations(context);
|
|
|
+ if(TextUtils.isEmpty(logitudeLStr) && logitudeLStr.length() > 3){
|
|
|
+ logitudeLStr = getNetworkLocations();
|
|
|
+ }
|
|
|
+ return logitudeLStr;
|
|
|
+ }
|
|
|
+}
|