Question

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

Was it helpful?

Solution

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

OTHER TIPS

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));
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top