Android谷歌地图异步任务添加标记

Android谷歌地图异步任务添加标记,android,google-maps,android-asynctask,google-maps-markers,Android,Google Maps,Android Asynctask,Google Maps Markers,我有一张地图。我有一个点数数据库。我想在AsyncTask中显示数据库中的点,这样用户可以在加载点时在地图上移动。没有太多的点(如353或其他),但当用户使用更大的标记靠近(缩放)地图时,我会重新绘制它们。当这个重画发生时,应用程序会冻结一小部分秒,但这是显而易见的,所以我认为AsyncTask就是答案 但是它不起作用。这很奇怪。 我在这里发现了一些问题,但是那里的问题主要不是调用“publishProgress()”方法,这不是我的情况 在方法“doInBackground()”中,我打开数据

我有一张地图。我有一个点数数据库。我想在AsyncTask中显示数据库中的点,这样用户可以在加载点时在地图上移动。没有太多的点(如353或其他),但当用户使用更大的标记靠近(缩放)地图时,我会重新绘制它们。当这个重画发生时,应用程序会冻结一小部分秒,但这是显而易见的,所以我认为AsyncTask就是答案

但是它不起作用。这很奇怪。 我在这里发现了一些问题,但是那里的问题主要不是调用“publishProgress()”方法,这不是我的情况

在方法“doInBackground()”中,我打开数据库,执行查询并将其保存到游标(这是我的ShowPoints类的属性,它扩展了AsyncTask,因此该类的所有方法都应该可以访问它//至少我认为是这样)。 然后我遍历游标,每次调用“publishProgress()”,应该调用“onProgressUpdate()”,在这里,我使用“doinBackground()”中的游标创建标记

奇怪的是,如果我进行调试,并一步一步地检查代码,那么一切都会正常工作。但是,当我运行应用程序时,没有添加任何标记,或者只有少数标记

private class ShowPoints extends AsyncTask<String, Integer, String> {

      private String resp="Body načteny";
      private Cursor c_body;
      private LatLng marker_pozice;

      @Override
      protected String doInBackground(String... params) {
          int i=0;
       try {
           DatabaseHelper db = new DatabaseHelper(MainActivity.this);
            try {

                db.openDataBase();

            }catch(SQLException sqle){

                throw sqle;

            }
            c_body = db.query(DatabaseHelper.TABLE_POINTS, allColumns, null, null, null,null, null);
            if(c_body.moveToFirst())
            {
                if(params[0]=="small"){
                    do {
                            i++;
                        int ikona = R.drawable.gray_dot;
                        int trasa = c_body.getInt(6);
                        switch(trasa){
                        case 1: ikona = R.drawable.color_00a750_dot;
                        break;
                        case 2: ikona = R.drawable.color_ec1c24_dot;
                        break;
                        case 3: ikona = R.drawable.color_ffca05_dot;
                        break;
                        case 4: ikona = R.drawable.color_6dcef5_dot;
                        break;
                        case 5: ikona = R.drawable.color_f497b0_dot;
                        break;
                        case 6: ikona = R.drawable.color_0088cf_dot;
                        break;
                        case 7: ikona = R.drawable.color_98d3bf_dot;
                        break;
                        case 8: ikona = R.drawable.color_c3792f_dot;
                        break;
                        case 9: ikona = R.drawable.color_c5168c_dot;
                        break;
                        }
                    publishProgress(ikona);
                    Log.i("Thread",String.valueOf(i));


                         } while (c_body.moveToNext());
                }else{
                         do {
                        i++;
                        int ikona = R.drawable.bigg_dot;
                        int trasa = c_body.getInt(6);
                        switch(trasa){
                        case 1: ikona = R.drawable.biggr_00a750_dot;
                        break;
                        case 2: ikona = R.drawable.biggr_ec1c24_dot;
                        break;
                        case 3: ikona = R.drawable.biggr_ffca05_dot;
                        break;
                        case 4: ikona = R.drawable.biggr_6dcef5_dot;
                        break;
                        case 5: ikona = R.drawable.biggr_f497b0_dot;
                        break;
                        case 6: ikona = R.drawable.biggr_0088cf_dot;
                        break;
                        case 7: ikona = R.drawable.biggr_98d3bf_dot;
                        break;
                        case 8: ikona = R.drawable.biggr_c3792f_dot;
                        break;
                        case 9: ikona = R.drawable.biggr_c5168c_dot;
                        break;
                        }
                        Log.i("Thread",String.valueOf(i));
                    publishProgress(ikona);

                         } while (c_body.moveToNext());
                }
            }
            db.close();
       } catch (Exception e) {
        e.printStackTrace();
        resp = e.getMessage();
       }
       return resp;
      }

@Override
      protected void onProgressUpdate(Integer... ikona) {
            marker_pozice = new LatLng(c_body.getDouble(3), c_body.getDouble(4));                 
            mMap.addMarker(new MarkerOptions()
              .position(marker_pozice)
              .title(c_body.getString(2))
              .snippet(c_body.getString(7)+"\n"+c_body.getString(1))
              .icon(BitmapDescriptorFactory.fromResource(ikona[0])));
            }
       // progress. For example updating ProgessDialog
      }
我在“doinBackground()”中所做的日志记录也起作用,但是标记并没有添加到地图中(或者不是所有的标记……有时画3,有时画19等等)

mMap是可访问的,因为该类位于另一个类中,其中映射是一个属性,并在调用该异步之前实例化。正如我所说,在Debugger中,我可以一步一步地画出所有的点


在“onPostExecute()”中,我还有一个祝酒词,在我启动应用程序时会显示,因此毫无疑问,它完成了异步(日志记录工作,调用了最终方法)。

虽然您的意图是好的,我只是看不到它有什么好处,因为你仍然在为每一个元素调用UI线程,我打赌你不会看到性能上有太大的差异。话虽如此,我敢打赌,您的问题在于,当您的
doInBackground
继续将光标移动到下一行时,您正在按住光标并在
onProgressUpdate
中使用它。这就解释了为什么当你调试它时,它能正常工作,因为你是一步一步来的

为了提高性能,我建议您在
doInBackground
中创建一个包含地图对象(图标、位置等)所有信息的对象列表。仅将标记添加到地图中它们位于地图可见边界框中的位置,这样您就不会打印甚至不可见的标记。然后在您的
onPostExecute
中使用该列表并在那里绘制所有内容


虽然我不确定这是否是问题的原因,但很可能就是问题所在

为此,我在asynctask中创建了一个标记选项列表,然后在onPostExecute中以循环方式将它们添加到映射中。实际上,我有两个同步运行的任务。Task1设置标记选项列表,当它完成时,task2启动并获取/下载与标记关联的任何图标。我会在最后进行下载,因为很可能您会重用标记,并且可以使用散列来获取以前下载的标记。您可能只需要使用第二个异步任务来获取标记图标,因此我已经包含了用于获取标记位图的类

这里有一个自定义类来处理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;
}
下面是加载标记位图的第二个异步任务

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);
        }
        }
私有类LoadMarkerBitmapDescriptor扩展
异步任务{
@凌驾
受保护的布尔doInBackground(列表…参数){
布尔ret=真;
列表kmlmarkeroptions=params[0];
int markerSize=kmlmarkeroptions.size();
对于(int currentMarkerPosition=0;currentMarkerPositionprivate 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);
        }
        }