【问题标题】:android load multiple markers block my UIandroid加载多个标记阻止我的UI
【发布时间】:2015-06-29 05:55:44
【问题描述】:

我在 markerarray 列表中有大约 11000 个标记,当我尝试加载地图时它会阻塞我的 UI,即使我使用 Thread 和 RunOnUi 线程,还有其他更好的方法可以尝试吗??

 Thread t = new Thread(new Runnable() {

                @Override
                public void run() {
                    for (int i = 0; i < list.size(); i++) {

                        markerOptionList.add(new MarkerOptions().position(
                                new LatLng(Double.parseDouble(list.get(i)
                                        .getLatitude()), Double
                                        .parseDouble(list.get(i)
                                                .getLangtitude()))).title(
                                list.get(i).getName() + "~"
                                        + list.get(i).getCity() + "~"
                                        + list.get(i).getSector() + "~"
                                        + String.valueOf(false)));
                    }

                    getActivity().runOnUiThread(new Runnable() {

                        @Override
                        public void run() {
                            addMarker();
                        }
                    });

                }
            });
            t.start();

添加标记是我在地图中添加标记的方法

请提出更好的实施方式 提前致谢

【问题讨论】:

  • 您不应该一次添加所有标记。而是按需添加它们。您可以通过使用缩放级别和地图的可见部分来确定何时加载哪些标记来做到这一点。因此,在较低的缩放级别下,您可以按区域或类似的方式对标记进行分组,并且只为该区域显示一个标记,并且随着缩放的增加,增加该特定区域的数量。
  • 我认为 Android 中的 AsyncTask 可能会对您有所帮助
  • 在 for 循环外创建 MarkerOption 对象。 MarkerOptions markerOptions=new MarkerOptions();
  • 带有 Progressdialog 的异步任务对我有帮助

标签: android google-maps-markers


【解决方案1】:

对于您提到的标记数量(11000),强烈建议使用标记聚类。你可以阅读更多关于它的信息:https://developers.google.com/maps/documentation/android-api/utility/marker-clustering

如果您仍然想在没有标记聚类的情况下执行此操作,有一个简单的技巧可以改善用户体验。

添加标记会修改 UI - 它必须在主 UI 线程中完成。一次添加太多会阻塞主线程,因此应用程序在忙于添加标记时不会响应任何用户操作。最终用户可能会觉得应用程序已挂起或停止,甚至可能导致ANR

诀窍是逐渐添加标记,为主线程留下一些中断来处理其他操作,以便应用程序始终保持响应。

我会建议这样的方法:

public class DrawMarkersTask extends AsyncTask<Void, MarkerOptions, Void> {
    protected void onPreExecute() {
        // this method executes in UI thread
        googleMap.clear(); // you can clear map here for example
    }

    @Override
    protected Void doInBackground(Void... params) {
        // this method executes in separate background thread
        // you CANT modify UI here
        for (int i = 0; i < list.size(); i++) {
            // prepare your marker here
            MarkerOptions markerOptions = new MarkerOptions().position(
                            new LatLng(Double.parseDouble(list.get(i)
                                    .getLatitude()), Double
                                    .parseDouble(list.get(i)
                                            .getLongtitude()))).title(
                            list.get(i).getName() + "~"
                                    + list.get(i).getCity() + "~"
                                    + list.get(i).getSector() + "~"
                                    + String.valueOf(false)));

            publishProgress(markerOptions); // pass it for the main UI thread for displaying
            try {
                Thread.sleep(50); // sleep for 50 ms so that main UI thread can handle user actions in the meantime
            } catch (InterruptedException e) {
                // NOP (no operation)
            }
        }
        return null;
    }

    protected void onProgressUpdate(MarkerOptions... markerOptions) {
        // this executes in main ui thread so you can add prepared marker to your map
        googleMap.addMarker(markerOptions[0]);
    }

    protected void onPostExecute(Void result) {
        // this also executes in main ui thread
    }
}

请注意,DrawMarkersTask 必须是 Activity 的内部类,因此它可以直接访问 UI。如果你想让它成为单独的类,请遵循:android asynctask sending callbacks to ui

然后你在不阻塞主线程的情况下绘制标记:

DrawMarkersTask drawMarkersTask = new DrawMarkersTask();
drawMarkersTask.execute();

注意:这只是一个技巧。它与减少或优化此问题的复杂性无关,它只是通过及时传播操作将其隐藏在用户之外。然而,这种方法有时仍然是可以接受的折衷方案。

【讨论】:

  • 这种方法对我很有效。我设置了Thread.sleep(1),并且仍然允许在加载标记时进行大量的用户交互。感谢您的全面回答。
【解决方案2】:

加载 11000 个制造商对设备来说是一项非常繁重的工作。即使您可以将它们全部添加到地图中,当您缩小时,您也可能会遇到同样的问题。 有一个official Google article 关于各种高级标记管理技术。使用其中一个来仅显示必要的标记而不是全部。

【讨论】:

  • @Madhu 它不需要您删除任何标记。您可以在不同的缩放级别上显示它们。即使您可以一次全部显示它们,它们仍然会相互重叠。
猜你喜欢
  • 2011-09-07
  • 2011-08-26
  • 1970-01-01
  • 2021-08-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-11-16
相关资源
最近更新 更多