Aligning the text in Google Maps Marker snippet

2020-07-21 05:16发布

问题:

I want the string in my snippet to be aligned to the center.

Also, line breakers (\n) in the snippet are converted to spaces, is there a way to insert line breakers?

My relevant code:

GoogleMap map = ...
map.addMarker(new MarkerOptions().position(pos)
        .title("title")
        .snippet("body \n and mind");

Thanks

回答1:

Using some examples, as well as answers from: custom info window adapter with custom data in map v2, I've come across the following solution:

marker.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:orientation="vertical" >

  <TextView
    android:id="@+id/info"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:gravity="center" />

 </RelativeLayout>

Java:

map.setInfoWindowAdapter(new InfoWindowAdapter() {

        @Override
        public View getInfoWindow(Marker arg0) {
            return null;
        }

        @Override
        public View getInfoContents(Marker marker) {

            View v = getLayoutInflater().inflate(R.layout.marker, null);

            TextView info= (TextView) v.findViewById(R.id.info);

            info.setText(marker.getPosition().toString());

            return v;
        }
    });