2

I'm trying to create custom markers for android map. I'm using IconGenerator from Google Utility. My custom marker includes a ParseImageView (image view from Parse SDK).

This is the code for the custom marker.

This is inside my function 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());

And then I use it like this

BitmapDescriptor icon = generateRichMarkerIcon(false, card);
            Marker marker = this.googleMap.addMarker(new MarkerOptions()
                    .icon(icon)
                    .position(new LatLng(lat, lng)));

Still, I can't see the image, but a blank marker (size is fine, 70x70) without image view inside.

This is a screenshot: https://drive.google.com/file/d/1-t5FOrKotac5YMfNoLbZo1NFjEFGpqtg/view?usp=sharing

What is wrong with this code?

Abhab
  • 23
  • 5

2 Answers2

0

Maybe its cause due to you make icon before the image load in image view as you load an image in the background you need to wait until image has been loaded after that do all stuff regarding iconGenerator.

Munir
  • 2,548
  • 1
  • 11
  • 20
0

If you want then you can also customize markers with use of Canvas. I did it Using Canvas & it helps me:

  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.

Use following method

 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);

    }

Hope This helps You!

Your Question is Similar like this Question. Refer this tutorial too.

Bhoomika Patel
  • 1,895
  • 1
  • 13
  • 30