我正在尝试用一些标记来做地图,这些标记应该有一个带有图像和textview的infolabel。我已经解决了每个infolabel的文本不同但我正在与imageview挣扎。
当我添加一个新标记时,我的应用程序会将新图像放入每个存在的信息窗口中...
这是我的codesnippet,我在其中设置了image-和textview的值:
public class CustomInfoWindowAdapter implements GoogleMap.InfoWindowAdapter {
private Activity context;
public CustomInfoWindowAdapter(Activity context){
this.context = context;
}
@Override
public View getInfoWindow(Marker marker) {
return null;
}
@Override
public View getInfoContents(Marker marker) {
boolean imageGeandert = false;
View view = context.getLayoutInflater().inflate(R.layout.custom_infowindow, null);
TextView tvTitle = (TextView) view.findViewById(R.id.nameTxt);
TextView tvSubTitle = (TextView) view.findViewById(R.id.addressTxt);
tvTitle.setText(marker.getTitle());
tvSubTitle.setText(marker.getSnippet());
ImageView imageView = (ImageView) view.findViewById(R.id.clientPic);
imageView.setImageResource(R.mipmap.logo);
Log.d("Loka2", String.valueOf(MapsActivity.iconFinalFinal2));
return view;
}
}
Log.d输出如下所示:
* 12-19 21:36:14.499 25315-25315 / com.example.yannick.mapdemo D / Lokale位图:android.graphics.Bitmap@9bb0b83
12-19 21:36:14.526 25315-25315 / com.example.yannick.mapdemo D / Loka2:android.graphics.Bitmap@9bb0b83
12-19 21:36:14.672 25315-25315 / com.example.yannick.mapdemo D / Lokale位图:android.graphics.Bitmap@40daa30
12-19 21:36:14.682 25315-25315 / com.example.yannick.mapdemo D / Loka2:android.graphics.Bitmap@40daa30
12-19 21:36:14.844 25315-25315 / com.example.yannick.mapdemo D / Lokale位图:android.graphics.Bitmap@4fa0090
12-19 21:36:14.854 25315-25315 / com.example.yannick.mapdemo D / Loka2:android.graphics.Bitmap@4fa0090
12-19 21:36:14.948 25315-25315 / com.example.yannick.mapdemo D / Loka2:android.graphics.Bitmap@4fa0090
12-19 21:36:15.014 25315-25315 / com.example.yannick.mapdemo D / Loka2:android.graphics.Bitmap@4fa0090
12-19 21:36:15.062 25315-25315 / com.example.yannick.mapdemo D / Loka2:android.graphics.Bitmap@4fa0090 *
最后一个是针对每个现有的infowindow,我不知道为什么......
答案 0 :(得分:0)
您只需将每个标记映射到图像ID,然后使用此地图来确定getInfoContents()
中要使用的图像资源。
首先,将地图定义为实例变量:
Map<Marker, Integer> mMarkerMap = new HashMap<>();
然后每当标记添加到地图时填充此地图:
public void addMarker(LatLng latLng, String title, String snippet, int imageID) {
MarkerOptions markerOptions = new MarkerOptions()
.position(latLng)
.title(title)
.snippet(snippet);
Marker marker = mGoogleMap.addMarker(markerOptions);
mMarkerMap.put(marker, imageID);
}
您可以像这样调用上面的方法:
LatLng latLng = new LatLng(37.7244502,-122.4703867);
String title = "title";
String snippet = "snippet";
addMarker(latLng, title, snippet, R.mipmap.logo);
然后修改InfoWindowAdapter,以便为特定标记的InfoWindow使用相应的资源:
public class CustomInfoWindowAdapter implements GoogleMap.InfoWindowAdapter {
private Activity context;
public CustomInfoWindowAdapter(Activity context){
this.context = context;
}
@Override
public View getInfoWindow(Marker marker) {
return null;
}
@Override
public View getInfoContents(Marker marker) {
boolean imageGeandert = false;
View view = context.getLayoutInflater().inflate(R.layout.custom_infowindow, null);
TextView tvTitle = (TextView) view.findViewById(R.id.nameTxt);
TextView tvSubTitle = (TextView) view.findViewById(R.id.addressTxt);
tvTitle.setText(marker.getTitle());
tvSubTitle.setText(marker.getSnippet());
ImageView imageView = (ImageView) view.findViewById(R.id.clientPic);
//get resource ID and set as image resource:
int resID = mMarkerMap.get(marker);
imageView.setImageResource(resID);
return view;
}
}