java Android - google maps v2 - 从字符串/ coor加载地址的任务。

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java Android - google maps v2 - 从字符串/ coor加载地址的任务。相关的知识,希望对你有一定的参考价值。

public class AddressFromStringTask extends AsyncTask<String, Void, android.location.Address> {

    private WeakReference<AddressCallback> mWeakListener;
    private Exception exception;

    public AddressFromStringTask(AddressCallback listener) {
        mWeakListener = new WeakReference<>(listener);
    }

    // Decode image in background.
    @Override protected Address doInBackground(String... params) {
        Geocoder coder = new Geocoder(App.getInstance().getApplicationContext(), Locale.getDefault());

        try {
            ArrayList<android.location.Address> addresses = (ArrayList<android.location.Address>)
                    coder.getFromLocationName(params[0], 1);

            if (addresses != null && addresses.size() > 0) {
                return addresses.get(0);
            }
            exception = new IOException("No valid locations found for that address");
        } catch (IOException e) {
            exception = e;
        }
        return null;
    }

    @Override protected void onPostExecute(Address address) {
        AddressCallback listener = mWeakListener.get();
        if (listener != null) {
            if (exception == null){
                listener.onLoad(address);
            } else{
                listener.onError(exception);
            }
        }
    }
}
public class AddressFromLocationTask extends AsyncTask<LatLng, Void, android.location.Address> {

    private WeakReference<AddressCallback> mWeakCallback;
    private Exception exception;

    public AddressFromLocationTask(AddressCallback callback) {
        mWeakCallback = new WeakReference<>(callback);
    }

    // Decode image in background.
    @Override protected android.location.Address doInBackground(LatLng... params) {
        Geocoder coder = new Geocoder(App.getInstance().getApplicationContext(), Locale.getDefault());

        try {
            LatLng latLng = params[0];
            ArrayList<android.location.Address> addresses = (ArrayList<android.location.Address>)
                    coder.getFromLocation(latLng.latitude, latLng.longitude, 1);

            if (addresses != null && addresses.size() > 0) {
                return addresses.get(0);
            }
            exception = new IOException("No valid locations found for that address");
        } catch (IOException e) {
            exception = e;
        }
        return null;
    }

    @Override protected void onPostExecute(android.location.Address address) {
        AddressCallback callback = mWeakCallback.get();
        if (callback != null) {
            if (exception == null){
                callback.onLoad(address);
            } else{
                callback.onError(exception);
            }
        }
    }
}
public interface AddressCallback {
    void onLoad(android.location.Address address);
    void onError(Exception e);
}

以上是关于java Android - google maps v2 - 从字符串/ coor加载地址的任务。的主要内容,如果未能解决你的问题,请参考以下文章

Android 26无法启动接收器com.google.android.gcm.GCMBroadcastReceiver:java.lang.IllegalStateException:不允许启动服务

java.lang.NoClassDefFoundError:解析失败:Lcom/google/android/maps/GeoPoint

下载编译 Android wear 源代码,尝试制作可穿戴设备功能

java.lang.IllegalArgumentException:组件类 com.google.android.gms.wallet.ocr.CardRecognitionShimProxyAct

无法获取提供程序 com.google.android.gms.ads.MobileAdsInitProvider:java.lang.IllegalStateException

Android:java.lang.IllegalStateException:不允许接收者接收 com.google.android.c2dm.permission.SEND