添加覆盖项目以OSMDroid地图中的AsyncTask [英] Adding Overlay Items to OSMDroid-Map in AsyncTask

查看:345
本文介绍了添加覆盖项目以OSMDroid地图中的AsyncTask的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要什么:

我要加载地图后在user's当前位置10个标记和地图,从数据库中的AsyncTask的加载。

什么doenst工作

一切工作正常,除10个额外的标记。我装他们从数据库中,他们加入到OverlayItems的列表,无效地图 - 他们仍然地图无法显示结果
更新:现在的工作。通过后的创建ItemizedIconOverlay的在加载的AsyncTask的附加项目的项目显示这样工作,inteded

此外,OverlayItems的列表被初始化为零对象(明显),但我添加一个OverlayItem后,它包含了1 OverlayItem + 11多个空对象(或空Refernces?空项目?what's正确长期?)

我的提问

1)OverlayItems的@列表:装载的一切之后,还有一个空的对象,我认为这可能会导致问题,但它没有。 (测试用的完整列表,没有区别)结果
为什么列表初始化为零的项目,但是当我增加一个,它包含12项? (11空,1真正的项目)(code以下)

2)的 [解决] 据我知道,如果我添加OverlayItems到列表中,无效的地图,他们应该显示。如果我添加项目到第二个列表,覆盖第一个与第二个,它仍然可以工作? (由于未汇入作业相同的对象了,但只有列表2参照,对吧?)结果
更新:作为inteded 按后
的创建ItemizedIconOverlay的加载附加项在的AsyncTask的项,其显示和工作

展开code 结果
   创建地图

  mResourceProxy =新DefaultResourceProxyImpl(getApplicationContext());
    mapView.setTileSource(TileSourceFactory.MAPNIK);
    mapView.setBuiltInZoomControls(真);
    mapView.setMultiTouchControls(真);
    mapController = this.mapView.getController();
    mapController.setZoom(25);
    GeoPoint的中心=新的GeoPoint(DataManager.glat,DataManager.glon);
    mapController.setCenter(中心);    //项目= NULL
    项目=新的ArrayList< OverlayItem>(); //项目仍然是空的
    items.add(新OverlayItem(在这里,SampleDescription,中心));
    //现在,项目包含一个OverlayItem,而且11空(NULL)项目    this.mLocationOverlay =新ItemizedIconOverlay< OverlayItem>(物品,
    新ItemizedIconOverlay.OnItemGestureListener< OverlayItem>(){
                @覆盖
                公共布尔onItemSingleTapUp(最终诠释指数,
                        最终OverlayItem项){
                    意向意图=新的Intent();
                    intent.putExtra(newShopName,item.mTitle);
                    intent.putExtra(newShopAdd,item.mDescription);
                    的setResult(RESULT_OK,意向);
                    完();                    返回true;
                }
                @覆盖
                公共布尔onItemLong preSS(最终诠释指数,
                        最终OverlayItem项){                    吐司= Toast.makeText(ShopChooseActivity.this,item.mTitle +,+ item.mDescription,Toast.LENGTH_LONG);
                        toast.show();
                        返回false;
                    }
                },mResourceProxy);        。this.mapView.getOverlays()加(this.mLocationOverlay);
        mapView.invalidate();
        loadMap =新LoadChooseShop(ShopChooseActivity.this,项目).execute();

与OverlayItems从AsynTask新列表更新项目

 项目= loadMap.get();
如果(项目!= NULL)
mapView.invalidate();


解决方案

  1. 对于在ItemizedIconOverlay不显示新项目 - 您使用添加的项目()有什么方法?出于某种原因,它看起来像的addItem(位置,项目)方法不调用填充(),所以内部列表永远不会被更新。使用AddItem(项目)方法现在。我要指出,ItemizedIconOverlay是用户提交的类,并且是真的只是一个解决方案的起点。


  2. 您提到在您的项目清单有空。这是Java的ArrayList的工作方式。该名单是由一个不变数组支持所以当支持数组已满,要添加一个新的项目,列表必须创建一个新的更大的支持数组。由于这是一个昂贵的操作,新阵列将旧的两倍。支持数组中的未使用插槽都设置为null。唯一重要的事情是什么是mItemList.size()返回。


