Android谷歌地图异步任务添加标记 [英] Android google maps async task add markers

查看:129
本文介绍了Android谷歌地图异步任务添加标记的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一张地图。我有一个点的数据库。我想在AsyncTask中显示来自数据库的点,所以用户可以在地图上移动,当点加载时。没有太多的点(比如353等),但是当用户用更大的标记靠近(缩放)到地图时,我会重新绘制它们。当这种重绘发生时,应用程序冻结只需几分之一秒,但是它很明显,所以我认为AsyncTask将是答案。



然而,它不起作用。这很奇怪。
我在这里发现了几个问题,但问题大部分都没有调用publishProgress()方法,这不是我的情况。



方法doInBackground()我打开数据库,执行查询并将其保存到一个Cursor(这是我的ShowPoints类的属性,它扩展了AsyncTask,所以它应该可以通过此类的所有方法访问//至少我认为那)。
比我通过光标,每次调用publishProgress()哪个应该叫onProgressUpdate(),在这里我使用doInBackgroud()中的光标创建标记。



奇怪的是,如果我进行调试,并逐步完成代码,它就会一切正常。但是,当我运行应用程序时,没有添加标记或仅添加其中的几个标记。

  private类ShowPoints扩展了AsyncTask< String,整数,字符串> {

private String resp =Bodynačteny;
私人游标c_body;
私人LatLng marker_pozice;

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

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;
休息;
案例2:ikona = R.drawable.color_ec1c24_dot;
休息;
案例3:ikona = R.drawable.color_ffca05_dot;
休息;
案例4:ikona = R.drawable.color_6dcef5_dot;
休息;
案例5:ikona = R.drawable.color_f497b0_dot;
休息;
案例6:ikona = R.drawable.color_0088cf_dot;
休息;
案例7:ikona = R.drawable.color_98d3bf_dot;
休息;
案例8:ikona = R.drawable.color_c3792f_dot;
休息;
案例9:ikona = R.drawable.color_c5168c_dot;
休息;
}
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;
休息;
情况2:ikona = R.drawable.biggr_ec1c24_dot;
休息;
案例3:ikona = R.drawable.biggr_ffca05_dot;
休息;
案例4:ikona = R.drawable.biggr_6dcef5_dot;
休息;
案例5:ikona = R.drawable.biggr_f497b0_dot;
休息;
案例6:ikona = R.drawable.biggr_0088cf_dot;
休息;
案例7:ikona = R.drawable.biggr_98d3bf_dot;
休息;
案例8:ikona = R.drawable.biggr_c3792f_dot;
休息;
案例9:ikona = R.drawable.biggr_c5168c_dot;
休息;
}
Log.i(Thread,String.valueOf(i));
publishProgress(ikona);

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

$ b @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])));
}
//进度。例如更新ProgessDialog
}

我在onCreate方法中调用它:

  ShowPoints pointShower = new ShowPoints(); 
pointShower.execute(small);

另外,我在doInBackgroud()中做的日志记录可以工作,但标记不会添加到地图(或不是所有的人......有时候会绘制3个,有时候是19个等等)。

mMap是可接受的,因为这个类在另一个类中,该映射是一个属性,并在调用此Async之前实例化。同样在onPostExecute()中,我有一个烤面包,当显示的时候会显示出所有的点。

我启动应用程序,所以毫无疑问,它完成了异步(日志工作,最后的方法被称为)。

当你的这里的意图是好的,我只是没有看到很多收益,因为你仍然回调每个单一元素的UI线程,我敢打赌,你不会看到很大的性能差异。话虽如此,我敢打赌,你的问题在于你保留了游标并在 onProgressUpdate 中使用它,而你的 doInBackground 继续movine光标到下一行。这可以解释为什么当你调试它时正常工作,因为你正在一步步前进。



我会建议增加性能的是在你的 doInBackground 创建一个包含有关地图对象(图标,位置等)所有信息的对象列表。只将标记添加到地图中的位置,这些地图位于地图的可见边界框中,以便不绘制甚至不可见的标记。然后在你的 onPostExecute 中使用该列表并在那里绘制所有内容。



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

I have a map. I have a database of points. I want to display points from database in AsyncTask, so user can move around the map, when the points are being loaded. There's not much points (like 353 or etc), but I redraw them all when user gets closer (zoom) to the map with bigger markers. When this redraw occurs, app freezes like for a fraction of a second, but it's noticeable, so I thought AsyncTask would be the answer.

However it's not working. And it's weird. I found a few questions here, but the problem there was mostly not calling the "publishProgress()" method, which is not my case.

In the method "doInBackground()" I open the database, do the query and save it to a Cursor (which is attribute of my ShowPoints class, which extends AsyncTask, so it shoud be accesable by all methods of this class //at least I think that). Than I go through the cursor and each time call "publishProgress()" which shoud call "onProgressUpdate()" and here I create the marker using my cursor from "doInBackgroud()".

It's strange that if I do debug, and go through the code by step, it all works as it shoud. But when I run the app, no markers are added or only a few of them.

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
      }

I call this in the onCreate method:

ShowPoints pointShower = new ShowPoints();
pointShower.execute("small");

Also the logging I'm doing in "doInBackgroud()" works, but markers are not added to the map (or not all of them ... sometimes there are drawn 3, sometimes 19 etc).

mMap is accesable, because this class is inside another class, where the map is an attribute and is instantiated before this Async is called. Also as I'm saying, in debuger step by step I can draw all the points.

Also in "onPostExecute()" I have a toast, which is displayed when I launch the app, so there's no doubt it finishes the async (logging works, final method is called).

解决方案

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

这篇关于Android谷歌地图异步任务添加标记的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