简体   繁体   中英

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.

在此处输入图片说明

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.

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:

<?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:

在此处输入图片说明

References (nothing is original (except smiley face and demo code)):

  1. Rendering view/bitmap
  2. createDrawableFromView

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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