Question

I am trying to implement my own map overlay for osmdroid (but I assume it is fairly similar to Google map overlays).

What I am trying to do is draw a plane, rotate it according to bearing and draw a speed vector (line ahead of the plane in the flight direction that shows where it will soon be).

The idea is that I draw the plane (et all) on a canvas "facing North", then rotate it according to flight direction and "merge" it with the overlay canvas (I tried drawing directly to the overlay canvas, but on rotate, it was rotating the map as well).

I have created a subclass of Overlay and overiden the onDraw method as follows:

@Override
protected void draw(Canvas c, MapView mapView, boolean shadow) {
    if (location != null) {
        Point locPoint = new Point();
        GeoPoint locGeoPoint = new GeoPoint(location);
        final Projection pj = mapView.getProjection();
        pj.toMapPixels(locGeoPoint, locPoint);

        this.drawPlane(c, locPoint, location.getBearing());
    }
}

private void drawPlane(Canvas cs, Point ctr, float bearing) {

    Paint paint = new Paint();
    paint.setColor(Color.RED);
    paint.setAntiAlias(true);

    Bitmap planeBM = Bitmap.createBitmap(cs.getWidth(), cs.getHeight(), Bitmap.Config.ARGB_8888);
    planeBM.setDensity(cs.getDensity());        
    Canvas c = new Canvas(planeBM);

    Rect r = new Rect();

    //Point center = new Point(cs.getWidth() / 2, cs.getHeight() /2);
    Point center = new Point(0, 0);

    // Draw fuselage
    r.left = center.x - PLANE_WIDTH / 2;
    r.right = r.left + PLANE_WIDTH;
    r.top = center.y - PLANE_SIZE / 3;
    r.bottom = r.top + PLANE_SIZE;

    c.drawRect(r, paint);

    // Draw wing (REMOVED)

    // Draw stabilizer    (REMOVED)

    // TODO Draw Speed vector


    // "Merging" canvas

    Matrix merge = new Matrix(cs.getMatrix());
    //merge.setTranslate(0, 0);
    //merge.setRotate(bearing, center.x, center.y);
    cs.drawBitmap(planeBM, merge, paint);
    cs.save();

}

Basically my plane never shows.

I assume this has to do with the matrix in the initial canvas which has large values (I assume these are sort of geographical coordinates).

It all seems to be consistent though (the plane location has large values as well consistent with the matrix).

I have tried a number of things :

  • drawing from the actual plane location (large values) : did not help;
  • setting the matrix of my new canvas with the overlay canvas matrix : did not help;
  • merging with a new "empty" matrix : did not help; -...

I know that my image contains the plane (at least if I draw from 0,0 or the new canvas center as I saved it to the SD to check...

Was it helpful?

Solution

In case this is usefull to someone, I finally found a solution.

Basicaly I was trying to do too much at the same time. Using setPostRotate instead of setRotate on the merge matrix did solve the issue (that and goig bac to the drawing board for the correct translation parameters).

Using the below as a merge matrix worked:

 // "Merging" canvas
 Matrix merge = new Matrix();
 merge.setTranslate(loc.x - pCenter.x, loc.y - pCenter.y);
 merge.postRotate(bearing, loc.x, loc.y);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top