0

I have the following code :

ArrayList<OverlayItem> items = new ArrayList<OverlayItem>();
GeoPoint geoPoint = new GeoPoint(data.getPosition().longitude, data.getPosition().latitude, data.getPosition().altitude);
OverlayItem overlayItem = new OverlayItem(placeName, description, geoPoint);
overlayItem.setMarker(this.getResources().getDrawable(R.drawable.ic_launcher));
items.add(overlayItem);

overlay = new ItemizedOverlayWithFocus<OverlayItem>(this.getApplicationContext(), items,
        new ItemizedIconOverlay.OnItemGestureListener<OverlayItem>() {

            @Override
            public boolean onItemSingleTapUp(final int index, final OverlayItem item) {
                return true; // We 'handled' this event.
            }

            @Override
            public boolean onItemLongPress(final int index, final OverlayItem item) {
                return false;
            }
        });

mapView.getOverlays().set(0,overlay);
mapView.invalidate();

And I would like to create my own marker with canvas, for now, I'm using a static image, but I would like to create my own forms with circles, lines, ... I think it's possible but I can't find how to make it work.

Any help will be appreciated

Felix Kling
  • 795,719
  • 175
  • 1,089
  • 1,143
Bibu
  • 1,249
  • 2
  • 17
  • 40
  • I'm fighting with exactly the same problem (i.e. drawing custom car drawables as an OSMDroid marker). The `setMarker()` method isn't working and I'm currently implementing your solution. Does it work efficiently for many cars? Have you come up with some better solution since you've asked this question in 2012? – syntagma Dec 13 '13 at 13:17
  • It was working with 20 markers, but it was a long time ago like you noticed. But as far as I remember, it was quite smooth. – Bibu Dec 13 '13 at 18:02
  • 1
    Thanks. Did you handle Bitmap resizing and various screen sizes in any particular way? – syntagma Dec 13 '13 at 18:39
  • Map was zoom ajustable but markers stayed at the same size but were able to rotate depending on the direction they had. Concerning screen size, I only develop this application to 7" tablet. – Bibu Dec 13 '13 at 19:09

2 Answers2

1

I found a solution that solve a part of my problem. I create a class which extends from SimpleLocationOverlay, then I override the draw() methode and do my canvas.drawLine,drawCircle ... But now that I use a SimpleLocationOverlay type, I'm not able any more to use ItemizedOverlayWithFocus, so my question is, how to replace it with what I use now ?

For those who are interesting in my solution, and those who want to help me, the code looks like that :

public class MyOverlay extends SimpleLocationOverlay{
private MyCar myCar = new MyCar();
private GeoPoint geoPosition = new GeoPoint(0, 0);
private Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);

public myOverlay(Context ctx) {
    super(ctx);
    // TODO Auto-generated constructor stub
}

public myOverlay(Context ctx, MyCar _myCar) {
    super(ctx);
    this.myCar= _myCar;
    this.paint.setAntiAlias(true);
    ...
}

@Override
public void draw(Canvas c, MapView osmv, boolean shadow) {
    Point mapCenterPoint = new Point();
    osmv.getProjection().toPixels(this.geoPosition, mapCenterPoint);
    c.drawCircle(mapCenterPoint.x, mapCenterPoint.y, 10, this.paint);
}
}

And how I use it in my Activity :

    ArrayList<MyOverlay> items = new ArrayList<MyOverlay>();
    int i;

    GeoPoint geoPoint = null;
    MyOverlay myOverlay;

    mapView.getOverlays().clear();

    for (i = 0; i < listeCar.size(); i++) {
        geoPoint = new GeoPoint(listeCar.get(i).getPosition().longitude, listeCar.get(i).getPosition().latitude, listeCar.get(i).getPosition().altitude);
        myOverlay= new myOverlay(getApplicationContext(),listeCar.get(i), mapView);
        myOverlay.setLocation(geoPoint);
        myOverlay.draw(new Canvas(), mapView, false);
        items.add(myOverlay);
    }
    mapView.getOverlays().addAll(0, items );
    items.clear();
Bibu
  • 1,249
  • 2
  • 17
  • 40
  • That approach won't work with the OnItemGestureListener unless you include super.draw(c, osmv, shadow); in your overridden drwa() method. The inherited ItemizedOverlay class needs to call onDrawItem() which updates the HotspotPlace that the touch gestures work with. – dubmojo Dec 11 '12 at 00:59
0

My answer is a little late, but I've been struggling with this an a bunch of other issues using osmdroid. If you want to use your own Markers, do so within a Bitmap first, and set it as a Drawable Marker in the OverlayItem.setMarker() method.

If you override the ItemizedIconOverlay.draw() method for drawing your own Markers, you have to update the ItemizedIconOverlay's Hotspot logic as well for any touch screen gestures that use OnItemGestureListener.

To create a Bitmap using Canvas, try

Bitmap bitmap = Bitmap.createBitmap(width, height, Config.RGB_565);
Canvas canvas = new Canvas(bitmap);
// Draw on your canvas...

Then try assigning the Bitmap:

overlayItem.setMarker(new BitmapDrawable(bitmap));
dubmojo
  • 6,660
  • 8
  • 41
  • 68