wxr
2022-11-23 1e7b3abd15d37f6c6bc97ac14922457b9604c275
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
package com.mm.android.deviceaddmodule.mobilecommon.location;
 
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationManager;
import android.provider.Settings;
import android.text.TextUtils;
 
import com.mm.android.deviceaddmodule.mobilecommon.utils.LogUtil;
 
import java.text.DecimalFormat;
 
/**
 * 获取经纬度和反编码的工具类
 * 1.首先使用原生的provider获取
 * 2.未获取到时,采用基于FusedLocation方式
 * 3.支持对经纬度进行反编码
 * note:need to assign necessary listener to your goal
 */
public class FuseLocationUtil {
 
    private Activity mActivity;
    private FetchAddressTask.OnGeoDecodeCompleted mGeoDecodeCompletedListener;
    private OnLocationGetCompleted mLocationGetCompletedListener;
    private Location mLocation;
 
    //仅获取经纬度回调接口
    public interface OnLocationGetCompleted {
        void onLocationGetCompleted(Location result);
    }
 
    //判断定位是否可用
    public static boolean isLocationEnabled(Context context) {
        LocationManager lm = (LocationManager) context.getApplicationContext().getSystemService(Context.LOCATION_SERVICE);
        return lm != null && (lm.isProviderEnabled(LocationManager.NETWORK_PROVIDER) || lm.isProviderEnabled(LocationManager.GPS_PROVIDER));
    }
 
    //判断Gps是否可用
    public static boolean isGpsEnabled(Context context) {
        LocationManager lm = (LocationManager) context.getApplicationContext().getSystemService(Context.LOCATION_SERVICE);
        return lm != null && lm.isProviderEnabled(LocationManager.GPS_PROVIDER);
    }
 
    /**
     * 打开Gps设置界面
     */
    public static void openGpsSettings(Context context) {
        Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(intent);
    }
 
    /**
     * GPS坐标 转换成 角度
     */
    public static String gpsToDegree(double location) {
        double degree = Math.floor(location);
        double minute_temp = (location - degree) * 60;
        double minute = Math.floor(minute_temp);
        String second = new DecimalFormat("#.##").format((minute_temp - minute) * 60);
        return (int) degree + "°" + (int) minute + "′" + second + "″";
    }
 
    //不在中国范围内
    public static boolean outOfChina(double longitude, double latitude) {
        return longitude < 72.004 || longitude > 137.8347 || latitude < 0.8293 || latitude > 55.8271;
    }
 
    //比较两次坐标,是否偏移
    public static boolean isMove(Location location, Location preLocation) {
        boolean isMove;
        if (preLocation != null) {
            double speed = location.getSpeed() * 3.6;
            double distance = location.distanceTo(preLocation);
            double compass = Math.abs(preLocation.getBearing() - location.getBearing());
            double angle;
            if (compass > 180) {
                angle = 360 - compass;
            } else {
                angle = compass;
            }
            if (speed != 0) {
                if (speed < 35 && (distance > 3 && distance < 10)) {
                    isMove = angle > 10;
                } else {
                    isMove = (speed < 40 && distance > 10 && distance < 100) ||
                            (speed < 50 && distance > 10 && distance < 100) ||
                            (speed < 60 && distance > 10 && distance < 100) ||
                            (speed < 9999 && distance > 100);
                }
            } else {
                isMove = false;
            }
        } else {
            isMove = true;
        }
        return isMove;
    }
 
 
    public Location getGpsInfoByProvider(Context context) {
        Location location = null;
        try {
            Criteria criteria = new Criteria();
            criteria.setAccuracy(Criteria.ACCURACY_COARSE);//低精度,如果设置为高精度,依然获取不了location。
            criteria.setAltitudeRequired(false);//不要求海拔
            criteria.setBearingRequired(false);//不要求方位
            criteria.setCostAllowed(true);//允许有花费
            criteria.setPowerRequirement(Criteria.POWER_LOW);//低功耗
 
            LocationManager locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
            //从可用的位置提供器中,匹配以上标准的最佳提供器
            if (locationManager != null) {
                String locationProvider = locationManager.getBestProvider(criteria, true);
                location = TextUtils.isEmpty(locationProvider) ? null : locationManager.getLastKnownLocation(locationProvider);
            }
            if (mLocationGetCompletedListener != null) {
                mLocationGetCompletedListener.onLocationGetCompleted(location);
            }
            LogUtil.debugLog("FuseLocationUtil", location == null ? "getGpsInfo by provider not worked" :
                    "getBestProvider worked! -> Longitude: " + location.getLongitude() + " & Latitude: " + location.getLatitude());
        } catch (SecurityException e) {
            e.printStackTrace();
        }
 
        return location;
    }
 
 
}