简体   繁体   中英

Google maps api custom marker

I am looking for good tourtial or solution for custom marker from xml file (RelativeLayout).

I have:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/lay"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >

<ImageView
    android:layout_width="55dp"
    android:layout_height="65dp"
    android:src="@drawable/custom_marker" />

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

    </RelativeLayout>

How I can use it as marker?

Well, there is some kind of workaround to turn your layout into Drawable which can be used as a marker image:

LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
RelativeLayout view = (RelativeLayout)inflater.inflate(R.layout.your_layout, null);
view.setDrawingCacheEnabled(true);
view.setLayoutParams(new LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT));
view.measure(MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED), MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
view.layout(0, 0, view.getMeasuredWidth(), view.getMeasuredHeight()); 

view.buildDrawingCache();
//EDIT: updated drawable creation
Drawable d = new BitmapDrawable(context.getResources(), view.getDrawingCache().copy(Config.ARGB_8888, true)); //this drawable can be used as a marker

// **** EDIT2: don't forget to set bounds to your drawable
d.setBounds(0, 0, view.getMeasuredWidth(), view.getMeasuredHeight());
view.setDrawingCacheEnabled(false);

As you might understand, you need to store Context pointer in order to obtain LayoutInflater

Hope it helps

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM