在Google Map v2自定义标记中写入文字 - Android

时间:2015-06-08 09:26:21

标签: android google-maps-markers

在我的地图中,我有一些标记列表,现在我需要实现输出,如下所示enter image description here

即。索引号的标记。

我搜索了谷歌,但没有得到任何解决方案,请帮我理解实现这一点。

2 个答案:

答案 0 :(得分:3)

使用Google地图标记执行任何操作

我已经做了一个非常灵活和有趣的黑客来实现你想要的任何标记。

我的工作是,

  1. 创建XML布局文件,并按照您希望标记显示的方式进行设计。例如,在你的情况下,它将是一个带有TextView的绿色标记图像。

  2. 根据要求设置TextViews的值。

  3. 将布局文件转换为图像并获取BitmapDescriptor对象。

  4. 使用刚刚创建的图片创建自定义标记。

  5. 此类视图对Info Window的好处是您可以同时打开多个标记的窗口。与在InfoWindow中一样,您只能一次打开一个标记。

    代码示例

    Check out my this answer for Code Help.

答案 1 :(得分:2)

对于自定义标记:

  • 您可以在顶部创建custom imagedraw以及index number image
  • 查看custom marker
  • 的此示例

Marker Window

您可以创建包含xmlImageView的自定义TextViews文件:

类似于customMapView.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:background="@color/white_full"
    android:layout_width="match_parent"
    android:padding="@dimen/padding_micro"
    android:layout_height="match_parent">

    <ImageView
        android:id="@+id/img"
        android:layout_gravity="left"
        android:src="@drawable/ic_launcher"
        android:textColor="@color/black_full"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <TextView
        android:gravity="right"
        android:id="@+id/name"
        android:text="test"
        android:textColor="@color/black_full"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

      <!-- Other TextViews or Whatever you want-->

</RelativeLayout>

初始化Markers后,请致电setInfoWindowAdapter并充气your custom view。类似的东西:

    private void customizeMarkerInfoWindow(){
        //Setting custom info window
        mMap.setInfoWindowAdapter(new GoogleMap.InfoWindowAdapter() {
            //Use default infoWindow frame
            @Override
            public View getInfoWindow(Marker marker) {
                return null;
            }

            @Override
            public View getInfoContents(Marker marker) {
                // Getting view from the layout file
                View view = mActivity.getLayoutInflater().inflate(R.layout.customMapView, null);
                // Getting the position from the marker
                LatLng latLng = marker.getPosition();
                //UI elements
                ImageView img = (TextView) view.findViewById(R.id.img);
                TextView coord = (TextView) view.findViewById(R.id.name);
                mukAdTv.setText(mukAd);
                //You set the image here depending on how you want to set it, if you are downloading from internet you can use PICASSO for it and set it to img
                //With picasso
                Picasso.with(mActivity).load(YOUR_IMAGE_URL).into(img);
                //From drawables
                //img.setBackground(yourDrawable);
                name.setText(latLng.latitude + ", " + latLng.longitude);

                return view;
            }
        });
    }