This exercise will help to set custom marker instead of default marker.🚩
Something like in the screenshot below
To set an custom marker you can use the previous exercise - Draw Polyline on GoogleMap of Google Maps Android API v2
And make an edit on set marker on it.
Place where the marker is set
Custom Marker function[getMarkerBitmapFromView(int,context)]
For CircleImageView - add the dependency
For marker icon you can use 9-patch image - its better
Something like in the screenshot below
To set an custom marker you can use the previous exercise - Draw Polyline on GoogleMap of Google Maps Android API v2
And make an edit on set marker on it.
Place where the marker is set
MarkerOptions markerOptions = new MarkerOptions();
markerOptions.position(latLng);
markerOptions.icon(BitmapDescriptorFactory.fromBitmap(getMarkerBitmapFromView(R.drawable.pulis, context)));
markerOptions.position(latLng);
markerOptions.icon(BitmapDescriptorFactory.fromBitmap(getMarkerBitmapFromView(R.drawable.pulis, context)));
Custom Marker function[getMarkerBitmapFromView(int,context)]
public static Bitmap getMarkerBitmapFromView(@DrawableRes int resId, Context context, int check) {
View customMarkerView = ((LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.view_custom_marker, null);
CircleImageView markerImageView = customMarkerView.findViewById(R.id.profile_image);
markerImageView.setImageResource(resId);
customMarkerView.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED);
customMarkerView.layout(0, 0, customMarkerView.getMeasuredWidth(), customMarkerView.getMeasuredHeight());
customMarkerView.buildDrawingCache();
Bitmap returnedBitmap = Bitmap.createBitmap(customMarkerView.getMeasuredWidth(), customMarkerView.getMeasuredHeight(),
Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(returnedBitmap);
canvas.drawColor(Color.WHITE, PorterDuff.Mode.SRC_IN);
Drawable drawable = customMarkerView.getBackground();
if (drawable != null)
drawable.draw(canvas);
customMarkerView.draw(canvas);
return returnedBitmap;
}
View customMarkerView = ((LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.view_custom_marker, null);
CircleImageView markerImageView = customMarkerView.findViewById(R.id.profile_image);
markerImageView.setImageResource(resId);
customMarkerView.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED);
customMarkerView.layout(0, 0, customMarkerView.getMeasuredWidth(), customMarkerView.getMeasuredHeight());
customMarkerView.buildDrawingCache();
Bitmap returnedBitmap = Bitmap.createBitmap(customMarkerView.getMeasuredWidth(), customMarkerView.getMeasuredHeight(),
Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(returnedBitmap);
canvas.drawColor(Color.WHITE, PorterDuff.Mode.SRC_IN);
Drawable drawable = customMarkerView.getBackground();
if (drawable != null)
drawable.draw(canvas);
customMarkerView.draw(canvas);
return returnedBitmap;
}
For CircleImageView - add the dependency
implementation 'de.hdodenhof:circleimageview:2.2.0'
For marker icon you can use 9-patch image - its better
_________________________________________________________________________________
Happy Coding...
Comments
Post a Comment