0

我所做的是从 xml 文件中获取数据,然后使用自定义 ItemizedOverlay 添加项目。

public CustomItemizedOverlay callme(List<Places> result){

    Drawable drawable = this.getResources().getDrawable(R.drawable.ic_launcher);

    latitudeE6  = result.get(i).latitude;
    longitudeE6 = result.get(i).longitude;

    GeoPoint point = new GeoPoint(latitudeE6a, longitudeE6a);                               
    OverlayItem overlayitem = new OverlayItem(point, result.get(i).title, result.get(i).text);

    CustomItemizedOverlay itemizedOverlay = new CustomItemizedOverlay(drawable);

    itemizedOverlay.addOverlay(overlayitem);//add point

    i++;

    latitudeE6 =0;
    longitudeE6 =0;
    return itemizedOverlay;

}

当我在 UI 线程上时,我使用 while() 循环来打印:

int j=0;
List<Overlay> mapOverlays = mapView.getOverlays();

while( j < 5 ) {
    test1 = callme(result1);                                
    mapOverlays.add(test1);                                 

    j++;
} 

MapController mapController = mapView.getController();
mapController.setZoom(12);

只需要显示五个项目,当 j < 5 时循环停止。问题是它只显示 xml 数据中的最后一项。

4

2 回答 2

2

Step1:添加这个java文件..

public class MapItemizedOverlay extends ItemizedOverlay<OverlayItem>
{
public static boolean isRouteSelecting;
public static ArrayList<GeoPoint> geoPoints;

private GeoPoint geoPoint_;
private ArrayList<OverlayItem> overlayItems_ = new ArrayList<OverlayItem>();

public MapItemizedOverlay(Drawable defaultMarker)
{
    super(boundCenterBottom(defaultMarker));
    // TODO Auto-generated constructor stub
}

public void addOverlay(OverlayItem overlayItem)
{
    overlayItems_.add(overlayItem);
    populate();
}

@Override
protected OverlayItem createItem(int i)
{
    // TODO Auto-generated method stub
    return overlayItems_.get(i);
}

@Override
public int size()
{
    // TODO Auto-generated method stub
    return overlayItems_.size();
}
 }

第2步:进入您的MapActivity ...(oncreate)

mapItemizedOverlay_ = new MapItemizedOverlay(drawable_);

                     for(int i = 0; i < yourList.size(); i++)
            {

                GeoPoint geoPoint = new            GeoPoint(Latitude, Longitude);
                OverlayItem overlayItem = new OverlayItem(geoPoint, busStop.getStopName(), "");
                mapItemizedOverlay_.addOverlay(overlayItem);
            }
            drawable_ = this.getResources().getDrawable(R.drawable.ic_launcher);
            mapView_.getOverlays().add(mapItemizedOverlay_);
于 2012-05-08T08:43:45.983 回答
1

int i你在你的callme(List<Places> result)方法中错过了一个循环

更新:

List<Overlay> mapOverlays = mapView.getOverlays();

  for(int i=0;i<result1.size();i++)
   {
    Drawable drawable = this.getResources().getDrawable(R.drawable.ic_launcher);
    latitudeE6  = result1.get(i).latitude;
    longitudeE6 = result1.get(i).longitude;

    GeoPoint point = new GeoPoint(latitudeE6a,longitudeE6a);                                
    OverlayItem overlayitem = new OverlayItem(point, result1.get(i).title, result1.get(i).text);
    CustomItemizedOverlay itemizedOverlay = new CustomItemizedOverlay(drawable);           
    itemizedOverlay.addOverlay(overlayitem);        //add point

     mapOverlays.add(itemizedOverlay);  
    }           
    latitudeE6 =0;
    longitudeE6 =0;                     
MapController mapController = mapView.getController();
mapController.setZoom(12);

使用上面的代码删除你的代码你发布了什么问题,现在让我知道会发生什么..

于 2012-05-08T08:34:14.027 回答