在通过ArrayList循环时在后台添加标记

时间:2014-08-27 00:32:12

标签: android arraylist google-maps-android-api-2 ui-thread

我从googleMap个对象中将标记添加到ArrayList。大约有250个标记;我甚至必须将它们转换为位图来定制它们。这是一项资源密集型任务。但它严重阻止了我的UI线程。

我是这样做的:

    final HashMap<Marker, NearLocation> markerIdMap = new HashMap<Marker, NearLocation>();
    for (final NearLocation result : MainActivity.nearLocationList) {

            // Do all the hard work here

    }

如何在地图加载后动态地以某种方式执行此操作,并在生成它们时填充它们?我不确定我是否可以通过在后台完成一些工作来做到这一点,然后当标记完成时将其移动到UI线程进行添加。

我知道如何使用AsyncTask进行个性化。虽然我在循环时不确定...

2 个答案:

答案 0 :(得分:8)

据我所知,添加标记不能在UI线程之外完成。

你可以做的是在后台执行所有准备工作(创建标记,转换为位图等等)。为了在添加标记时稍微省略UI线程,您可以放大并使用https://code.google.com/p/android-maps-extensions/仅显示可见标记或群集标记以降低数量,尽管250不是很多。

以下是关于该主题的回答:Add markers dynamically on Google Maps v2 for Android

我有一个应用程序,大约有4500个标记使用第一种方法运行得相当好(只要它没有快速缩小)。应该注意的是,在这里我选择使用进度条制作一个闪屏,在用户有机会打开地图之前准备好所有标记。

如果您想要一种简单的机制来选择周围标记而不使用第三方库,您可以执行以下操作:Android Maps v2 - animate camera to include most markers

刚才想到的一个想法,如果标记的创建确实非常昂贵,那就是将EventBus https://github.com/greenrobot/EventBus添加到您的项目中。

使用EventBus,您可以在onEventAsync()方法中对标记进行长时间的准备。在该方法中,只要标记准备就绪,将新标记发布到UI EventBus.getDefault().post(marker)并在onEventMainThread(Marker marker)中捕获它。在这里,您可以将标记保存在所有准备好的标记列表中,如果地图当前处于打开状态,请添加它。

以下是我用于在前面提到的应用程序中准备标记的一些代码。它用于在消防部门使用的应用程序中显示消防栓。首次启动时,所有消防栓都从一组CSV文件中读取,并且所有~4500消防栓的MarkerOptions都会被创建。毫无疑问,很多代码对你没用,只是留下它以防万一你可以从中受益:

private List<HydrantHolder> mHydrants;
private Map<HydrantType, List<MarkerOptions>> mMarkers;

