Optimize hundreds of ItemizedOverlay & lt; Overlayitem & gt; Using OSMdroid

综合技术 2017-12-31

I'm using OSMdroid
to display offline maps. I'm trying to overlay ItemizedOverlay
onto my mapview. And I'm able to achieve this. But the problem is I 've over a hundred(s) of items that need to be overlayed. I'm getting the data from network.

Since the number of items are too much the mapview can't handle too much data, and its slows the app down and is ANR pops most of the times. To avoid this I'm using points only related to visible screen to check it the point is in the visible screen, yes works to some extent. But the computational complexity is too much, as I'm using a single loop to compute which might take O(n2)
.

@Override
    public void draw(Canvas canvas,MapView mapview,boolean shadow) {
        try {

             for(int i=0;i<mItemList.size();i++) {
                    GeoPoint currGeo = mItemList.get(i).getPoint();
                    Point newPoint = new Point();
                    final Rect viewportRect = new Rect();
                    mapview.getProjection().toPixels(currGeo, newPoint);
                    viewportRect.set(mapview.getProjection().getScreenRect());
                    if(viewportRect.contains(newPoint.x, newPoint.y)){
                        super.draw(canvas, mapview, shadow);
                    }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

How can I optimize this snippet or, is there any other approaches where I can optimize, the pan scroll zoom of mapview?

You can "cache" the computationally expensive part of toPixels(). When you set the location on your Item
you should also call:

mProjectedPoint = mapview.getProjection().toProjectedPixels(latitude, longitude, null);

and store that value in your Item
for later use. When you want to get the pixel location of your Item
you can call

Point finalPoint = mapview.getProjection().toPixelsFromProjected(mProjectedPoint, null);

The toPixelsFromProjected
call is much faster than calling toPixels
every time.

However you also have another option - you can get the map's bounding box (which is in lat/long coordinates) and just check to see if your item's location is in the bounding box.

Note that all of these solutions however only determine if the item's center point is in the screen. The actual drawable portion of your item (for example if it is an icon) is larger than just a single point.

Hello, buddy!

责编内容by:Hello, buddy! (源链)。感谢您的支持!

您可能感兴趣的

为WebView增加悬浮按钮 背景 由于现在项目中需要对WebView进行封装,使其实现很多扩展功能,可以在任何地方new一个重写的WebView,实现相应的功能。WebView需要高度复用,因此想要增加悬浮按钮也只能在重写的WebView中添加相应功能。 同时还需要保证悬浮按钮和WebView的可见状态完全一致,在We...
Android Custom SeekBar Example SeekBar is the extension of ProgressBar in android. In this tutorial, I am going to show how to customize android SeekBar. There are lots of ways to ...
android学习路线:如何成长为高级工程师... 博主参加了2014 CSDN博客之星评选,帮我投一票吧。 点击给我投票 前言 之所以写这篇文章,是因为最近博客和我的开发群( 215680213 )中,不少小伙伴都让我讲讲...
Jave + Kotlin : Picking State Saver library Jave + Kotlin : Picking State Saver library Saving State was so cumbersome in Android. But there’s a lib...
Get a new coordinate position after rotation with ... I'm wondering how to use a Matrix to get the new position of a coordinate within a rectangle after rotation. What I would like to do is: Define...