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

时间:2014-02-11 18:38:40

标签: android google-maps google-maps-markers

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

我一直在使用Google Maps Utility Library在Google地图上使用Clusters,但他们只是举例说明如何使用带有背景的常规白色标记(example

让我们说我想要他们拥有的东西,除了周围的白板。

你知道我怎么能做到这一点?

提前致谢。

修改

我正在尝试将this tutorial与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>

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;
        }
}

2 个答案:

答案 0 :(得分:1)

您可能已在地图中添加了Marker,因此请尝试使用Marker代替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)));   
}

答案 1 :(得分:0)

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

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);