谷歌地图上的自定义标记问题

3

我在我的Google地图应用程序中使用了自定义标记,使用了位图和画布。当我在安卓版本5.1或其他版本上运行它时,标记上的图像显示正常,但在4.4版本上不行。

我确定这不是版本问题,但我很困惑,无法找出我的错误所在。请帮助我找到问题所在。

自定义标记(位图)代码:

Bitmap.Config conf = Bitmap.Config.ARGB_8888;
            Bitmap bmp = Bitmap.createBitmap(128, 128, conf);
            Canvas canvas1 = new Canvas(bmp);


    canvas1.drawBitmap(BitmapFactory.decodeResource(getResources(),
                        R.drawable.map_markers_green), 0, 0, color);

                canvas1.drawText(String.valueOf(a).toUpperCase(), 56, 53, color);
                googleMap.addMarker(new MarkerOptions()
                        .icon(BitmapDescriptorFactory.fromBitmap(bmp))
                                // Specifies the anchor to be at a particular point in the marker image.
                        .anchor(0.5f, 1));
在设备上正常工作的标记图像:

enter image description here

在不同设备上被切割的相同标记图像:

enter image description here


请注意,这可能是由于不同设备的屏幕尺寸或分辨率不同造成的。建议使用适当的图像大小和分辨率来避免这种情况。
1个回答

2

试试这段代码

Bitmap.Config conf = Bitmap.Config.ARGB_8888;
Bitmap bmp = Bitmap.createBitmap(200, 50, conf); 
 Canvas canvas = new Canvas(bmp);
canvas.drawText("TEXT", 0, 50, paint); // paint defines the textcolor, stroke width, size
 mMap.addMarker(new MarkerOptions()
                            .position(clickedPosition)
                            //.icon(BitmapDescriptorFactory.fromResource(R.drawable.marker2))
                            .icon(BitmapDescriptorFactory.fromBitmap(bmp))
                            .anchor(0.5f, 1)
                                );

不,问题仍然存在,这段代码也没有起作用。对我来说还是不起作用。 - young_08
我在我的代码中做了同样的事情,所以它在相同的设备上运行良好。但是在某些设备上,标记由于某种原因被切断了。 - young_08
还有一件事情,它可能会通过增加位图的宽度和高度来解决。但是这些更改会在原本正常工作的设备上出现问题。 - young_08
尝试在绘制文本方法中将56替换为0,在创建位图方法的第二个参数中将128替换为32。 - Vishal Thakkar
是的。我借鉴了你的代码并应用到我的项目中,问题已经解决了。 - young_08
显示剩余4条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接