private class ReadHydrantsFiles extends
        AsyncTask<Void, Integer, List<HydrantHolder>> {

    File[] hydrantsFiles = new File[0];

    // Before running code in separate thread
    @Override protected void onPreExecute() {

        loadStarted();

        String filepath = paths.PATH_LOCAL_HYDRANTS;

        File hydrantsPath = new File(filepath);
        hydrantsFiles = hydrantsPath.listFiles(new FilenameFilter() {

            @Override public boolean accept(File dir, String filename) {
                return filename.toLowerCase(Locale.ENGLISH).contains(
                        Constants.FILETYPE_CSV);
            }
        });

        int lineCount = 0;

        if (hydrantsFiles == null || hydrantsFiles.length == 0) {
            if (!preferences.isFirstStartUp()) {
                // TODO notify user
            }
            Log.e(TAG, "Missing hydrants");
            if (moduleCallback != null) {
                moduleCallback.doneLoadingModule(toString());
            }
            this.cancel(false);
        } else {

            for (File file : hydrantsFiles) {
                // store linecount for visual progress update
                lineCount += Files.lineCount(file);

            }

        }

    }

    // The code to be executed in a background thread.
    @Override protected List<HydrantHolder> doInBackground(Void... args) {
        List<HydrantHolder> all_hydrants = new ArrayList<HydrantHolder>();
        // Directory path here
        int totalLineProgress = 0;

        // // required
        int indexLatitude = modulePreferences.indexLatitude();
        int indexLongitude = modulePreferences.indexLongitude();
        // optional
        int indexAddress = modulePreferences.indexAddress();
        int indexAddressNumber = modulePreferences.indexAddressNumber();
        int indexAddressRemark = modulePreferences.indexAddressRemark();
        int indexRemark = modulePreferences.indexRemark();
        // decimals
        int latitude_decimal = modulePreferences.latitude_decimal();
        int longitude_decimal = modulePreferences.longitude_decimal();

        if (indexLatitude <= 0 || indexLongitude <= 0)
            return all_hydrants;

        for (File file : hydrantsFiles) {
            BufferedReader in = null;
            try {

                String hydrantspath = paths.PATH_LOCAL_HYDRANTS;
                File hydrantsPath = new File(hydrantspath);

                // Read File Line By Line
                in = new BufferedReader(new InputStreamReader(
                        new FileInputStream(file), "windows-1252"));
                String strLine;
                while ((strLine = in.readLine()) != null) {

                    totalLineProgress++;

                    String[] hydrantParts = strLine.split(";", -1);

                    String errors = "";
                    final String hydrantType = file.getName().replace(
                            Constants.FILETYPE_CSV, "");

                    File[] iconFiles = hydrantsPath
                            .listFiles(new FilenameFilter() {

                                @Override public boolean accept(File dir,
                                        String filename) {
                                    if (filename.contains(hydrantType)
                                            && filename
                                                    .contains(Constants.FILETYPE_PNG)) {
                                        return true;
                                    }
                                    return false;
                                }
                            });

                    HydrantHolder.Builder hb = new HydrantHolder.Builder();
                    if (hydrantParts.length >= 5) {

                        hb.setHydrantType(hydrantType);
                        if (iconFiles.length != 0) {
                            hb.setIconPath(hydrantspath
                                    + File.separatorChar
                                    + iconFiles[0].getName());
                        }
                        hb.setLatitude(hydrantParts[indexLatitude],
                                latitude_decimal);
                        hb.setLongitude(hydrantParts[indexLongitude],
                                longitude_decimal);

                        if (indexAddress > 0)
                            hb.setAddress(hydrantParts[indexAddress]);
                        if (indexAddressNumber > 0)
                            hb.setAddressNumber(hydrantParts[indexAddressNumber]);
                        if (indexAddressRemark > 0)
                            hb.setAddressRemark(hydrantParts[indexAddressRemark]);
                        if (indexRemark > 0)
                            hb.setRemark(hydrantParts[indexRemark]);

                        if (hb.getErrors().isEmpty()) {
                            HydrantHolder hydrant = hb.build();
                            all_hydrants.add(hydrant);
                        } else {
                            // TODO write error file to Dropbox if possible,
                            // otherwise locally
                            Log.d(TAG, errors);
                        }
                    } else {
                        errors = "Missing information";
                    }

                    publishProgress(totalLineProgress);
                }
            } catch (InvalidPathException e) {
                Log.e(TAG, e.getMessage(), e);
            } catch (IOException e) {
                Log.e(TAG, e.getMessage(), e);
            } finally {
                if (in != null) {
                    try {
                        in.close();
                    } catch (IOException e) {
                    }
                }
            }
        }
        Log.d(TAG, "" + all_hydrants.size());
        return all_hydrants;
    }

    // Update the progress
    @Override protected void onProgressUpdate(Integer... values) {
        // set the current progress of the progress dialog
        // if (progressDialog != null && values != null && values.length >
        // 0) {
        // progressDialog.setProgress(values[0]);
        // }
    }

    @Override protected void onPostExecute(List<HydrantHolder> hydrants) {
        // Device.releaseOrientation((Activity) context);

        Log.d(TAG, "Saved " + hydrants.size() + " hydrants");
        mHydrants = hydrants;

        new PrepareMarkerOptionsTask(hydrants).execute();

        super.onPostExecute(hydrants);
    }

}

