Skip to content

Instantly share code, notes, and snippets.

@rajohns08
Last active June 9, 2016 00:30
Show Gist options
  • Save rajohns08/7bcca07bb87889bf8d37f308ed7c6eec to your computer and use it in GitHub Desktop.
Save rajohns08/7bcca07bb87889bf8d37f308ed7c6eec to your computer and use it in GitHub Desktop.
Android - Use layout file for map marker
public static MarkerOptions createMarker(Context context, LatLng point, int bedroomCount) {
MarkerOptions marker = new MarkerOptions();
marker.position(point);
int px = context.getResources().getDimensionPixelSize(R.dimen.map_marker_diameter);
View markerView = ((LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.map_circle_text, null);
markerView.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
markerView.layout(0, 0, px, px);
markerView.buildDrawingCache();
TextView bedNumberTextView = (TextView)markerView.findViewById(R.id.bed_num_text_view);
Bitmap mDotMarkerBitmap = Bitmap.createBitmap(px, px, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(mDotMarkerBitmap);
bedNumberTextView.setText(bedroomCount + "");
markerView.draw(canvas);
marker.icon(BitmapDescriptorFactory.fromBitmap(mDotMarkerBitmap));
return marker;
}
<?xml version="1.0" encoding="utf-8"?>
<shape
xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">
<solid
android:color="#666666"/>
<size
android:width="@dimen/map_marker_diameter"
android:height="@dimen/map_marker_diameter"/>
</shape>
<?xml version="1.0" encoding="utf-8"?>
<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/bed_num_text_view"
android:gravity="center"
android:layout_width="@dimen/map_marker_diameter"
android:layout_height="@dimen/map_marker_diameter"
android:background="@drawable/map_circle_pin"
tools:text="4+"
android:textColor="@color/white">
</TextView>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment