简体   繁体   中英

Is there any way to call Geocoder.getFromLocation asynchronously?

I have a pair of coordinates and I'm using Geocoder.getFromLocation to get the address details. But this method is synchronous and freezes for some time the app. I tried to put it inside an AsyncTask, but I get an java.lang.IllegalStateException: Not on the main thread .

Are there any way to call this method asynchronously?

Here is the method to retrieve the address (inside an MapUtil class):

public static Address getAddress(Context context, Marker marker) {
    Geocoder geocoder = new Geocoder(context);
    Address address = null;
    try {
        List<Address> addressList = geocoder.getFromLocation(marker.getPosition().latitude, marker.getPosition().longitude, 1);

        if (addressList != null && addressList.size() > 0) {
            address = addressList.get(0);
        }
    } catch (Exception e) {
        e.printStackTrace();
    }

    return address;
}

Here is my AsyncTask at MainActivity:

new AsyncTask<Void, Void, Void>() {
    Address address = null;

    @Override
    protected Void doInBackground(Void... params) {
        address = MapUtil.getAddress(MainActivity.this, marker);
        return null;
    }

    @Override
    protected void onPostExecute(Void result) {
        super.onPostExecute(result);
        System.out.println("Address = " + address);
    }
}.execute();

And here is the full stack trace:

11-13 06:36:12.423: W/System.err(17247): java.lang.IllegalStateException: Not on the main thread
11-13 06:36:12.423: W/System.err(17247):    at maps.aq.o.b(Unknown Source)
11-13 06:36:12.423: W/System.err(17247):    at maps.ak.g.b(Unknown Source)
11-13 06:36:12.423: W/System.err(17247):    at maps.af.bk.c(Unknown Source)
11-13 06:36:12.423: W/System.err(17247):    at cpd.onTransact(SourceFile:80)
11-13 06:36:12.423: W/System.err(17247):    at android.os.Binder.transact(Binder.java:347)
11-13 06:36:12.423: W/System.err(17247):    at com.google.android.gms.maps.model.internal.d$a$a.getPosition(Unknown Source)
11-13 06:36:12.423: W/System.err(17247):    at com.google.android.gms.maps.model.Marker.getPosition(Unknown Source)
11-13 06:36:12.423: W/System.err(17247):    at com.example.util.MapUtil.getAddress(MapUtil.java:17)
11-13 06:36:12.423: W/System.err(17247):    at com.example.MainActivity$3.doInBackground(MainActivity.java:102)
11-13 06:36:12.423: W/System.err(17247):    at com.example.MainActivity$3.doInBackground(MainActivity.java:1)
11-13 06:36:12.423: W/System.err(17247):    at android.os.AsyncTask$2.call(AsyncTask.java:287)
11-13 06:36:12.423: W/System.err(17247):    at java.util.concurrent.FutureTask.run(FutureTask.java:234)
11-13 06:36:12.423: W/System.err(17247):    at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
11-13 06:36:12.423: W/System.err(17247):    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
11-13 06:36:12.423: W/System.err(17247):    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
11-13 06:36:12.423: W/System.err(17247):    at java.lang.Thread.run(Thread.java:841)

Just highlighting the solution:

The call to marker.getPosition() needs to be performed on the main thread. As said on the stack trace error:

11-13 06:36:12.423: W/System.err(17247): java.lang.IllegalStateException: Not on the main thread
11-13 06:36:12.423: W/System.err(17247):    at maps.aq.o.b(Unknown Source)
11-13 06:36:12.423: W/System.err(17247):    at maps.ak.g.b(Unknown Source)
11-13 06:36:12.423: W/System.err(17247):    at maps.af.bk.c(Unknown Source)
11-13 06:36:12.423: W/System.err(17247):    at cpd.onTransact(SourceFile:80)
11-13 06:36:12.423: W/System.err(17247):    at android.os.Binder.transact(Binder.java:347)
11-13 06:36:12.423: W/System.err(17247):    at com.google.android.gms.maps.model.internal.d$a$a.getPosition(Unknown Source)
11-13 06:36:12.423: W/System.err(17247):    at com.google.android.gms.maps.model.Marker.getPosition(Unknown Source)
...

So, the solution was just get the Position before the doInBackground() call, for example on the onPreExecute method inside my AsyncTask. Also I changed the getAddress() method to receive directly a Position instead of the Marker.

new AsyncTask<Void, Void, Void>() {
    Address address = null;
    Position position = null;

    @Override
    protected Void doInBackground(Void... params) {
        position = marker.getPosition();
        address = MapUtil.getAddress(MainActivity.this, position);
        return null;
    }

    @Override
    protected void onPostExecute(Void result) {
        super.onPostExecute(result);
        System.out.println("Address = " + address);
    }
}.execute();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM