gpt4 book ai didi

android - 使用 ASYNC Task 加载 map 图钉

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:03:27 26 4
gpt4 key购买 nike

我已成功将 map 图钉添加到我的 map 叠加层中,但它加载缓慢并且在 map 上移动缓慢。我想是在它从服务器下载 map 图钉的时候。这些引脚的大小只有 20-30 kb,但有时可能需要添加 25 个引脚。我已经尝试在后台添加图钉,但它会破坏应用程序。如何在不影响性能的情况下在后台添加图钉?

当用户触摸 map 并执行此 ASYNC 任务时,我从 map 获取位置。

    private class mapStations extends AsyncTask<Void, Void, JSONObject> {

@Override
protected JSONObject doInBackground(Void... arg0) {

JSONObject obj = null;
try {
obj = new JSONObject(API.nearByStations(pxLat, pxLng, 0));

} catch (JSONException e) {
e.printStackTrace();
}

return obj;
}

@Override
protected void onPostExecute(JSONObject details) {
String tag = "mapStations";
JSONArray stations;
Drawable d = null;
try {
mapOverlays = mapView.getOverlays();
mapOverlays.clear();
stations = details.getJSONArray("stations");

for (int j = 0; j < stations.length(); j++) {
JSONObject jsonObject = stations.getJSONObject(j);
Log.i(tag, "url: " + jsonObject.getString("logo"));
try {

Bitmap staticImage = BitmapFactory
.decodeStream((InputStream) new URL(jsonObject.getString("logo"))
.getContent());
d = new BitmapDrawable(staticImage);

} catch (MalformedURLException e) {
Log.e(tag, e.toString());
e.printStackTrace();
} catch (IOException e) {
Log.e(tag, e.toString());
e.printStackTrace();
}
add(jsonObject.getDouble("lat"),
jsonObject.getDouble("lng"), d);
}

} catch (JSONException e) {
e.printStackTrace();
}

}
}

public void add(double lat, double lng, Drawable d) {

mapOverlays = mapView.getOverlays();
if(d.equals(null)){
Log.i(tag, "d was null");
d = this.getResources().getDrawable(R.drawable.androidmarker);
}

itemizedOverlay = new MapsOverlay(d);

GeoPoint point = new GeoPoint((int) (lat * 1e6), (int) (lng * 1e6));
OverlayItem overlayitem = new OverlayItem(point, "", "");

itemizedOverlay.addOverlay(overlayitem);
mapOverlays.add(itemizedOverlay);
}

