简体   繁体   中英

Can I extend the Overlay class?

I have created a class that extends the Overlay class in Osmdroid (CustomOverlay.java). However, nothing appear on the map after I call the method createMark() on the main activity which make use of CustomOverlay.java. I do not want to use ItemizedOverlay class.

Any idea?

CustomOverlay.java (class that extends Overlay)

    public CustomOverlay(Context context) {
    super(context);

}

@Override
protected void draw(Canvas canvas, MapView mapView, boolean shadow) {

    Point screenPoint = new Point();   
    mapView.getProjection().toPixels(geoPoint, screenPoint);

    super.drawAt(canvas, mapView.getResources().getDrawable(R.drawable.marker), screenPoint.x, screenPoint.y, shadow);

}
}

This is the method I call in the main activity:

  private void createMarker() {
    List<Overlay> mapOverlays = mMapView.getOverlays();
    Overlay c = new CustomOverlay(this);
    mapOverlays.add(c);
    mMapView.invalidate();

}

Answer:

    @Override
protected void draw(Canvas canvas, MapView mapView, boolean shadow) {

    Point screenPoint = new Point();   
    mapView.getProjection().toPixels(geoPoint, screenPoint);

    Bitmap marker= BitmapFactory.decodeResource(mapView.getResources(), R.drawable.marker);            
    canvas.drawBitmap(marker, screenPoint.x, screenPoint.y, null); 

}

Here's the skeleton of a customized overlay:

public class MyItemizedOverlay extends ItemizedOverlay<OverlayItem> {

  private ArrayList<OverlayItem> mOverlays = new ArrayList<OverlayItem>();

  public MyItemizedOverlay(Drawable defaultMarker) {
    super(boundCenterBottom(defaultMarker));
  }

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

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

  public void removeAllOverlay() {
    mOverlays.clear();
  }

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

See the differences? Where is your populate() call? Hope this helps.

Refer this link for your answer. :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM