6

当我单击标记时,会出现一个气球。但是标记和气球之间的空间太大,那么我怎样才能减少这个距离呢?这就像setBalloonBottomOffset使用V1 Google Map.

custom balloon的是这个类:

public class CustomInfoWindowAdapter implements InfoWindowAdapter {

    private final View mWindow;
    private final View mContents;

    public CustomInfoWindowAdapter(Activity activity) {
        LayoutInflater inflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        mWindow = inflater.inflate(R.layout.ballon, null);
        mContents = inflater.inflate(R.layout.ballon, null);
    }

    @Override
    public View getInfoWindow(Marker marker) {
        render(marker, mWindow);
        return mWindow;
    }

    @Override
    public View getInfoContents(Marker marker) {
        render(marker, mContents);
        return mContents;
    }

    private void render(Marker marker, View view) {
        String title = marker.getTitle();
        TextView titleUi = ((TextView) view.findViewById(R.id.txtTitle));
        if (title != null) {
            titleUi.setText(title);
        } else {
            titleUi.setText("");
        }

        String snippet = marker.getSnippet();
        TextView snippetUi = ((TextView) view.findViewById(R.id.txtPlace));
        if (snippet != null) {
            snippetUi.setText(snippet);
        } else {
            snippetUi.setText("");
        }
    }
}

我显示一个标记

marker.showInfoWindow();

我的气球 xml 是

<RelativeLayout
    android:layout_width="250dp"
    android:layout_height="75dp"
    android:background="@drawable/ballon" >

    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >

        <LinearLayout
            android:id="@+id/linearLayout1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:orientation="vertical" >

            <TextView
                android:id="@+id/txtTitle"
                style="@style/Ballon_Title"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:ellipsize="end"
                android:paddingLeft="15dp"
                android:paddingRight="50dp"
                android:paddingTop="10dp"
                android:singleLine="true"
                android:text="Con mis amigos amigos" />

            <TextView
                android:id="@+id/txtPlace"
                style="@style/Ballon_Place"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:ellipsize="end"
                android:paddingBottom="20dp"
                android:paddingLeft="15dp"
                android:paddingRight="50dp"
                android:singleLine="true"
                android:text="Puerta del sol" />
        </LinearLayout>
    </RelativeLayout>

    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >
    </RelativeLayout>

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:paddingRight="12dp"
        android:paddingTop="18dp"
        android:src="@drawable/icon_arrow" />
</RelativeLayout>

4

3 回答 3

2

默认情况下,地图标记“锚定”在布局底部的中间(即锚点(0.5,1))。

您可以在创建标记时使用MarkerOptions.anchor更改锚点。

于 2013-02-24T00:40:13.663 回答
0

您是否尝试过为整个气球布局使用负下边距?像这样:

<RelativeLayout
    android:layout_width="250dp"
    android:layout_height="75dp"
    android:background="@drawable/ballon"

    android:layout_marginBottom="-8dp" >

可能不会起作用或导致内容在底部被切断,但作为最简单的方法仍然值得尝试。也许还有另一种欺骗GoogleMap类的方法,但很难弄清楚没有 API 的源代码。

另一个提示:您可能不需要您的getInfoContents()方法 - 只需返回null那里即可。null仅当您从getInfoWindow()参见此处)返回时才会调用此方法- 将您的内容视图合并到默认信息窗口中,这显然不是您的目标,因为您想四处移动气球。因此,您也可以丢弃mContents,它完全是多余的,只是占用内存以获取永远不会被使用的膨胀视图层次结构的另一个副本。

告诉我保证金是否适合你。我们可能还可以尝试((View)mWindow.getParent())在显示项目时稍后使用类似的东西,但这会更棘手,所以我建议先尝试边距。

于 2013-02-22T11:15:43.867 回答
0

这是一个老问题,当前的 API 有一个MarkerOptions.infoWindowAnchor方法可能会有所帮助。

于 2020-09-09T09:37:00.563 回答