androidosmdroidoverlayitem

How to create its own Marker for OverlayItem in OsmDroid


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


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