5

Is it possible to define in Layouts folder (Android) an XML file where i specify how my pointer/marker will look like? For example, i would like to have an image and a TextView as Marker (not the popup, but the marker itself).

I have been using Google Maps Utility Library to use Clusters on Google Maps, but they just have examples how to do it using the normal white marker with background (example)

Lets say that i want exactly what do they have, except the white board around.

Do you know how can i accomplish this?

Thanks in advance.

EDIT:

I'm trying to combine this tutorial with Google Maps Utility Library (Clusters). For now i have this, but is not working:

custom_cluster_marker_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" >

    <ImageView
        android:id="@+id/image"
        android:layout_width="55dp"
        android:layout_height="65dp"
        android:src="@drawable/cluster" />

    <TextView
        android:id="@+id/num_txt"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="19dp"
        android:layout_marginTop="10dp"
        android:gravity="center"
        android:text="0"
        android:textColor="#ce8223"
        android:textSize="25dp"
        android:textStyle="bold" />

</RelativeLayout>

MeterRender.java

private class MeterRenderer extends DefaultClusterRenderer<MyMeter> {


        private TextView mClusterTextView;

        public MeterRenderer() {
            super(c, map, mClusterManager);

            View custom_cluster_view = ((LayoutInflater) c.getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.custom_cluster_marker_layout, null);
            mClusterTextView = (TextView) custom_cluster_view.findViewById(R.id.num_txt);
        }

        @Override
        protected void onBeforeClusterItemRendered(MyMeter meter, MarkerOptions markerOptions) {

            markerOptions.icon(BitmapDescriptorFactory
                    .fromPath(createBillboardTexture("a", "123")));
        }

        @Override
        protected void onBeforeClusterRendered(Cluster<MyMeter> cluster, MarkerOptions markerOptions) {
            View custom_cluster_view = ((LayoutInflater) c.getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.custom_cluster_marker_layout, null);
            mClusterTextView = (TextView) custom_cluster_view.findViewById(R.id.num_txt);
            mClusterTextView.setText(cluster.getSize());
            markerOptions.icon(BitmapDescriptorFactory.fromBitmap(createDrawableFromView(c, custom_cluster_view)));


        }

        public Bitmap createDrawableFromView(Context context, View view) {
            DisplayMetrics displayMetrics = new DisplayMetrics();
            ((Activity) context).getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);

            view.measure(displayMetrics.widthPixels, displayMetrics.heightPixels);
            view.layout(0, 0, displayMetrics.widthPixels, displayMetrics.heightPixels);
            view.buildDrawingCache();
            Bitmap bitmap = Bitmap.createBitmap(view.getMeasuredWidth(), view.getMeasuredHeight(), Bitmap.Config.ARGB_8888);

            Canvas canvas = new Canvas(bitmap);
            view.draw(canvas);

            return bitmap;
        }
}
Bugdr0id
  • 2,962
  • 6
  • 35
  • 59
  • Did you get a chance to solve this issue? if so how? I similar issue: http://stackoverflow.com/questions/25965799/xml-layout-to-map-marker – TheDevMan Sep 22 '14 at 02:50
  • HI, if you resolved this issue, kindly help me. I am facing same problem. I want to display dynamic content like numbers on map marker icons with different colors. – shyam.y Jan 19 '15 at 06:27
  • the snippet code looks good but createDrawableFromView is wrote wrong, you didn't set view.setDrawingCacheEnabled(true) http://developer.android.com/reference/android/view/View.html#setDrawingCacheEnabled(boolean) looks this comment http://stackoverflow.com/questions/2339429/android-view-getdrawingcache-returns-null-only-null/4618030#4618030 – horkavlna Dec 02 '15 at 12:30

2 Answers2

1

You probably already have added Marker in your map, so try use Marker instead of MarkerOptions

@Override
protected void onBeforeClusterItemRendered(MyMeter meter, Marker marker) {

    marker.setIcon(BitmapDescriptorFactory
                    .fromPath(createBillboardTexture("a", "123")));
}

@Override
protected void onBeforeClusterRendered(Cluster<MyMeter> cluster, Marker marker) {
    View custom_cluster_view = ((LayoutInflater) c.getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.custom_cluster_marker_layout, null);
    mClusterTextView = (TextView) custom_cluster_view.findViewById(R.id.num_txt);
    mClusterTextView.setText(cluster.getSize());
    marker.setIcon(BitmapDescriptorFactory.fromBitmap(createDrawableFromView(c, custom_cluster_view)));   
}
RediOne1
  • 10,389
  • 6
  • 25
  • 46
0

You can use your own image as a marker. You can load the icon from sources.

fromAsset(String assetName) – Loading from assets folder
fromBitmap (Bitmap image) – Loading bitmap image
fromFile (String path) – Loading from file
fromResource (int resourceId) – Loading from drawable resource

// create marker
MarkerOptions marker = new MarkerOptions().position(new LatLng(latitude, longitude)).title("Hello Maps");

// Changing marker icon
marker.icon(BitmapDescriptorFactory.fromResource(R.drawable.my_marker_icon)));

// adding marker
googleMap.addMarker(marker);
android_guy
  • 236
  • 1
  • 7
  • 18
  • 3
    I know how to define my own icon, but this problem is different. The main problem for me is that i want to have dynamic information on this icon/marker, thats why i would like to have something defined on Layout.xml to be able to access to his TextView (for example) and change it whenever i need. – Bugdr0id Feb 12 '14 at 09:11
  • I would love an answer too. When I use a layout, it is blank. I want to change background colors based on type of marker. A static image wont cut it. And I am not making multiple sets of icons to accomplish this. – TheLettuceMaster Jul 12 '14 at 20:29
  • Did you get a chance to solve this issue? if so how? I similar issue: http://www.stackoverflow.com/questions/25965799/xml-layout-to-map-marker – TheDevMan Sep 22 '14 at 05:15