在布局 XML 中定义的谷歌地图标记

Bug*_*0id 5 android google-maps google-maps-markers

是否可以在 Layouts 文件夹(Android)中定义一个 XML 文件,在其中指定我的指针/标记的外观?例如,我想要一个图像和一个 TextView 作为标记(不是弹出窗口,而是标记本身)。

我一直在使用 Google Maps Utility Library 在 Google Maps 上使用 Clusters,但他们只有示例如何使用带有背景的普通白色标记(示例

可以说我想要他们有什么,除了周围的白板。

你知道我怎样才能做到这一点吗?

提前致谢。

编辑:

我正在尝试将本教程 与 Google Maps Utility Library (Clusters)结合起来。现在我有这个,但不起作用:

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>
Run Code Online (Sandbox Code Playgroud)

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;
        }
}
Run Code Online (Sandbox Code Playgroud)

and*_*guy 0

您可以使用自己的图像作为标记。您可以从源加载图标。

\n\n
fromAsset(String assetName) \xe2\x80\x93 Loading from assets folder\nfromBitmap (Bitmap image) \xe2\x80\x93 Loading bitmap image\nfromFile (String path) \xe2\x80\x93 Loading from file\nfromResource (int resourceId) \xe2\x80\x93 Loading from drawable resource\n\n// create marker\nMarkerOptions marker = new MarkerOptions().position(new LatLng(latitude, longitude)).title("Hello Maps");\n\n// Changing marker icon\nmarker.icon(BitmapDescriptorFactory.fromResource(R.drawable.my_marker_icon)));\n\n// adding marker\ngoogleMap.addMarker(marker);\n
Run Code Online (Sandbox Code Playgroud)\n

  • 我知道如何定义自己的图标,但这个问题有所不同。对我来说,主要问题是我想要有关此图标/标记的动态信息,这就是为什么我希望在 Layout.xml 上定义一些内容,以便能够访问他的 TextView(例如)并在需要时更改它。 (3认同)