问题
I am trying to colour some building on Google Maps like this:
I am able to overlay a polygon object on top of the building but Im not able to apply a label on top of it (Building's ID).
How can overlay text on buildings like it's shown in the image above with Android?
回答1:
I know this question is a bit old, but I just figured out how to do this for my own project and thought I'd share. The method is to create a bitmap image of your text, and then display it as a GroundOverlay on your polygon with a zIndex greater than the polygon.
If you've got the list of LatLngs for your polygon you can do the following:
private void showText(List<LatLng> latLngList) {
LatLngBounds.Builder builder = new LatLngBounds.Builder();
for (LatLng latLng : latLngList) {
builder.include(latLng);
}
googleMap.addGroundOverlay(new GroundOverlayOptions()
.positionFromBounds(builder.build())
.image(
BitmapDescriptorFactory.fromBitmap(
getBitmapFromView()
)
)
.zIndex(100)
);
}
The .zIndex(100) part is important since you'll want the text to be displayed over your polygon. (I'm not actually sure what the maximum z index is but 100 works).
Here's the method to transform a layout into a bitmap.
private Bitmap getBitmapFromView() {
View customView = ((LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.my_text_layout, null);
customView.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED);
customView.layout(0, 0, customView.getMeasuredWidth(), customView.getMeasuredHeight());
customView.buildDrawingCache();
Bitmap returnedBitmap = Bitmap.createBitmap(customView.getMeasuredWidth(), customView.getMeasuredHeight(),
Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(returnedBitmap);
canvas.drawColor(Color.WHITE, PorterDuff.Mode.SRC_IN);
Drawable drawable = customView.getBackground();
if (drawable != null) {
drawable.draw(canvas);
}
customView.draw(canvas);
return returnedBitmap;
}
If you just want the text, then your layout 'R.layout.my_text_layout' can just be a layout with a TextView.
来源:https://stackoverflow.com/questions/42170114/google-maps-text-overlay-android