点击地图覆盖层如何显示弹出窗口?

7

我希望在点击我在 Android 上添加到谷歌地图的图层时显示一个自定义图像,并在其中添加一些数据。

有人能指导我如何创建带有数据的自定义图像或对象以在谷歌地图上显示吗?

有人告诉我可以尝试使用自定义视图,但我对它们一无所知。


我不完全理解你想做什么。你可以在地图叠加层上注册一个onclicklistener,然后显示在toast消息中。你也可以在toast消息中添加图片。 如果你能更具体地说明你的问题,我可以给你一些例子。 - Janusz
2个回答

7
这个项目演示了如何在地图上添加弹出面板(与Toast不同,它们是持久的)。此处查看项目。

2
使用以下Java代码创建一个自定义的Toast消息,其中包含图像和一些文本。
LayoutInflater inflater = getLayoutInflater();
View layout = inflater.inflate(R.layout.toast_layout, (ViewGroup) findViewById(R.id.toast_layout_root));

TextView text = (TextView) layout.findViewById(R.id.text);

text.setText(content);
ImageView image = (ImageView) layout.findViewById(R.id.image);
image.setImageBitmap(bmImg);


Toast toast = new Toast(getApplicationContext());
toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0);
toast.setDuration(Toast.LENGTH_LONG);
toast.setView(layout);
toast.show();

同时,还需要处理这个布局文件。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:id="@+id/toast_layout_root"
          android:orientation="horizontal"
          android:layout_width="fill_parent"
          android:layout_height="fill_parent"
          android:padding="10dp"
          android:background="#DAAA"
          >
<ImageView android:id="@+id/image"
           android:layout_width="40dp"
           android:layout_height="40dp"
           android:layout_marginRight="10dp"
           />
<TextView android:id="@+id/text"
          android:layout_width="wrap_content"
          android:layout_height="fill_parent"
          android:textColor="#FFF"
          />
</LinearLayout>

是的,它很有用,但是我可以显示一些停留在屏幕上的东西,而不是Toast吗?Toast会出现然后消失。图片会以Toast的形式出现,但不能让Toast成为图片的形状。 - UMAR-MOBITSOLUTIONS
添加另一个视图(可以是文字或者图片视图)。当你点击它时,可以为其设置setVisibility()。更多信息请参考:http://developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/view/Visibility1.html。 - Praveen
刚刚修复了代码,在“setImageBitmap”之前初始化图像。请确认。 - Siddharth

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接