private class PrepareMarkerOptionsTask extends
        AsyncTask<Void, Integer, Map<HydrantType, List<MarkerOptions>>> {
    // Before running code in separate thread
    List<HydrantHolder> mHydrants;

    public PrepareMarkerOptionsTask(List<HydrantHolder> hydrants) {
        this.mHydrants = hydrants;
        mMarkers = new HashMap<HydrantsModule.HydrantType, List<MarkerOptions>>();
    }

    @Override protected void onPreExecute() {

    }

    // The code to be executed in a background thread.
    @Override protected Map<HydrantType, List<MarkerOptions>> doInBackground(
            Void... arg) {

        for (HydrantHolder hydrant : mHydrants) {

            final String hydrant_type = hydrant.getHydrantType();
            final String hydrant_icon_path = hydrant.getIconPath();
            double latitude = hydrant.getLatitude();
            double longitude = hydrant.getLongitude();

            final LatLng position = new LatLng(latitude, longitude);

            final String address = hydrant.getAddress();
            final String addressNumber = hydrant.getAddressNumber();
            final String addressremark = hydrant.getAddressRemark();
            final String remark = hydrant.getRemark();

            // Log.d(TAG, hydrant.toString());

            BitmapDescriptor icon = BitmapDescriptorFactory
                    .defaultMarker(BitmapDescriptorFactory.HUE_RED);

            if (!hydrant_icon_path.isEmpty()) {
                File iconfile = new File(hydrant_icon_path);
                if (iconfile.exists()) {
                    BitmapDescriptor loaded_icon = BitmapDescriptorFactory
                            .fromPath(hydrant_icon_path);
                    if (loaded_icon != null) {
                        icon = loaded_icon;
                    } else {
                        Log.e(TAG, "loaded_icon was null");
                    }
                } else {
                    Log.e(TAG, "iconfile did not exist: "
                            + hydrant_icon_path);
                }
            } else {
                Log.e(TAG, "iconpath was empty on hydrant type: "
                        + hydrant_type);
            }

            StringBuffer snippet = new StringBuffer();
            if (!address.isEmpty())
                snippet.append("\n" + address + " " + addressNumber);
            if (addressremark.isEmpty())
                snippet.append("\n" + addressremark);
            if (!remark.isEmpty())
                snippet.append("\n" + remark);

            addHydrantMarker(
                    hydrant_type,
                    new MarkerOptions().position(position)
                            .title(hydrant_type)
                            .snippet(snippet.toString()).icon(icon));

            // publishProgress(markers.size());
        }
        return mMarkers;
    }

    // Update the progress
    @Override protected void onProgressUpdate(Integer... values) {
    }

    @Override protected void onCancelled() {
    }

    // after executing the code in the thread
    @Override protected void onPostExecute(
            Map<HydrantType, List<MarkerOptions>> markers) {

        Log.d(TAG, "Prepared " + markers.size() + " hydrantMarkerOptions");
        mMarkers = markers;

        loadEnded();

        super.onPostExecute(markers);

    }

}

public Set<HydrantType> getHydrantTypes() {
    return new HashSet<HydrantType>(mMarkers.keySet());
}

private void addHydrantMarker(String typeName, MarkerOptions marker) {
    HydrantType type = new HydrantType(typeName, marker.getIcon());
    if (mMarkers.containsKey(type)) {
        List<MarkerOptions> markers = mMarkers.get(type);
        markers.add(marker);
    } else {
        List<MarkerOptions> markers = new ArrayList<MarkerOptions>();
        markers.add(marker);
        mMarkers.put(type, markers);
        enableHydrantType(type);
    }
}

