【问题标题】:How to offset the balloon view in a marker in Google Map V2?如何在 Google Map V2 的标记中偏移气球视图?
【发布时间】:2013-02-14 15:45:26
【问题描述】:

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

我的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>

【问题讨论】:

  • 您可以添加屏幕截图以便我们看到错误吗?我建议你 1-return null;getInfoContents 内 2-在 getInfoWindow 内膨胀视图。不要重复使用。
  • 它没有任何错误。主要问题是标记和气球之间的距离,当我有很多 poi 时,用户不知道点击了哪个 poi。所以,如果很难修复它,最好离开它。

标签: android android-maps-v2


【解决方案1】:

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

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

【讨论】:

  • 你怎么说标记和气球之间的默认距离是锚方法的最小距离,所以我不能用这种方法进一步减少这个距离,现在,它们之间的当前距离是有点大。
  • Anchor 允许您将标记放置在气球布局中的任何位置,从左上角的 anchor(0.0,0.0) 到右下角的 anchor(1.0,1.0)。如果默认的 anchor(0.5,1.0) 即底部居中,与您的图形不对齐,则将其更改为 anchor(0.5,0.9) 以将标记向上移动 10% 的布局高度。
【解决方案2】:

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

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

    android:layout_marginBottom="-8dp" >

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

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

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

【讨论】:

  • 是的,我试过了。但是气球和标记之间的距离没有改变。它使布局,包含在气球中,向下移位。而且不是很好,因为气球箭头在布局中消失了。
  • 好的,那么下一步。在从getInfoWindow() 返回之前,将类似LayoutParams parentParams = ((View)mWindow.getParent()).getLayoutParams() 的内容发布到ui 处理程序(onCreate() 中的new Handler() 是一个作为ui 线程处理程序的活动),在runnable 中设置一个断点。这样,您将看到是否可以移动信息窗口的父级。 (这只是我的想法,它可能不起作用。发布到处理程序的技巧是在将信息窗口添加到视图层次结构后执行可运行对象)干杯,让我知道它是怎么回事
  • 它没有父级,因为当我膨胀视图时,第二个参数为空。所以,当我执行 getParent 方法时返回 null。
  • 即使您将其发布到 getInfoWindow() 中的 ui 处理程序?看,在你从 getInfoWindow() 返回它之后,它会被插入到视图层次结构中——之后它确实有一个父级。诀窍是在父母在那里时抓住它:)
  • 我已经做到了。 p 和 v 为空: public View getInfoWindow(Marker marker) { render(marker, mWindow);处理程序处理程序 = 新处理程序();线程 t = new Thread(new Runnable() { @Override public void run() { ViewParent p = mWindow.getParent(); View v = ((View)mWindow.getParent()); LayoutParams l = v.getLayoutParams() ; LayoutParams parentParams = ((View)mWindow.getParent()).getLayoutParams(); Log.i("INFO", parentParams.toString()); } }); handler.postDelayed(t, 3000);返回窗口; }
【解决方案3】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-11-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-11-02
    • 2014-08-08
    • 1970-01-01
    相关资源
    最近更新 更多