0

i want to show a custom marker in google maps and marker's color is subjected to change dynamically when some specific events happen.So i was wondering which is the best way to draw shape like a map marker enter image description here

sijeesh
  • 298
  • 1
  • 15

3 Answers3

2

I have created a custom view that will draw shape like default marker. Here is the code :

public class CustomView extends View {
private Paint paint;
private RectF oval;
private Path fillPath;

public CustomView(Context context) {
    super(context);
    init();
}

public CustomView(Context context, AttributeSet attrs) {
    super(context, attrs);
    init();
}

public CustomView(Context context, AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
    init();

}

@Override
protected void onDraw(Canvas canvas) {

    oval.bottom = getHeight();
    oval.left = 0;
    oval.right = getWidth();
    oval.top = 0;
    // canvas.drawRect(oval, paint);
    canvas.drawArc(oval, 0, -180, true, paint);

    fillPath.moveTo(0, getHeight() / 2); // Your origin point
    fillPath.lineTo(getWidth(), getHeight() / 2); // First point
    // Repeat above line for all points on your line graph
    fillPath.lineTo(getWidth() / 2, getHeight()); // Final point
    fillPath.lineTo(0, getHeight() / 2); // Same origin point
    canvas.drawPath(fillPath, paint);

}

private void init() {
    android.view.ViewGroup.LayoutParams params = new LayoutParams(
            LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
    setLayoutParams(params);
    paint = new Paint();
    paint.setAntiAlias(true);
    paint.setColor(Color.RED);
    oval = new RectF();
    fillPath = new Path();
}

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    int width = getMeasuredWidth();
    int height = getMeasuredHeight();

    setMeasuredDimension(width, height);
    Log.e("H" + width, "W" + height);

}

  }

Hope this might help you.

avinash
  • 1,744
  • 24
  • 40
1

@sijeesh: Yes.You can set Custom Marker (Image Should be Small) .

Markers indicate single locations on the map. You can customize your markers by changing the default color, or replacing the marker icon with a custom image.

First import the following class:

import com.google.android.gms.maps.model.MarkerOptions;

Now you can set Custom marker this way

MarkerOptions custommarker = new MarkerOptions().icon(BitmapDescriptorFactory.fromResource(R.drawable.your_custom_icon));

For more info Please Have a look Android Custom marker with ImageView

My Advise

Please follow Official documentations.I hope it will helps you .

https://developers.google.com/maps/documentation/android-api/marker

Community
  • 1
  • 1
IntelliJ Amiya
  • 74,896
  • 15
  • 165
  • 198
  • 1
    hi, i know that we can draw custom marker, but my question is how to draw that particular shape at runtime using shape drawable – sijeesh Sep 14 '15 at 12:35
  • 1
    @sijeesh : Try to create a custom view, in onDraw() method you could draw any shape as per your requirement. Then, create the bitmap of the custom view and pass the bitmap to the MarkerOptions. – avinash Sep 15 '15 at 09:59
0

If you only need to change color of marker, you'd better make marker icon as a simple image and just change its tinting color when required.

Bitmap original = BitmapFactory.decodeResource(getResources(), R.drawable.ic_brush_black_24dp);
        Bitmap colored = Bitmap.createBitmap(original.getWidth(), original.getHeight(), Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(colored);
        canvas.drawBitmap(original, 0, 0, null);
        canvas.drawColor(Color.RED, PorterDuff.Mode.SRC_ATOP);
        marker.setIcon(BitmapDescriptorFactory.fromBitmap(colored));
dtx12
  • 4,438
  • 1
  • 16
  • 12