2

这是我的 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 上。

PS 我正在使用 SherlockMapActivity。而且我更改了 android-mapviewballoons 清单文件,因为我没有旧的 api。我将其更改为最低 8 和目标 16。

4

2 回答 2

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();   
}
于 2013-02-07T07:56:19.837 回答
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();
    }
于 2013-02-07T08:36:07.450 回答