简体   繁体   English

如何在Google地图中同时显示所有标记标签?

[英]How to show all marker labels in Google Maps simultaenously?

I am writing a code to show all my friends on a marker. 我正在编写代码以在标记上显示我所有的朋友。 This is my code to add markers on my map. 这是我的代码,可在地图上添加标记。 I want to show all marker titles simultaneously. 我想同时显示所有标记标题。 In the sample image below, I can see every CCD shop's title is shown simultaneously. 在下面的示例图像中,我可以看到同时显示了每个CCD商店的标题。

在此处输入图片说明

GoogleMap mMap = // map initialized here

mMap.addMarker(new MarkerOptions()
                    .position(new LatLng(lat, lng))
                    .title("My Nearby Friend")
                    .icon(BitmapDescriptorFactory.fromResource(R.drawable.ic_profile)));

Can someone tell me how to achieve this? 有人可以告诉我如何实现这一目标吗?

Esstentially what is needed is to render the marker icon starting with a customizable layout which will include the drawable(icon) and text as shown. 本质上,需要的是从可定制的布局开始呈现标记图标,该布局将包括所示的drawable(icon)和文本。

Note that the marker still behaves like a marker - has a title/infowindow on click - so that needs to be accommodated when designing your layout. 请注意,标记的行为仍类似于标记-单击时具有标题/信息窗口-因此在设计布局时需要容纳该标记/信息窗口。 So if the title is just duplicating the text then don't set the title. 因此,如果标题只是复制文本,则不要设置标题。

test_marker_icon.xml: test_marker_icon.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="horizontal" android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_centerHorizontal="true"
    android:layout_centerInParent="true"
    android:layout_centerVertical="true">

   <ImageView
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:id="@+id/testiconimage"/>

   <TextView
       android:layout_toRightOf="@+id/testiconimage"
       android:id="@+id/marker_text1"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:textAlignment="viewEnd"
       android:textAppearance="@style/TextAppearance.AppCompat.Medium"
       android:textColor="@color/black"
       tools:text="test" />

   <TextView
       android:id="@+id/marker_text2"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:layout_below="@id/marker_text1"
       android:layout_toRightOf="@+id/testiconimage"
       android:textAppearance="@style/TextAppearance.AppCompat.Medium"
       android:textColor="@color/black"
       android:textStyle="italic"
       tools:text="more text" />

</RelativeLayout>

And the code to render it - in this example two markers are added with same icon and each has unique text: 以及渲染它的代码-在此示例中,两个标记添加了相同的图标,每个标记具有唯一的文本:

    MarkerOptions mo = new MarkerOptions();
    mo.position(new LatLng(latLng.latitude+5, latLng.longitude-.2));


    LayoutInflater inflater = (LayoutInflater) getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View v = inflater.inflate(R.layout.test_marker_icon, null, false);

    ImageView iv = (ImageView) v.findViewById(R.id.testiconimage);
    iv.setImageDrawable(getResources().getDrawable(R.drawable.face));

    TextView tv = (TextView) v.findViewById(R.id.marker_text1);
    tv.setText("Marker 1");

    tv = (TextView) v.findViewById(R.id.marker_text2);
    tv.setText("A really nice place");

    Marker m = mMap.addMarker(mo);
    m.setIcon(BitmapDescriptorFactory.fromBitmap(createDrawableFromView(MapsActivity.this, v)));

    MarkerOptions mo2 = new MarkerOptions();
    mo2.position(new LatLng(latLng.latitude+4.8, latLng.longitude-.2));

    tv = (TextView) v.findViewById(R.id.marker_text1);
    tv.setText("Marker 2");

    tv = (TextView) v.findViewById(R.id.marker_text2);
    tv.setText("Not quite as nice as (1)");

    m = mMap.addMarker(mo2);
    m.setIcon(BitmapDescriptorFactory.fromBitmap(createDrawableFromView(MapsActivity.this, v)));

And the supporting method to create the bitmap: 以及创建位图的支持方法:

// Convert a view to bitmap
public Bitmap createDrawableFromView(Context context, View view) {
    DisplayMetrics displayMetrics = new DisplayMetrics();
    MapsActivity.this.getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
    view.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
    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;
}

And sample output: 并输出示例:

在此处输入图片说明

And output after clicking on marker 2: 在标记2上点击后输出:

在此处输入图片说明

References (nothing is original (except smiley face and demo code)): 参考资料(原始内容(笑脸和演示代码除外)):

  1. Rendering view/bitmap 渲染视图/位图
  2. createDrawableFromView createDrawableFromView

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

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