我正在尝试为Android地图创建自定义标记。我正在使用Google Utility中的IconGenerator。我的自定义标记包括ParseImageView(来自Parse SDK的图像视图)。
这是自定义标记的代码。
这是在我的函数generateRichMarkerIcon
IconGenerator iconGenerator = new IconGenerator(getContext());
ParseImageView imageView = new ParseImageView(getContext());
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setLayoutParams(new ViewGroup.LayoutParams(70, 70));
imageView.setParseFile(imageFile);
imageView.loadInBackground();
iconGenerator.setContentView(imageView);
if (selected) {
iconGenerator.setColor(Color.BLUE);
}
return BitmapDescriptorFactory.fromBitmap(iconGenerator.makeIcon());
然后我像这样使用它
BitmapDescriptor icon = generateRichMarkerIcon(false, card);
Marker marker = this.googleMap.addMarker(new MarkerOptions()
.icon(icon)
.position(new LatLng(lat, lng)));
尽管如此,我看不到图像,但是没有图像视图的空白标记(尺寸很好,70x70)。
这是屏幕截图:https://drive.google.com/file/d/1-t5FOrKotac5YMfNoLbZo1NFjEFGpqtg/view?usp=sharing
此代码有什么问题?
答案 0 :(得分:0)
可能是因为你在图像加载图像之前在图像加载之前制作了图标,因为你在背景中加载图像需要等到图像加载之后才能完成关于iconGenerator的所有操作。
答案 1 :(得分:0)
如果您愿意,还可以使用Canvas自定义标记。我做了它使用Canvas&它帮助我:
Marker myLocMarker = mMap.addMarker(new MarkerOptions()
.position(sydney)
.icon(BitmapDescriptorFactory.fromBitmap(writeTextOnDrawable(R.drawable.ic_marker_black, "$125")))); // where ic_marker_black is my marker custom Image, & "$125" is text i want to put on marker.
使用以下方法
private Bitmap writeTextOnDrawable(int drawableId, String text) {
Bitmap bm = BitmapFactory.decodeResource(getResources(), drawableId)
.copy(Bitmap.Config.ARGB_8888, true);
Typeface tf = Typeface.create("Helvetica", Typeface.BOLD);
Paint paint = new Paint();
paint.setStyle(Paint.Style.FILL);
paint.setColor(Color.WHITE);
paint.setTypeface(tf);
paint.setTextAlign(Paint.Align.CENTER);
paint.setTextSize(convertToPixels(MapActivity.this, 18));
Rect textRect = new Rect();
paint.getTextBounds(text, 0, text.length(), textRect);
Canvas canvas = new Canvas(bm);
//If the text is bigger than the canvas , reduce the font size
if (textRect.width() >= (canvas.getWidth() - 4)) //the padding on either sides is considered as 4, so as to appropriately fit in the text
paint.setTextSize(convertToPixels(MapActivity.this, 18)); //Scaling needs to be used for different dpi's
//Calculate the positions
int xPos = (canvas.getWidth() / 2) - 2; //-2 is for regulating the x position offset
//"- ((paint.descent() + paint.ascent()) / 2)" is the distance from the baseline to the center.
int yPos = (int) ((canvas.getHeight() / 2) - ((paint.descent() + paint.ascent()) / 2));
canvas.drawText(text, xPos, yPos, paint);
return bm;
}
public static int convertToPixels(Context context, int nDP) {
final float conversionScale = context.getResources().getDisplayMetrics().density;
return (int) ((nDP * conversionScale) + 0.5f);
}
希望这有助于你!