5

I have a map in my android application that shows many markers (~20-50). But the app performs very poor when i try to scroll/zoom (in Google Android Maps i did a sample search for pizza and there were also some 20-50 results found and i didn't notice any particular performance problems when zooming/scrolling through the map).

Here is my (pseudo)code:

onCreate() {
    ....
    drawable = this.getResources().getDrawable(R.drawable.marker1);
    itemizedOverlay = new MyItemizedOverlay(drawable,mapView);
    ...
    callWebServiceToRetrieveData();

    createMarkers();
}

createMarkers(){
    for(elem:bigList){
        GeoPoint geoPoint = new GeoPoint((int)(elem.getLat()*1000000), (int) (elem.getLon()*1000000));
        OverlayItem overlayItem = new OverlayItem(geoPoint, elem.getName(), elem.getData());
        itemizedOverlay.addOverlay(overlayItem);

        mapOverlays.add(itemizedOverlay);
}
mapView.invalidate();

}

the MyItemizedOverlay.addOverlay looks like this:

public void addOverlay(OverlayItem overlay) {
    m_overlays.add(overlay);
    populate();
}
Dave
  • 3,983
  • 3
  • 17
  • 8

3 Answers3

8

If I understand how this works correctly, you should not be calling populate after every add an overlay. You should do it once you've added them all. I think what's happening is that you add the first OverlayItem and call populate() so it adds that to the ItemizedOverlay. Then you add a second OverlayItem to the list and call populate() and it adds those two overlays to the ItemizedOverlay resulting in three items in the overlay. So I think you're getting way more than the 20-50 that you think.

CaseyB
  • 24,780
  • 14
  • 77
  • 112
  • Not only that, but you can have more than one marker in an overlay. The way your pseudocode is written, you are adding the *same overlay* once per marker -- `mapOverlays.add(itemizedOverlay);` -- which is not exactly going to help you. – CommonsWare Apr 17 '10 at 20:37
  • Would be interesting to me, too. – Tobias Reich Dec 05 '12 at 16:21
6

I've done the following now and it works fast(er):

createMarkers(){
    for(elem:bigList){
        GeoPoint geoPoint = new GeoPoint((int)(elem.getLat()*1000000), (int) (elem.getLon()*1000000));
        OverlayItem overlayItem = new OverlayItem(geoPoint, elem.getName(), elem.getData());
        itemizedOverlay.addOverlay(overlayItem);

   }
   itemizedOverlay.populateNow();
   mapOverlays.add(itemizedOverlay); //outside of for loop

}

and in MyOverlay:

public void addOverlay(OverlayItem overlay) {
    m_overlays.add(overlay);
}

public void populateNow(){
    populate();
}

is this better/correct now ? or any other improvements possible?

Dave
  • 3,983
  • 3
  • 17
  • 8
0

I ran into the same problem last night, and my solution was the same as yours. I felt weird about how I called the populate method.

I had the same code as you but I called

speedyPopulate() { 
  populate(); 
}

in the MyOverlay class

Rahul Sharma
  • 2,867
  • 2
  • 27
  • 40