0

我想将标题和片段值以外的值传递给 OverlayItem,我通过一些搜索发现为 OverlayItem 创建子类可能是解决方案。但我不知道怎么写。

VenueMapActivity.class

 public void drawVenue(){
        ........
        Drawable marker = getResources().getDrawable(R.drawable.mall2);
        VenueMapOverlay venuePos = new VenueMapOverlay(marker,mapView);
        String getAdd;

        StringBuilder strReturnedAddress = new StringBuilder("Address: ");
        try {
            // Getting Array data from php

            venues = json.getJSONArray(TAG_VENUE);
            GeoPoint[] mallCoords = new GeoPoint[venues.length()];
            // looping through All data
            for(int i = 0; i < venues.length(); i++){
                ....
                mallCoords[i] = new GeoPoint((int)(lat * 1E6),(int)(lng *1E6));
                List<Overlay> overlays = mapView.getOverlays();
                    // The custom of overlayItem
                OverlayItem overlayItem = new CustomItem(mallCoords[i], name, strReturnedAddress.toString(), vid);
                venuePos.addOverlay(overlayItem);
                venuePos.setCurrentLocation(currentLocation);
                overlays.add(venuePos);      
            }

        }
        catch(JSONException e){
            e.printStackTrace();
        }
}
//To allow intent when balloon is clicked
public void startCustomActivity(Context context, String tmp){
        Intent Details = new Intent(context, InfoActivity.class);
        Details.putExtra("Id", tmp);
        context.startActivity(Details);
    }
class CustomItem extends OverlayItem {

        String venueid =null;
        CustomItem(GeoPoint pt, String name, String snippet,
                   String venueid) {
          super(pt, name, snippet);
          this.venueid = venueid ;

        }

VenueMapOverlay.class

public class VenueMapOverlay extends BalloonItemizedOverlay<OverlayItem> {

private Context mContext;
private ArrayList<OverlayItem> venues = new ArrayList<OverlayItem>();
private Location currentLocation;

public VenueMapOverlay(Drawable defaultMarker, MapView mapView) {
    super(boundCenter(defaultMarker), mapView);

    mContext = mapView.getContext();
}

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

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

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

public void setCurrentLocation(Location loc){
    this.currentLocation = loc;
}

public Location convertGpToLoc(GeoPoint gp){
    Location convertedLocation = new Location("");

    convertedLocation.setLatitude(gp.getLatitudeE6() / 1e6);
    convertedLocation.setLongitude(gp.getLongitudeE6() / 1e6);

    return convertedLocation;
}


@Override
protected boolean onBalloonTap(int index, OverlayItem item) {
    String tmp = venues.get(index).getTitle();      
    VenueMapActivity sub = new VenueMapActivity();
    sub.startCustomActivity(mContext, tmp);
    return true;
}
}

在 OnBalloonTap 方法中,我如何获得场地 ID 的价值?
请帮帮我。谢谢你。

4

1 回答 1

0

试试这个:

OverlayItem item = venues.get(index);
if(item instanceof CustomItem) {
  CustomItem cstItem = (CustomItem) item;
  String myId = cstItem.getVenueId();
}

您可以编写 CustomItem 方法,例如getVenueId()

class CustomItem extends OverlayItem {

    String venueid = null;
    CustomItem(GeoPoint pt, String name, String snippet,
               String venueid) {

      super(pt, name, snippet);
      this.venueid = venueid ;
    }

    String getVenueId()
    {
        return venueid;
    }
}
于 2012-04-07T15:56:59.717 回答