【问题标题】:Custom info window for google maps android谷歌地图安卓的自定义信息窗口
【发布时间】:2014-08-28 10:19:15
【问题描述】:

我希望在marker 点击时出现custom info window

我希望它是这样的:

ImageView

TextView

TextView

TextView

Button  Button

我看到了其他人的帖子问题,但这仍然令人困惑。

【问题讨论】:

  • 对于一个 InfoWindows 来说不是实时视图,因此您不能在 InfoWindow 中放置一个按钮并期望监听点击事件
  • 无论如何我都可以拥有它,以便如果不通过信息窗口,单击标记将允许我单击按钮
  • 在标记点击时打开一个对话框

标签: android google-maps google-maps-markers android-button infowindow


【解决方案1】:

关于ImageView和TextView,试试这个:

设置信息窗口布局:infowindowlayout.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="#FFFFFF" >

<ImageView
    android:id="@+id/imageView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />

<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="TextView" />

<TextView
    android:id="@+id/textView2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="TextView" />

</LinearLayout>

在您的 MapActivity 中(这是一个标记的示例,但您也可以将其用于其他标记):

public class MapActivity extends Activity  {
private GoogleMap mMap;
private Marker Somewhere;
private int markerclicked;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_map);
...
...
final LatLng somewhere = new LatLng(..., ...);

Somewhere=mMap.addMarker(new MarkerOptions()
.position(somewhere)
.title("YOUR TITLE")
.snippet("INFO")
.icon(BitmapDescriptorFactory.fromResource(R.drawable.pin)));

...
...
...

mMap.setInfoWindowAdapter(new InfoWindowAdapter() {

        // Use default InfoWindow frame
        @Override
        public View getInfoWindow(Marker arg0) {
            return null;
        }

        // Defines the contents of the InfoWindow
        @Override
        public View getInfoContents(Marker arg0) {

            // Getting view from the layout file infowindowlayout.xml
            View v = getLayoutInflater().inflate(R.layout.infowindowlayout, null);

            LatLng latLng = arg0.getPosition();

            ImageView im = (ImageView) v.findViewById(R.id.imageView1);
            TextView tv1 = (TextView) v.findViewById(R.id.textView1);
            TextView tv2 = (TextView) v.findViewById(R.id.textView2);
            String title=arg0.getTitle();
            String informations=arg0.getSnippet();

            tv1.setText(title);
            tv2.setText(informations);

            if(onMarkerClick(arg0)==true && markerclicked==1){
               im.setImageResource(R.drawable.yourdrawable);
              }


            return v;

        }
    });
}

public boolean onMarkerClick(final Marker marker) {

if (marker.equals(Somewhere)) 
  {
      markerclicked=1;
      return true;
  }
return false; 
}

关于Buttons、ImageButtons等没有办法。 documentation 说:

如前面关于信息窗口的部分所述,信息窗口不是实时视图,而是将视图呈现为地图上的图像。因此,您在视图上设置的任何侦听器都将被忽略,并且您无法区分视图各个部分的点击事件。建议您不要在自定义信息窗口中放置交互式组件(例如按钮、复选框或文本输入)。

不管怎样,好像有解决办法,见this answer here。我不知道它是否适用于我的代码,但它似乎适用于按钮和图像按钮。

【讨论】:

  • 这在大部分情况下都有效,但图像没有显示
  • 非常感谢您提供有关预渲染场景的信息...我试图让其中的一些按钮运行,但似乎 Google 似乎在最后没有对此进行任何更改距您回答此问题已有 6 年了。
猜你喜欢
  • 1970-01-01
  • 2013-05-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-05-29
  • 2019-06-03
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多