Java-Google在Android上的地图绘制速度非常慢还是我使用的方式错误?

因此,我将几个月前为iOS开发的应用移植回了Android.该应用程序具有可在地图上显示的数百(341)个点的数据库.在iOS上执行此操作时,将这些点添加到我的地图时没有遇到性能问题.如果用户缩小,则舒尔(Shure),以便所有可见的点立即出现,您可能会注意到速度有所下降,但没什么大不了的.另一方面,在Android上,这极其缓慢,直到冻结为止. AVD上的速度也比手机(HTC Hero)上的速度慢.

我想指出,我是Java的新手,但是我有C/C++ / OBJ-C的经验.
我在代码中摆弄了一些,并使用了一些简单的性能测试,并得出了一些关于我的代码的令人惊讶的结论.

这是我在MapActivity中用于向我的地图添加叠加层的函数,我在其中留下了一些注释掉的代码,我将在其中进行解释.

private void addOverlaysToMapFromManagedInterestPoints() {

       this.mapOverlays = myMap.getOverlays();

        GeoPoint point;
        OverlayItem overlayitem;



       for( managed_interest_point mip : managedObjectManager.interestPoints )
        {


            TypeOfPoint type = getEnumForTypeOfPoint(mip.ZTYPEOFPOINT);
            if(type!=null)
            {
                switch (type) {
                case kAnimalHospital:
                    point = new GeoPoint(degreesToMicrodegreesConversion(mip.ZLATITUDE),degreesToMicrodegreesConversion(mip.ZLONGITUDE));
                    overlayitem  = new OverlayItem(point, mip.ZTITLE, mip.ZSUBTITLE);                   
                    //this.animalHospitalOverlay.addOverlay(overlayitem);
                    AnnotationsOverlay testOverlay =  new AnnotationsOverlay(this.animalHospitalPin,this);
                    testOverlay.addOverlay(overlayitem);
                    Log.d("MainActivity","added animalHospital");
                    break;
                case kStore:
                    point = new GeoPoint(degreesToMicrodegreesConversion(mip.ZLATITUDE),degreesToMicrodegreesConversion(mip.ZLONGITUDE));
                    overlayitem = new OverlayItem(point, mip.ZTITLE, mip.ZSUBTITLE);                                     
                    //this.storesOverlay.addOverlay(overlayitem);   
                    AnnotationsOverlay testOverlay2 =  new AnnotationsOverlay(storePin,this);
                    testOverlay2.addOverlay(overlayitem);
                    this.mapOverlays.add(testOverlay2);
                    Log.d("MainActivity","added Store point");
                    break;
                case kVeterinary:                       
                    point = new GeoPoint(degreesToMicrodegreesConversion(mip.ZLATITUDE),degreesToMicrodegreesConversion(mip.ZLONGITUDE));
                     overlayitem = new OverlayItem(point, mip.ZTITLE, mip.ZSUBTITLE);                   
                     //this.storesOverlay.addOverlay(overlayitem);
                     AnnotationsOverlay testOverlay3 =  new AnnotationsOverlay(this.veterinaryPin,this);
                     testOverlay3.addOverlay(overlayitem);
                     this.mapOverlays.add(testOverlay3);
                     Log.d("MainActivity","added veterinary point");
                     break;
                default:
                    Log.d("MainActivity", "unknown enum");
                    break;
                }//end switch
            }//end if 
        }//end foreach

       //this.mapOverlays.add(this.storesOverlay);
       //this.mapOverlays.add(this.veterinariesOverlay);
       //this.mapOverlays.add(this.animalHospitalOverlay);  
       Log.d("MainActivity","end of foreach in add overlays to map ");


}

这是AnnotationsOverlay我的ItemizedOverlay子类的代码.

public class AnnotationsOverlay extends ItemizedOverlay {

private ArrayList<OverlayItem> mOverlays = new ArrayList<OverlayItem>();
Context mContext;



public AnnotationsOverlay(Drawable defaultMarker) {
    super(boundCenterBottom(defaultMarker));

}

public AnnotationsOverlay(Drawable defaultMarker,Context context)
{
    super(boundCenterBottom(defaultMarker));
    mContext=context;

}

@Override
protected OverlayItem createItem(int i) {


    return mOverlays.get(i);
}

@Override
public int size() {

    return mOverlays.size();
}

//show calloutAssesory view tapped för oss iPhöne människor
@Override
protected boolean onTap(int index) {
  OverlayItem item = mOverlays.get(index);
  AlertDialog.Builder dialog = new AlertDialog.Builder(mContext);
  dialog.setTitle(item.getTitle());
  dialog.setMessage(item.getSnippet());
  dialog.show();
  return true;
}

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

}

我的第一个本能是在循环内分配AnnotationsOverlay是一件坏事,因为似乎可以作为某种集合的集合,您应该向其中添加多个项目.

