我的 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>
最佳答案
自 Marker
是final
,您不能从中创建子类。
因此,我发现的最佳解决方案是使用 Marker
中的内容。 -- 例如代码 fragment -- 保存一个允许您查找其他数据的 key 。例如,它可能是 HashMap
的关键。你的数据模型,或者它可能是你的数据库的主键,或者什么的。
然后,你的 <????>
不来自 Marker
直接,但来自您的真实数据模型,其中 Marker
只是具有将两者联系在一起的识别信息。
关于android - 如何将数据传递到 Google Maps API v2 中的自定义窗口信息?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16107548/