【问题标题】:BalloonItemizedOverlay items not getting displayedBalloonItemizedOverlay 项目未显示
【发布时间】:2013-02-07 07:45:08
【问题描述】:

这是我的自定义叠加层

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 上。

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

【问题讨论】:

    标签: android android-mapview itemizedoverlay


    【解决方案1】:

    如果你使用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();   
    }
    

    【讨论】:

      【解决方案2】:

      错误就在这里:

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

      【讨论】:

        猜你喜欢
        • 2012-12-08
        • 2021-12-20
        • 2010-11-24
        • 1970-01-01
        • 1970-01-01
        • 2012-05-29
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多