由于每次随后对add()的调用都花费了越来越长的时间,因此该假设被证明是错误的.我将此分配放入循环中,现在此函数在可接受的时间内执行.所有的点都显示在地图上,但是速度非常慢(可能会降低2 fps).我认为每次添加调用也调用populate()很奇怪.功能.我试着删除它并编写一个公共方法,该方法包装仅在添加所有项目后才调用的填充对象,但这并不觉得严谨.编写公共方法来包装私有方法通常不是一个好主意.
同样,它给了我奇怪的行为,如果我这样做,所有的叠加层都会得到相同的图像.

那么,我在做什么错呢?还是Android上的Google Maps API的灵活性比iOS上的灵活性差很多?

解决方法:

我在这里取得了一些进展,这可能对阅读此问题的某些人有用.
1.现在,我已将我的填充方法从添加方法中移出,并将其包装在公共方法中,这样,在填充整个OverlayItems数组之后,我只能调用一次.
我也尝试在没有调试器的情况下运行该应用程序,即仅在手机上运行该应用程序,然后它可以顺利运行.这不是我唯一一次注意到调试器将事情放慢10-20倍甚至更多的时间.过去,我曾遇到过类似的问题,其中在调试模式下运行会使某些操作减慢了极长时间.

新代码如下所示:

    private void addOverlaysToMapFromManagedInterestPoints() {

       this.mapOverlays = myMap.getOverlays();

        GeoPoint point;
        OverlayItem overlayitem;



       for( managed_interest_point mip : managedObjectManager.interestPoints )
        {

            if(mip==null)
            {
                Log.d("MainActivity","this should not happen!");
            }

            TypeOfPoint type = getEnumForTypeOfPoint(mip.ZTYPEOFPOINT);
            if(type!=null)
            {
                switch (type) {
                case kAnimalHospital:
                    point = new GeoPoint(degreesToMicrodegreesConversion(mip.ZLATITUDE),degreesToMicrodegreesConversion(mip.ZLONGITUDE));
                    overlayitem  = new OverlayItem(point, mip.ZTITLE, mip.ZSUBTITLE);                   
                    this.animalHospitalOverlay.addOverlay(overlayitem);
                    Log.d("MainActivity","added animalHospital");
                    break;
                case kStore:
                    point = new GeoPoint(degreesToMicrodegreesConversion(mip.ZLATITUDE),degreesToMicrodegreesConversion(mip.ZLONGITUDE));
                    overlayitem = new OverlayItem(point, mip.ZTITLE, mip.ZSUBTITLE);                                     
                    this.storesOverlay.addOverlay(overlayitem); 
                    Log.d("MainActivity","added Store point");
                    break;
                case kVeterinary:                       
                    point = new GeoPoint(degreesToMicrodegreesConversion(mip.ZLATITUDE),degreesToMicrodegreesConversion(mip.ZLONGITUDE));
                     overlayitem = new OverlayItem(point, mip.ZTITLE, mip.ZSUBTITLE);                   
                     this.veterinariesOverlay.addOverlay(overlayitem);
                     Log.d("MainActivity","added veterinary point");
                     break;
                default:
                    Log.d("MainActivity", "unknown enum");
                    break;
                }//end switch
            }//end if 
        }//end foreach

      this.storesOverlay.callToPopulate();
      this.veterinariesOverlay.callToPopulate();
      this.animalHospitalOverlay.callToPopulate();


       this.mapOverlays.add(this.storesOverlay);
       this.mapOverlays.add(this.veterinariesOverlay);
       this.mapOverlays.add(this.animalHospitalOverlay);    
       Log.d("MainActivity","end of foreach in add overlays to map ");


}



public class AnnotationsOverlay extends ItemizedOverlay {

private ArrayList<OverlayItem> mOverlays = new ArrayList<OverlayItem>();
Context mContext;



public AnnotationsOverlay(Drawable defaultMarker) {
    super(boundCenterBottom(defaultMarker));

}

public AnnotationsOverlay(Drawable defaultMarker,Context context)
{
    super(boundCenterBottom(defaultMarker));
    mContext=context;

}

@Override
protected OverlayItem createItem(int i) {


    return mOverlays.get(i);
}

@Override
public int size() {

    return mOverlays.size();
}

//show calloutAssesory view tapped för oss iPhöne människor
@Override
protected boolean onTap(int index) {
  OverlayItem item = mOverlays.get(index);
  AlertDialog.Builder dialog = new AlertDialog.Builder(mContext);
  dialog.setTitle(item.getTitle());
  dialog.setMessage(item.getSnippet());
  dialog.show();
  return true;
}

public void addOverlay(OverlayItem overlay)
{
    mOverlays.add(overlay);
    //populate();
}

public void callToPopulate()
{
    populate();
}



}
上一篇:Python:有关改进逐块代码以读取数百万点的建议


下一篇:显示每个请求统计信息和程序流的Java Profiler