public class HydrantType {

    private final String name;
    private final BitmapDescriptor icon;

    public HydrantType(String name, BitmapDescriptor icon) {
        this.name = name;
        this.icon = icon;
    }

    public String getName() {
        return name;
    }

    public BitmapDescriptor getIcon() {
        return icon;
    }

    @Override public int hashCode() {
        return name.hashCode();
    }

    @Override public boolean equals(Object o) {
        if (o instanceof HydrantType) {
            if (((HydrantType) o).name.equals(name)) {
                return true;
            }
        }
        return false;
    }

} 

根据评论,我添加了更多的文字和代码。

是的,我一次添加所有MarkerOptions。但是,因为我在添加所有标记之前放大了GoogleMaps-extensions(第一个链接),只花费CPU力量将可见的标记添加到地图中。如果用户平移地图或缩小,则会自动添加更多可见标记。

制作地图延迟加载标记:

@Override
public void onMapReady(GoogleMap map) {
    Log.i(TAG, "onMapReady");
    if (map != null) {
        map.setClustering(new ClusteringSettings().enabled(false)
                .addMarkersDynamically(true));
    }
}

我用来添加消防栓的代码(现在这在很多情况下都过于复杂,但如果仔细阅读,只需缩放到一个地址并在缩放完成后添加所有消防栓):

public void addHydrantsNearAddress(final AddressHolder addressHolder,
        final boolean zoomToAddress) {

    final GoogleMap map = googlemaps.getMap();


    final OnCameraChangeListener addHydrantsAfterZoom = new OnCameraChangeListener() {

        @Override public void onCameraChange(CameraPosition cameraPosition) {
            Log.d(TAG, cameraPosition.target.toString());
            Log.d(TAG, addressHolder.position.toString());

            final GoogleMap map = googlemaps.getMap();
            // if (Location.distanceBetween(cameraPosition.tar,
            // startLongitude, endLatitude, endLongitude, results)) {
            new Handler().postDelayed(new Runnable() {

                @Override public void run() {
                    addAllHydrants();
                }
            }, 500);

            map.setOnCameraChangeListener(null); // unregister
            // }
        }
    };

    if (map == null) {
        // wait for the map to be ready before adding hydrants
        googlemaps.setGoogleMapsCallback(new GoogleMapsCallback() {

            @Override public void mapReady(GoogleMap map) {

                if (zoomToAddress) {
                    map.setOnCameraChangeListener(addHydrantsAfterZoom);
                    googlemaps.zoomTo(addressHolder);
                } else {
                    addAllHydrants();
                }

                googlemaps.setGoogleMapsCallback(null); // unregister
            }
        });
    } else {

        if (zoomToAddress) {
            // only setOnCameraChangeListener if cammera needs to move
            LatLng cammeraPos = map.getCameraPosition().target;
            LatLng addressPos = addressHolder.position;

            float[] results = new float[1];
            Location.distanceBetween(cammeraPos.latitude,
                    cammeraPos.longitude, addressPos.latitude,
                    addressPos.longitude, results);

            // Log.d(TAG, "distance " + results[0]);

            if (results[0] > 1) {
                map.setOnCameraChangeListener(addHydrantsAfterZoom);
                googlemaps.zoomTo(addressHolder);
            } else {
                googlemaps.zoomTo(addressHolder);
                addAllHydrants();
            }

        }

    }

}

为了使示例更加复杂,另一个原因是我让用户在消防栓类型之间进行过滤。希望你能看到这个想法尽管如此。

addAllHydrants()就像听起来一样简单,迭代MarkerOptions并添加它们:

public void addAllHydrants() {

    enableAllHydrants();

    GoogleMap map = googlemaps.getMap();

    map.setTrafficEnabled(modulePreferences.showTraffic());
    map.setMapType(modulePreferences.getMapType());

    addHydrants(map);
}

