android - 如何将数据传递到 Google Maps API v2 中的自定义窗口信息?

标签 android google-maps-markers google-maps-android-api-2

我的 map 窗口信息有一个自定义布局,但我看不到任何有关如何从标记传递数据的示例。

有人可以帮忙吗?

mMap.setInfoWindowAdapter(new InfoWindowAdapter() {

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

    @Override
    public View getInfoContents(Marker arg0) {

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

        ImageView pic = (ImageView) v.findViewById(R.id.pic); 
        String url = <????>;
        // set pic image from url

        TextView name = (TextView) v.findViewById(R.id.name); 
        name.setText(<????>);

        TextView address = (TextView) v.findViewById(R.id.address); 
        address.setText(<????>);

        return v;

    }
});

mCenterList = MyApplication.dbHelper.getAllCenter();

for(Center center : mCenterList){

    Marker marker = mMap.addMarker(
            new MarkerOptions().position(new LatLng(center.lat, center.lng))
            .icon(BitmapDescriptorFactory.fromResource(R.drawable.elipin)));                

    // How can I pass custom data, here pic, name and address, to the marker, to
    // make it available in getInfoContents?


}

info_window_layout.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="300dp"
    android:layout_height="50dp"
    android:padding="5dp" >

    <ImageView
        android:id="@+id/pic"
        android:layout_width="40dp"
        android:layout_height="40dp"
        android:layout_marginRight="5dp"
        android:scaleType="centerCrop" />

    <ImageView
        android:id="@+id/arrow"
        android:layout_width="40dp"
        android:layout_height="40dp"
        android:layout_alignParentRight="true"
        android:layout_marginLeft="5dp"
        android:scaleType="centerCrop"
        android:src="@drawable/arrowri" />

    <TextView
        android:id="@+id/name"
        android:layout_width="match_parent"
        android:layout_height="25dp"
        android:layout_toRightOf="@id/pic"
        android:layout_toLeftOf="@id/arrow"
        android:gravity="center_vertical"
        android:singleLine="true"
        android:textColor="#ffffff"
        android:textSize="18sp" />

    <TextView
        android:id="@+id/address"
        android:layout_width="match_parent"
        android:layout_height="15dp"
        android:layout_toRightOf="@id/pic"
        android:layout_toLeftOf="@id/arrow"
        android:layout_below="@id/name"
        android:gravity="center_vertical"
        android:singleLine="true"
        android:textColor="#ffffff"
        android:textSize="16sp" />

</RelativeLayout>

最佳答案

Markerfinal ,您不能从中创建子类。

因此,我发现的最佳解决方案是使用 Marker 中的内容。 -- 例如代码 fragment -- 保存一个允许您查找其他数据的 key 。例如,它可能是 HashMap 的关键。你的数据模型,或者它可能是你的数据库的主键,或者什么的。

然后,你的 <????>不来自 Marker直接,但来自您的真实数据模型,其中 Marker只是具有将两者联系在一起的识别信息。

关于android - 如何将数据传递到 Google Maps API v2 中的自定义窗口信息?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16107548/

相关文章:

android - AppNotIdleException 由于 AnimatedVectorDrawableCompat

android - 如何在所有设备上渲染 ♦ (Unicode Black Diamond Suit)?

jquery - 更改谷歌地图位置 onclick

android - 错误包 `com.google.android.gms...` 不存在

java - Android 如何获取当前的系统设置值?

android - LinearLayout 中的 ConstraintLayout 不起作用

google-maps - 如何将商家/"default"位置标记和 infoWindow 添加到 Google map (v3)

android - 谷歌地图航向标记或指示器

android - 从 Google Map 的 setOnInfoWindowClickListener 接口(interface)方法替换 fragment 时 UI 挂起

android - 如何使用受限 API key 的 Android 应用程序使用 Google Geolocation API