【问题标题】:Write text in Google Map v2 custom marker - Android在 Google Map v2 自定义标记中写入文本 - Android
【发布时间】:2015-06-08 09:26:21
【问题描述】:

在我的地图中,我有一些标记列表,现在我需要实现如下输出

即带有索引号的标记。

我在谷歌上搜索过,但没有得到任何解决方案,请帮我想出一个实现这个的想法。

【问题讨论】:

  • 请问你是怎么做带有数字的图标的?谢谢
  • @ItuokeAjanlekoko 首先将您的标记转换为位图,然后使用油漆和画布在标记上写数字。

标签: android google-maps-markers


【解决方案1】:

使用 Google 地图标记随心所欲

我做了一个非常灵活和有趣的 hack 来实现你想要的任何类型的标记。

我所做的是,

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

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

  3. 将 Layout 文件转换为图片并获取 BitmapDescriptor 对象。

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

这种视图优于信息窗口的好处是您可以同时打开多个标记的窗口。与在 InfoWindow 中一样,您一次只能为一个标记打开它。

代码示例

Check out my this answer for Code Help.

【讨论】:

    【解决方案2】:

    对于自定义标记:

    • 您可以在顶部创建custom imagedrawindex numberimage
    • 查看此示例以获取 custom marker

    对于Marker Window

    您可以创建包含ImageViewTextViews 的自定义xml 文件:

    类似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;
                }
            });
        }
    

    【讨论】:

    • 感谢 custommarker 链接。这为获得解决方案提供了线索:)
    猜你喜欢
    • 2013-06-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-11-02
    相关资源
    最近更新 更多