在图像视图不工作的情况下映射自定义标记

Map Custom Markers With Image View Not Working

我正在尝试为 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

这段代码有什么问题?

可能是因为您在图像视图中加载图像之前创建了图标,因为您在后台加载图像时需要等到图像加载完毕,然后再执行所有与 iconGenerator 相关的操作。

如果需要,您还可以使用 Canvas 自定义标记。我使用 Canvas 做到了,它对我有帮助:

  Marker myLocMarker = mMap.addMarker(new MarkerOptions()
                        .position(sydney)
                        .icon(BitmapDescriptorFactory.fromBitmap(writeTextOnDrawable(R.drawable.ic_marker_black, "5")))); // where ic_marker_black is my marker custom Image, & "5" 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);

    }

希望对您有所帮助!

您的问题也与此类似Question. Refer this tutorial