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.
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.
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.
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.
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;
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With