What I want:

I want a map with a marker at the user´s current location and 10 more, loaded in an AsyncTask from a Database after loading the map.

What doenst work

Everything is working fine, except the 10 additional markers. I loaded them from the DB, added them to the List of OverlayItems, and invalidated the map - they still aren´t shown.
Update: works now: By creating the ItemizedIconOverlay after loading the additional Items in an AsyncTask the Items are shown and work as inteded.

Additionally, the List of OverlayItems is initialized with zero objects (obviously), but after I add one OverlayItem, it contains 1 OverlayItem + 11 more null-Objects (or null-Refernces? null-Items? what´s the correct term?)

My Question

1) @ List of OverlayItems: After loading everything, there is still one null-Object, I thought this might cause problems, but it didnt. (Tested with a full List, no difference)
Why is the list initialized with zero items, but when I add one, it contains 12 Items? (11 null, 1 "real" item) (Code below)

2) [SOLVED] As far as I know, if I add OverlayItems to the List and invalidate the map, they should be shown. If I add the Items to a second List, and "overwrite" the first one with the second one, does it still work? (As it´s not the same object anymore, but only a reference to list2, right?)
Update: By creating the ItemizedIconOverlay after loading the additional Items in an AsyncTask the Items are shown and work as inteded.

Full Code
Creating the Map

 mResourceProxy = new DefaultResourceProxyImpl(getApplicationContext());
    mapView.setTileSource(TileSourceFactory.MAPNIK);
    mapView.setBuiltInZoomControls(true);
    mapView.setMultiTouchControls(true);
    mapController = this.mapView.getController();
    mapController.setZoom(25);
    GeoPoint center = new GeoPoint(DataManager.glat, DataManager.glon);
    mapController.setCenter(center);

    // items = null
    items = new ArrayList<OverlayItem>(); // items is still empty
    items.add(new OverlayItem("Here", "SampleDescription", center));
    // now, items contains one OverlayItem, but also 11 empty (null) Items

    this.mLocationOverlay = new ItemizedIconOverlay<OverlayItem>(items,
    new ItemizedIconOverlay.OnItemGestureListener<OverlayItem>() {
                @Override
                public boolean onItemSingleTapUp(final int index,
                        final OverlayItem item) {


                    Intent intent=new Intent();
                    intent.putExtra("newShopName", item.mTitle);
                    intent.putExtra("newShopAdd", item.mDescription);
                    setResult(RESULT_OK, intent);
                    finish(); 

                    return true; 
                }
                @Override
                public boolean onItemLongPress(final int index,
                        final OverlayItem item) {

                    toast = Toast.makeText(ShopChooseActivity.this, item.mTitle + ", " + item.mDescription, Toast.LENGTH_LONG);
                        toast.show();
                        return false;
                    }
                }, mResourceProxy);

        this.mapView.getOverlays().add(this.mLocationOverlay);
        mapView.invalidate();


        loadMap = new LoadChooseShop(ShopChooseActivity.this, items).execute();

Updating the Items with the new List of OverlayItems from AsynTask

items = loadMap.get();
if(items != null)
mapView.invalidate();

解决方案

  1. Regarding the new items not showing in the ItemizedIconOverlay - what method are you using to add the items()? For some reason it looks like the addItem(location, item) method does not call populate() so the internal list never gets updated. Use the addItem(item) method for now. I should point out that ItemizedIconOverlay is a user-submitted class and is really just a starting point for a solution.

  2. You mention having nulls in your item list. That is the way Java's ArrayList works. The list is backed by an immutable array so when the backing array is full and you want to add a new item, the list has to create a new larger backing array. Since that is an expensive operation, the new array will be double the size of the old one. The unused "slots" in the backing array are set to null. The only thing that matters is what is returned by mItemList.size().

这篇关于添加覆盖项目以OSMDroid地图中的AsyncTask的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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