androidandroid-mapviewitemizedoverlay

BalloonItemizedOverlay items not getting displayed


Here is my CustomOverlay

class CustomOverlay extends BalloonItemizedOverlay<OverlayItem>
{
    private ArrayList<OverlayItem> mOverlays = new ArrayList<OverlayItem>();
    Context mContext;

    public CustomOverlay(Drawable defaultMarker, MapView mapView)
    {
        super(defaultMarker, mapView);
        mContext = mapView.getContext();
    }

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

    @Override
    protected OverlayItem createItem(int i)
    {
        return mOverlays.get(i);
    }

    @Override
    public int size()
    {
        return mOverlays.size();
    }

    @Override
    protected boolean onBalloonTap(int index)
    {
        Toast.makeText(mContext, "onBalloonTap for overlay index " + index,
                Toast.LENGTH_LONG).show();
        return true;
    }
}

Here is my main activity:

public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.map_outlets);

    mapView = (MapView) findViewById(R.id.mapView);
    mapView.setBuiltInZoomControls(true);
    mapView.setStreetView(true);

    lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
    lm.requestLocationUpdates(LocationManager.PASSIVE_PROVIDER, 1000L,
            500.0f, this);

    Location l = new Location(LocationManager.NETWORK_PROVIDER);
    l = lm.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);

    mc = mapView.getController();
    p = new GeoPoint((int) (l.getLatitude() * 1E6),
            (int) (l.getLongitude() * 1E6));
    mc.setCenter(p);
    mc.setZoom(18);
    mc.animateTo(p);

    Drawable redmarker = this.getResources().getDrawable(R.drawable.pinr);
    Drawable greenmarker = this.getResources().getDrawable(R.drawable.pin);

    mapOverlays = mapView.getOverlays();

    // Overlay 1
    overlay1 = new CustomOverlay(greenmarker, mapView);

    OverlayItem oi1 = new OverlayItem(new GeoPoint((int) (33.733492*1E6), (int) (73.088302*1E6)), "Evacuee Trust Cafe", "It sucks");
    overlay1.addOverlay(oi1);

    OverlayItem oi2 = new OverlayItem(new GeoPoint((int) (33.734547*1E6), (int) (73.08599*1E6)), "Balochistan Mosque", "Where muslim men convene");
    overlay1.addOverlay(oi2);

    // Overlay 2
    overlay2 = new CustomOverlay(redmarker, mapView);
    OverlayItem oi3 = new OverlayItem(new GeoPoint((int) (33.73287*1E6), (int) (73.086205*1E6)), "Agha Khan Road", "Its a Road ofc.");
    overlay2.addOverlay(oi3);

    mapOverlays.add(overlay1);
    mapOverlays.add(overlay2);

    mapView.invalidate();
}

The overlay items are not getting displayed on the mapView.

P.S. I'm using SherlockMapActivity. AND I changed the android-mapviewballoons manifest file since I didn't have the old apis. I changed it to minimum 8 and target 16.


Solution

  • The mistake is here:

    public CustomOverlay(Drawable defaultMarker, MapView mapView)
    {
        super(defaultMarker, mapView);
        mContext = mapView.getContext();
    }
    

    It needs to be:

    public CustomOverlay(Drawable defaultMarker, MapView mapView)
        {
            super(boundCenter(defaultMarker), mapView);
            mContext = mapView.getContext();
        }