gpt4 book ai didi

android - 有什么方法可以异步调用 Geocoder.getFromLocation 吗?

转载 作者:太空狗 更新时间:2023-10-29 14:17:52 30 4
gpt4 key购买 nike

我有一对坐标,我正在使用 Geocoder.getFromLocation获取地址详细信息。但是这种方法是同步的,并且会卡住应用程序一段时间。我试图将它放在 AsyncTask 中,但我得到了 java.lang.IllegalStateException: Not on the main thread

有什么方法可以异步调用这个方法吗?

这是检索地址的方法(在 MapUtil 类中):

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;
}

这是我在 MainActivity 的 AsyncTask:

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();

这是完整的堆栈跟踪:

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)

最佳答案

只是突出解决方案:

marker.getPosition() 的调用需要在主线程上执行。如堆栈跟踪错误所述:

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)
...

因此,解决方案只是在 doInBackground() 调用之前获取 Position,例如在我的 AsyncTask 中的 onPreExecute 方法上。我还更改了 getAddress() 方法以直接接收 Position 而不是 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();

关于android - 有什么方法可以异步调用 Geocoder.getFromLocation 吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19950040/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com