Android google maps async task add markers

夙愿已清 提交于 2019-12-04 08:34:37

While your intentions here are good, I just dont see much gain from it because you are still calling back to the UI thread for every single element and I bet you wont see much of a difference in performance. Having said that I am willing to bet your problem is that you are holding onto the cursor and using that in the onProgressUpdate while your doInBackground continues on movine the cursor to the next row. Which would explain why when you debug it works correctly because you are stepping along.

What I would recommend for performance increase would be to in your doInBackground create a list of some object that holds all the information about the map object (icon,position, etc..). Only add the markers to the map where they are in the visible bounding box of the map so you are not plotting markers that are not even visible. then in your onPostExecute use that list and plot everything there.

While I dont know for sure if this is the cause of the problem but it could very well likely be the problem

I do this by creating a list of MarkerOptions in the asynctask then in the onPostExecute I add these to the map in a loop. Actually I have two tasks that run syncronously. Task1 sets up the list of markeroptions and when it finishes task2 starts and gets / downloads any icon associated with the marker. I do the downloads at the end because chances are you'll reuse markers and you can use a hash to get a previously downloaded marker. You'd probably just be using the 2nd async task which gets the marker icons so as small favor I've included the class I use to get the bitmaps for markers.

Here's a custom class to handle the crp.

package com.gosylvester.bestrides.beans;

import com.google.android.gms.maps.model.MarkerOptions;

public class KmlMarkerOptions  {

    public MarkerOptions markeroptions;
    public String href;
    public int hrefhash =-1;
}

here's the 2nd async task that loads the bitmaps for the markers.

private class LoadMarkerBitmapDescriptor extends
            AsyncTask<List<KmlMarkerOptions>, Void, Boolean> {

        @Override
        protected Boolean doInBackground(List<KmlMarkerOptions>... params) {
            Boolean ret = true;
            List<KmlMarkerOptions> kmlmarkeroptions = params[0];
            int markerSize = kmlmarkeroptions.size();
            for (int currentMarkerPosition = 0; currentMarkerPosition < markerSize; currentMarkerPosition++) {
                KmlMarkerOptions currentMarkerOptions = kmlmarkeroptions
                        .get(currentMarkerPosition);
                int hrefhash = currentMarkerOptions.hrefhash;
                if (hrefhash != -1) {
                    // not the poistion zero marker
                    if (currentMarkerPosition != 0) {
                        // search for an existing bitmap descriptor
                        for (int previousMarkerPosition = 0; previousMarkerPosition < currentMarkerPosition; previousMarkerPosition++) {
                            KmlMarkerOptions previousMarkerOptions = kmlmarkeroptions
                                    .get(previousMarkerPosition);
                            if (previousMarkerOptions.hrefhash == hrefhash) {
                                currentMarkerOptions.markeroptions
                                        .icon(previousMarkerOptions.markeroptions
                                                .getIcon());
                                break;
                            }
                        }
                    }
                    if (currentMarkerOptions.markeroptions.getIcon() == null) {
                        try {
                            InputStream is = (InputStream) new URL(
                                    currentMarkerOptions.href).getContent();
                            Bitmap bm = (Bitmap) BitmapFactory.decodeStream(is);
                            currentMarkerOptions.markeroptions
                                    .icon((BitmapDescriptor) BitmapDescriptorFactory
                                            .fromBitmap(bm));
                        } catch (Exception e1) {
                            e1.printStackTrace();
                            ret = false;
                        }
                    }
                }
            }

            return ret;
        }


protected void onPostExecute(Boolean result) {
    addIcontoMap();         
}


protected void addIcontoMap() {
        for (KmlMarkerOptions kmlmo : mKmlmo) {
            mMap.addMarker(kmlmo.markeroptions);
        }
        }
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!