这是在doinbackground中添加点的logcat。它不会每次都发生,但确实会发生。当它发生时我似乎也无法重现。有时当我缩小时会发生这种情况,有时当我在 map 上移动时会发生这种情况。我在这里有类似的问题Android MapView JSON Array Adding Array of points但我最终将其移至执行后。就像我之前提到的,加载引脚需要一段时间。我现在有点不知所措。我整天都在为此工作。

    [12-23 13:46:27.246: E/AndroidRuntime(1359): Uncaught handler: thread main exiting due to uncaught exception
12-23 13:46:27.286: E/AndroidRuntime(1359): java.util.ConcurrentModificationException
12-23 13:46:27.286: E/AndroidRuntime(1359): at java.util.AbstractList$SimpleListIterator.next(AbstractList.java:64)
12-23 13:46:27.286: E/AndroidRuntime(1359): at com.google.android.maps.OverlayBundle.draw(OverlayBundle.java:41)
12-23 13:46:27.286: E/AndroidRuntime(1359): at com.google.android.maps.MapView.onDraw(MapView.java:476)
12-23 13:46:27.286: E/AndroidRuntime(1359): at android.view.View.draw(View.java:6535)
12-23 13:46:27.286: E/AndroidRuntime(1359): at android.view.ViewGroup.drawChild(ViewGroup.java:1531)
12-23 13:46:27.286: E/AndroidRuntime(1359): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1258)
12-23 13:46:27.286: E/AndroidRuntime(1359): at android.view.ViewGroup.drawChild(ViewGroup.java:1529)
12-23 13:46:27.286: E/AndroidRuntime(1359): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1258)
12-23 13:46:27.286: E/AndroidRuntime(1359): at android.view.View.draw(View.java:6538)
12-23 13:46:27.286: E/AndroidRuntime(1359): at android.widget.FrameLayout.draw(FrameLayout.java:352)
12-23 13:46:27.286: E/AndroidRuntime(1359): at android.view.ViewGroup.drawChild(ViewGroup.java:1531)
12-23 13:46:27.286: E/AndroidRuntime(1359): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1258)
12-23 13:46:27.286: E/AndroidRuntime(1359): at android.view.ViewGroup.drawChild(ViewGroup.java:1529)
12-23 13:46:27.286: E/AndroidRuntime(1359): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1258)
12-23 13:46:27.286: E/AndroidRuntime(1359): at android.view.ViewGroup.drawChild(ViewGroup.java:1529)
12-23 13:46:27.286: E/AndroidRuntime(1359): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1258)
12-23 13:46:27.286: E/AndroidRuntime(1359): at android.view.ViewGroup.drawChild(ViewGroup.java:1529)
12-23 13:46:27.286: E/AndroidRuntime(1359): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1258)
12-23 13:46:27.286: E/AndroidRuntime(1359): at android.view.ViewGroup.drawChild(ViewGroup.java:1529)
12-23 13:46:27.286: E/AndroidRuntime(1359): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1258)
12-23 13:46:27.286: E/AndroidRuntime(1359): at android.view.View.draw(View.java:6538)
12-23 13:46:27.286: E/AndroidRuntime(1359): at android.widget.FrameLayout.draw(FrameLayout.java:352)][1]
12-23 13:46:27.286: E/AndroidRuntime(1359): at android.view.ViewGroup.drawChild(ViewGroup.java:1531)
12-23 13:46:27.286: E/AndroidRuntime(1359): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1258)
12-23 13:46:27.286: E/AndroidRuntime(1359): at android.view.ViewGroup.drawChild(ViewGroup.java:1529)
12-23 13:46:27.286: E/AndroidRuntime(1359): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1258)
12-23 13:46:27.286: E/AndroidRuntime(1359): at android.view.View.draw(View.java:6538)
12-23 13:46:27.286: E/AndroidRuntime(1359): at android.widget.FrameLayout.draw(FrameLayout.java:352)
12-23 13:46:27.286: E/AndroidRuntime(1359): at com.android.internal.policy.impl.PhoneWindow$DecorView.draw(PhoneWindow.java:1830)
12-23 13:46:27.286: E/AndroidRuntime(1359): at android.view.ViewRoot.draw(ViewRoot.java:1349)
12-23 13:46:27.286: E/AndroidRuntime(1359): at android.view.ViewRoot.performTraversals(ViewRoot.java:1114)
12-23 13:46:27.286: E/AndroidRuntime(1359): at android.view.ViewRoot.handleMessage(ViewRoot.java:1633)
12-23 13:46:27.286: E/AndroidRuntime(1359): at android.os.Handler.dispatchMessage(Handler.java:99)
12-23 13:46:27.286: E/AndroidRuntime(1359): at android.os.Looper.loop(Looper.java:123)
12-23 13:46:27.286: E/AndroidRuntime(1359): at android.app.ActivityThread.main(ActivityThread.java:4363)
12-23 13:46:27.286: E/AndroidRuntime(1359): at java.lang.reflect.Method.invokeNative(Native Method)
12-23 13:46:27.286: E/AndroidRuntime(1359): at java.lang.reflect.Method.invoke(Method.java:521)
12-23 13:46:27.286: E/AndroidRuntime(1359): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
12-23 13:46:27.286: E/AndroidRuntime(1359): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
12-23 13:46:27.286: E/AndroidRuntime(1359): at dalvik.system.NativeStart.main(Native Method)

最佳答案

为了防止用户在列表已经被编辑时无意中编辑列表。我决定在加载引脚时将对话框放在 onPreExecute() 中。这阻止了用户在添加新图钉时触摸 map 。在我有时间想出一个更好的主意来防止这种情况发生之前,我必须这样做。

我在开始添加新图钉之前清除了 map 上的叠加层,这是在 onPreExecute() 期间完成的。在 onPostExecute() 上,我关闭了对话框并 invalidate() map 。这将在将新 map 图钉添加到列表后在 map 上绘制它们。

@Override
protected void onPreExecute() {
Log.i(tag, "AsyncTask Started");
dialog.show();
mapOverlays = mapView.getOverlays();
mapOverlays.clear();
}

@Override
protected void onPostExecute(Void args) {
mapView.invalidate();
dialog.dismiss();
Log.i(tag, "AsyncTask Post");
}

关于android - 使用 ASYNC Task 加载 map 图钉,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8618728/

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