Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Redrawing bitmap on Google Maps v2 for Android

In this scenario I want to draw a bitmap on Google Maps using gms v2 and each user position update enforces bitmap update. Currently I use following code snippet:

public void init(){
    result = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
    canvas = new Canvas(result);
}

public void update(){
    // draw on canvas ...
    draw(result);
}

public void draw(Bitmap modifiedBmp) {
    if (overlay != null) {
        overlay.remove();
    }

    BitmapDescriptor descriptor = BitmapDescriptorFactory.fromBitmap(modifiedBmp);
    overlay = map.addGroundOverlay(new GroundOverlayOptions().image(descriptor).positionFromBounds(bounds).zIndex(100));
} 

The update() method is called each second. I find this approach extremely inefficient and I'm searching for a better solution (i.e. that doesn't require to add/remove overlay after each update). Drawing primitives on map using addPolygon(...) and addPolyline(...) isn't an option because I require drawing capabilities not present in standard api.

like image 729
jethro Avatar asked Jul 15 '13 22:07

jethro


People also ask

What is the latest version of Google Maps for Android?

July 18, 2022 The Maps SDK for Android version 18.1. 0 is now available. See the Release Notes for information about this release and for all previous releases. If you are a new user, see Set Up in the Google Cloud Console to start the installation process.

What is bit map in android?

A bitmap is simply a rectangle of pixels. Each pixel can be set to a given color but exactly what color depends on the type of the pixel. The first two parameters give the width and the height in pixels. The third parameter specifies the type of pixel you want to use.


2 Answers

One optimization could be to check if the new position is the same as the old one and don't redraw if that is the case. Also I don't think that the descriptor need to be created each time.

Another approach for moving markers is described here. It's the one from the official sample.

like image 192
Warpzit Avatar answered Nov 09 '22 03:11

Warpzit


I'm not sure if this is what you want, but this is how I used custom bitmaps in Google Maps.

The marker code:

BitmapDescriptor iconBitmap = BitmapDescriptorFactory
                    .fromResource(R.drawable.item_map_marker);

MarkerOptions options = new MarkerOptions();
options.position(new LatLng(hs.lat, hs.lng));
options.title(hs.sitename);
options.snippet(hs.street + ", " + hs.suburb);
options.icon(iconBitmap);

mMap.addMarker(options);

The tooltip adapter:

public class MyInfoWindowAdapter implements InfoWindowAdapter {

    public interface OnRenderCustomInfoWindow {

        public void onRender(Marker marker, View mWindow);
    }

    private View                     mWindow;

    private OnRenderCustomInfoWindow mRenderer;

    public MyInfoWindowAdapter(Context context, 
            OnRenderCustomInfoWindow onRender) {
        mRenderer = onRender;
        mWindow = LayoutInflater.from(context).inflate(
            R.layout.view_services_map_infowindow, null);
    }

    @Override
    public View getInfoWindow(Marker marker) {
        mRenderer.onRender(marker, mWindow);
        return mWindow;
    }

    @Override
    public View getInfoContents(Marker marker) {
        return null;
    }
}
like image 38
Brad Avatar answered Nov 09 '22 04:11

Brad