private void addHydrants(GoogleMap map) {

    Log.d(TAG, "addHydrants");

    if (mHydrants == null || mHydrants.isEmpty()) {
        Toast.makeText(context,
                context.getString(R.string.missing_hydrants),
                Toast.LENGTH_LONG).show();
        return;
    } else {
        for (MarkerOptions marker : getEnabledHydrantMarkers()) {
            map.addMarker(marker);
        }
    }

}

我真的认为你自己会过度复杂化,试图自己计算,获取和添加可见标记,而不仅仅是。

  1. 准备MarkerOptions(第一个代码blob中的示例)
  2. 使用onMapReady回调等待地图准备就绪(此示例来自Google的官方示例)
  3. 在新创建的地图上设置addMarkersDynamically(true)
  4. 放大感兴趣的地址或区域(例如设备位置)
  5. 添加所有MarkerOptions
  6. 查看在地图上移动时自动添加的标记
  7. 回答关于onMapReady回调的问题

    如果您使用XML添加地图,那么我怀疑您可能不需要添加回调。直接在您的活动onCreate()中尝试使用地图。只要对getExtendedMap()的调用不为空,就应该没问题。

    我在代码中创建片段,以便能够将控制器代码与地图放在一起。因此,我的SupportMapFragment看起来像这样:

    public class GoogleMapFragment extends SupportMapFragment {
    
        private OnGoogleMapFragmentListener mCallback;
    
        public GoogleMapFragment() {
            mCallback = null;
        }
    
        public static interface OnGoogleMapFragmentListener {
            void onMapReady(GoogleMap map);
        }
    
        @Override public void onAttach(Activity activity) {
            super.onAttach(activity);
            try {
                mCallback = (OnGoogleMapFragmentListener) activity;
            } catch (ClassCastException e) {
                throw new ClassCastException(getActivity().getClass().getName()
                        + " must implement OnGoogleMapFragmentListener");
            }
    
        }
    
        @Override public View onCreateView(LayoutInflater inflater,
                ViewGroup container, Bundle savedInstanceState) {
            View view = super.onCreateView(inflater, container, savedInstanceState);
            if (mCallback != null) {
                mCallback.onMapReady(getExtendedMap());
            }
    
            return view;
        }
    
    }
    

答案 1 :(得分:1)

只是想分享一个我想出的简单解决方案,用于显示所有标记而不必每次都重新加载。

基本上我依靠onCameraChangeListener检查用户的地图视图是否发生了变化,只显示该视图中的标记,否则标记将被隐藏......您仍然会有更长的加载时间第一次加载数据时,之后就很快了。

首先,您需要将所有标记添加到地图中,并将所有标记存储在Map

// Put it in onCreate or something
map.clear(); // Clears markers
markers.clear(); // private Map<Marker, Item> markers = new HashMap<Marker, Item>()
for (Item item : items) {
    MarkerOptions markerOptions = new MarkerOptions()
        .position(new LatLng(item.getLatitude(), item.getLongitude())
        .title(item.getTitle())
        .snippet(item.getSnippet());
    Marker marker = map.addMarker(marketOptions);
    markers.put(marker, item);
}

然后你需要设置监听器:

// Put in onCreate or something
map.setOnCameraChangeListener(new GoogleMap.OnCameraChangeListener() {
    @Override
    public void onCameraChange(final CameraPosition cameraPosition) {
        showMarkers(cameraPosition.target);
        LatLngBounds bounds = map.getProjection().getVisibleRegion().latLngBounds;
        for (Map.Entry<Marker, Item> entry : markers) {
            Marker marker = entry.getKey();
            if (bounds.contains(marker.getPosition()) {
                marker.setVisible(true);
            } else {
                marker.setVisible(false);
            }
        }
    }
});

这应该在animationMap结束时以及任何形式的相机移动(以编程方式或通过用户拖动)触发

您可以考虑使用onSaveInstanceState保留数据,以便在应用之间切换时提高响应速度