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
package com.mm.android.deviceaddmodule.mobilecommon.location;
 
import android.content.Context;
import android.location.Address;
import android.location.Geocoder;
import android.location.Location;
import android.os.AsyncTask;
import android.text.TextUtils;
 
import com.mm.android.deviceaddmodule.mobilecommon.utils.LogUtil;
 
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
 
/**
 * AsyncTask for reverse geocoding coordinates into a physical address.
 * include country/city/street etc.
 */
public class FetchAddressTask extends AsyncTask<Location, Void, String> {
 
    private Context mContext;
    private OnGeoDecodeCompleted mListener;
    private final String TAG = "FetchAddressTask";
 
    public FetchAddressTask(Context applicationContext, OnGeoDecodeCompleted listener) {
        mContext = applicationContext;
        mListener = listener;
    }
 
    @Override
    protected String doInBackground(Location... params) {
        // Set up the geocoder
        Geocoder geocoder = new Geocoder(mContext, Locale.getDefault());
 
        // Get the passed in location
        Location location = params[0];
        List<Address> addresses = null;
        String resultMessage = "";
 
        if (location == null) {
            LogUtil.debugLog(TAG, "location has't got");
            return "";
        }
 
 
        try {
            // In this sample, get just a single address
            addresses = geocoder.getFromLocation(location.getLatitude(), location.getLongitude(), 1);
        } catch (IOException ioException) {
            // Catch network or other I/O problems
            LogUtil.errorLog(TAG, "Catch network or other I/O problems -> service not available", ioException);
        } catch (IllegalArgumentException illegalArgumentException) {
            // Catch invalid latitude or longitude values
            LogUtil.errorLog(TAG, "invalid latitude or longitude used" + ". " +
                    "Latitude = " + location.getLatitude() +
                    ", Longitude = " +
                    location.getLongitude(), illegalArgumentException);
        }
 
        // If no addresses found, print an error message.
        if (addresses == null || addresses.size() == 0) {
            if (resultMessage.isEmpty()) {
                LogUtil.debugLog(TAG, "no address found");
            }
            return "";
        }
        // If an address is found, read it into resultMessage
        Address address = addresses.get(0);
        ArrayList<String> addressParts = new ArrayList<>();
 
        // Fetch the address lines using getAddressLine,
        // join them, and send them to the thread
        for (int i = 0; i <= address.getMaxAddressLineIndex(); i++) {
            addressParts.add(address.getAddressLine(i));
        }
        resultMessage = address.getCountryCode();
        LogUtil.debugLog(TAG, "address info -> " + resultMessage + " || " + TextUtils.join("\n", addressParts));
 
        return resultMessage;
    }
 
    /**
     * Called once the background thread is finished and updates the
     * UI with the result.
     *
     * @param address The resulting reverse geocoded address, or error
     *                message if the task failed.
     */
    @Override
    protected void onPostExecute(String address) {
        mListener.onGeoDecodeCompleted(address);
        super.onPostExecute(address);
    }
 
    //反编码回调接口
    public interface OnGeoDecodeCompleted {
        void onGeoDecodeCompleted(String result);
    }
}