Question

i will like to pass other than title and snippet value to the OverlayItem, i go through some search i found out that creating subclass for OverlayItem could be the solution. But i have no idea how to write it.

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;
}
}

At OnBalloonTap method, how do i get value of venueid??
Please help me out. Thankyou.

Was it helpful?

Solution

Try this:

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

You can write CustomItem method, for example 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;
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top