Question

I override the drawMyLocation of MyLocationOverlay to display an arrow as marker instead of the normal blue dot. However, I also need to change the direction to which the pointer is pointing depending on the sensor of the phone. I decided to call drawMyLocation inside onSensorChanged. But one of the parameters required to call drawMyLocation is a Canvas. How can I access the Canvas of the MyLocationOverlay? Or do I need to create a new Canvas and pass the new Canvas everytime I call drawMyLocation inside onSensorChanged?

Below is my code when I tried to override draw method. However, even though I can see that it was continuously being executed, it takes a while for the rotated bitmap to display.

**UPDATE: If I try to touch my map, I can see my arrow rotating as I touch the map. However, if I don't touch it, it takes a while for the arrow to update its direction.

public boolean draw(android.graphics.Canvas canvas, MapView mapView, boolean shadow, long when) 
{
    Log.v("Message", "Executing draw method...");        
    boolean result;        
    result = super.draw(canvas, mapView, shadow, when);

    if(geoLastFix != null) //geoLastFix contains the GeoPoint of my Location.
    {
        Point screenPts = mapView.getProjection().toPixels(geoLastFix, null);

        //rotate bitmap
        Bitmap arrowBitmap = marker;
        Matrix matrix = new Matrix();           
        matrix.postRotate(orientation);
        Bitmap rotatedBmp = Bitmap.createBitmap(
            arrowBitmap, 
            0, 0, 
            arrowBitmap.getWidth(), 
            arrowBitmap.getHeight(), 
            matrix, 
            true
        );
       //print bitmap to canvas
        canvas.drawBitmap(
            rotatedBmp, 
            screenPts.x - (rotatedBmp.getWidth()  / 2), 
            screenPts.y - (rotatedBmp.getHeight() / 2), 
            null
        );
    }        
    return result;
}
Was it helpful?

Solution

You should not be accessing the canvas outside of the draw() method. If you find yourself needing to do drawing or use the canvas, you should override the draw() method.

Use the onSensonrChanged method to save the current orientation or sensor information into your classes state and then override the draw() method and use the saved state to augment your drawing routine.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top