BalloonItemizedOverlay项目未显示

时间:2013-02-07 07:45:09

标签: android android-mapview itemizedoverlay

这是我的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;
    }
}

这是我的主要活动:

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

叠加项目未显示在mapView上。

P.S。我正在使用SherlockMapActivity。我改变了android-mapviewballolo清单文件,因为我没有旧api。我把它改为最低8和目标16。

2 个答案:

答案 0 :(得分:0)

如果您使用https://github.com/jgilfelt/android-mapviewballoons/,则应覆盖方法(在CustomOverlay中):

protected BalloonOverlayView<CustomOverlayItem> createBalloonOverlayView() {
    // use our custom balloon view with our custom overlay item type:
    return new CustomBalloonOverlayView<OverlayItem>(getMapView().getContext(), getBalloonBottomOffset();   
}

答案 1 :(得分:0)

错误在于:

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

需要:

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