简体   繁体   English

Google Maps API自定义标记

[英]Google maps api custom marker

I am looking for good tourtial or solution for custom marker from xml file (RelativeLayout). 我正在从xml文件(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: 好吧,有一种解决方法可以将布局转换为Drawable并用作标记图像:

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 如您所知,您需要存储Context指针以获得LayoutInflater

Hope it helps 希望能帮助到你

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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