18034727885
2026-06-26 253f680fb9666751e0f9a8e5e8f2f9e3eb1894e3
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
package com.example.basecore.amap;
 
 
import android.annotation.SuppressLint;
import android.content.Context;
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.Bundle;
import android.text.TextUtils;
import android.util.Log;
 
import com.amap.api.location.AMapLocation;
import com.amap.api.location.AMapLocationClient;
import com.amap.api.location.AMapLocationClientOption;
import com.amap.api.location.AMapLocationListener;
 
import java.io.IOException;
import java.util.List;
 
 
public class LocationProvider implements LocationListener {
    AMapLocationClient locationClient;
    public interface LocationGetListener {
        void getLocationInfo(Location location);
    }
 
    public LocationProvider(Context mContext) {
        this.mContext = mContext;
        initProvider();
    }
 
    Context mContext;
    LocationManager mLocationManager;
    LocationGetListener mListener;
 
    void initProvider() {
        mLocationManager = (LocationManager) mContext.getSystemService(Context.LOCATION_SERVICE);
 
        try {
            locationClient = new AMapLocationClient(mContext);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
 
    }
 
 
    public void getLocation(ILocationCallBack callBack) {
 
        AMapLocationClientOption locationClientOption = new AMapLocationClientOption();
        //使用单次定位
        locationClientOption.setOnceLocation(true);
        // 地址信息
        locationClientOption.setNeedAddress(true);
 
//给定位客户端对象设置定位参数
        locationClient.setLocationOption(locationClientOption);
 
        locationClient.setLocationListener(new AMapLocationListener() {
            @Override
            public void onLocationChanged(AMapLocation location) {
                if (location.getErrorCode() == 0) {
                    callBack.onLocationSuccess(location);
                    callBack.onLocationSuccess(location.getAddress(), location.getLatitude(), location.getLongitude());
                } else  {
                    callBack.onLocationFailed(location.getErrorInfo());
                }
            }
        });
//启动定位
        locationClient.startLocation();
    }
 
    /**
     * 停止单次客户端
     */
    public void stopSingleLocation(){
        if(null != locationClient){
            locationClient.stopLocation();
        }
    }
    /**
     * 根据定位结果返回定位信息的字符串
     * @param location
     * @return
     */
    public synchronized static String getLocationStr(AMapLocation location){
        if(null == location){
            return null;
        }
        StringBuffer sb = new StringBuffer();
        //errCode等于0代表定位成功,其他的为定位失败,具体的可以参照官网定位错误码说明
        if(location.getErrorCode() == 0){
            sb.append("定位成功" + "\n");
            sb.append("定位类型: " + location.getLocationType() + "\n");
            sb.append("经    度    : " + location.getLongitude() + "\n");
            sb.append("纬    度    : " + location.getLatitude() + "\n");
            sb.append("精    度    : " + location.getAccuracy() + "米" + "\n");
            sb.append("提供者    : " + location.getProvider() + "\n");
 
            sb.append("海    拔    : " + location.getAltitude() + "米" + "\n");
            sb.append("速    度    : " + location.getSpeed() + "米/秒" + "\n");
            sb.append("角    度    : " + location.getBearing() + "\n");
            if (location.getProvider().equalsIgnoreCase(
                    android.location.LocationManager.GPS_PROVIDER)) {
                // 以下信息只有提供者是GPS时才会有
                // 获取当前提供定位服务的卫星个数
                sb.append("星    数    : "
                        + location.getSatellites() + "\n");
            }
 
            //逆地理信息
            sb.append("国    家    : " + location.getCountry() + "\n");
            sb.append("省            : " + location.getProvince() + "\n");
            sb.append("市            : " + location.getCity() + "\n");
            sb.append("城市编码 : " + location.getCityCode() + "\n");
            sb.append("区            : " + location.getDistrict() + "\n");
            sb.append("区域 码   : " + location.getAdCode() + "\n");
            sb.append("地    址    : " + location.getAddress() + "\n");
            sb.append("兴趣点    : " + location.getPoiName() + "\n");
            //定位完成的时间
 
        } else {
            //定位失败
            sb.append("定位失败" + "\n");
            sb.append("错误码:" + location.getErrorCode() + "\n");
            sb.append("错误信息:" + location.getErrorInfo() + "\n");
            sb.append("错误描述:" + location.getLocationDetail() + "\n");
        }
 
        return sb.toString();
    }
 
 
    /**
     * 经纬度转换具体位置
     */
    private String transAddress(Double latitude, Double longitude) {
        List<Address> addressList = null;
        Geocoder geocoder = new Geocoder(mContext);
        try {
            addressList = geocoder.getFromLocation(latitude, longitude, 1);
        } catch (IOException e) {
            e.printStackTrace();
        }
        if (addressList != null && !addressList.isEmpty()) {
            return addressList.get(0).getAddressLine(0);
        }
        return "未知位置";
    }
 
 
    @SuppressLint("MissingPermission")
    public void getBestLocation(LocationGetListener listener) {
        mListener = listener;
        Criteria criteria = new Criteria();
        Location location;
        String provider = mLocationManager.getBestProvider(criteria, true);
        if (TextUtils.isEmpty(provider)) {
            location = getNetWorkLocation(mContext);
        } else {
            location = mLocationManager.getLastKnownLocation(provider);
        }
        if (location == null) {
            provider = LocationManager.PASSIVE_PROVIDER;
            location = mLocationManager.getLastKnownLocation(provider);
            if (mListener != null) {
                mListener.getLocationInfo(location);
            }
 
            mLocationManager.requestLocationUpdates(provider, 5 * 1000, 5, LocationProvider.this);
        } else if (mListener != null) {
            mListener.getLocationInfo(location);
        }
    }
 
    @SuppressLint("MissingPermission")
    public Location getNetWorkLocation(Context context) {
        Location location = null;
 
        if (mLocationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)) {
 
            location = mLocationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
        }
        return location;
    }
 
    public void unregisterLocationUpdaterListener() {
        mLocationManager.removeUpdates(this);
    }
 
    @Override
    public void onLocationChanged(Location location) {
        if (mListener != null) {
            mListener.getLocationInfo(location);
        }
        unregisterLocationUpdaterListener();
    }
 
    @Override
    public void onStatusChanged(String provider, int status, Bundle extras) {
        Log.v("location:", provider + " onStatusChanged. status = " + status);
    }
 
    @Override
    public void onProviderEnabled(String provider) {
        Log.v("location:", provider + " onProviderEnabled");
    }
 
    @Override
    public void onProviderDisabled(String provider) {
        Log.v("location:", provider + " onProviderDisabled");
 
    }
}