如何在 Android 的自定义 InfoWindow 中将不仅仅是标题和片段传递给 getInfoContents
How to pass more than just title and snippet to getInfoContents in custom InfoWindow for Android
我的 Android 应用程序中有一个 InfoWindowAdapter class,它引用包含三个 TextView 的 xml 布局。我在 addMarker()
方法中使用以下代码添加了一个新标记:
mapView.addMarker(new MarkerOptions()
.position(latLon)
.title(titleText)
.snippet(snippetText)
.icon(BitmapDescriptorFactory.fromResource(R.drawable.pin_green)));
mapView.setInfoWindowAdapter(new InfoWindow(getLayoutInflater()));
mapView.setOnInfoWindowClickListener(this);
然后我在 getInfoContents()
方法中使用 marker.getTitle()
和 marker.getSnippet()
设置我的两个信息窗口文本视图的文本:
@Override
public View getInfoContents(Marker marker) {
if (popup == null) {
popup = inflater.inflate(R.layout.infowindow_popup, null);
}
TextView tvTitle = (TextView) popup.findViewById(R.id.title);
tvTitle.setText(marker.getTitle());
TextView tvSnippet = (TextView) popup.findViewById(R.id.snippet) ;
tvSnippet.setText(marker.getSnippet());
TextView tvSnippet2 = (TextView) popup.findViewById(R.id.snippet_2) ;
tvSnippet2.setText("test");
return popup;
}
这对于前两个文本视图来说都很好,但我想知道的是我将第三个字符串传递给 infoWindowContents()
以与 tvSnippet2
一起使用的正确方法是什么?显然我不能使用 .title()
/.snippet()
和 marker.getTitle()
/marker.getSnippet()
因为它们已经被使用并且会重复数据。
信息窗口适配器:
public class InfoWindow implements InfoWindowAdapter {
private View popup = null;
private LayoutInflater inflater = null;
String str;
InfoWindow(LayoutInflater inflater, String s) {
this.inflater = inflater;
str = s;
}
@Override
public View getInfoContents(Marker marker) {
if (popup == null) {
popup = inflater.inflate(R.layout.infowindow_popup, null);
}
TextView tvTitle = (TextView) popup.findViewById(R.id.title);
tvTitle.setText(marker.getTitle());
TextView tvSnippet = (TextView) popup.findViewById(R.id.snippet) ;
tvSnippet.setText(marker.getSnippet());
TextView tvSnippet2 = (TextView) popup.findViewById(R.id.snippet_2) ;
tvSnippet2.setText(str);
return popup;
}
@Override
public View getInfoWindow(Marker marker) {
return null;
}
}
当你在 Map
中添加 Marker
时 concat your all Strings
in your Title 字符串如
String title="First String"+"_"+"Second String";
然后将其添加到您的 title
all = mMap.addMarker(new MarkerOptions()
.icon(BitmapDescriptorFactory
.fromResource(R.drawable.mark_red))
.position(Location)
.title(title)
.snippet(snippet);
现在,当您点击任何 Marker
然后您的 CustomInfoWindow
上升 up.So 您将在 getInfoContents(...)
中解析您的标题,例如
@Override
public View getInfoContents(Marker marker) {
if (popup == null) {
popup = inflater.inflate(R.layout.infowindow_popup, null);
}
String str=marker.getTitle();
final String[] str2=str.split("_");
TextView tvTitle = (TextView) popup.findViewById(R.id.title);
tvTitle.setText(str2[0]);// got first string as title
TextView tvSnippet = (TextView) popup.findViewById(R.id.snippet) ;
tvSnippet.setText(marker.getSnippet());
TextView tvSnippet2 = (TextView) popup.findViewById(R.id.snippet_2) ;
tvSnippet2.setText(str2[1]);// got second string
return popup;
}
有用于额外数据的标签之类的东西
Tag
An Object associated with the marker. For example, the Object can contain data about what the marker represents. This is easier than storing a separate Map<Marker, Object>. As another example, you can associate a String ID corresponding to the ID from a data set. Google Maps Android API neither reads nor writes this property except that when a marker is removed from the map, this property is set to null.
//creating marker
EventEntity entity; // custom object full of data
Marker m = map.addMarker(new MarkerOptions().position(
new LatLng(entity.getLocation().getLat(), entity.getLocation().getLng()))
.title(entity.getEventName())
.icon(BitmapDescriptorFactory.fromResource(R.drawable.map_marker_copy)));
m.setTag(entity); // here we set our custom data as tag
@Override public View getInfoContents(Marker marker) {
EventEntity event = (EventEntity) marker.getTag();
// and here we got EventEntity back so we can get all required data
View v = LayoutInflater.from(getApplicationContext())
.inflate(R.layout.infowindow_map, mapview, false);
MyTextView tvTitle = (MyTextView) v.findViewById(R.id.tv_title);
MyTextView tvDate = (MyTextView) v.findViewById(R.id.tv_date);
MyTextView tvType = (MyTextView) v.findViewById(R.id.tv_type);
MyTextView tvPrice = (MyTextView) v.findViewById(R.id.tv_price);
tvTitle.setText(event.getEventName());
tvType.setText(event.getType() + " event");
tvPrice.setText(event.getPrice() + event.getCurrency());
String date = dfDate.format(new Date(event.getDateStart()));
String start = dfTime.format(new Date(event.getDateStart()));
String end = dfTime.format(new Date(event.getDateEnd()));
tvDate.setText(date + " " + start + "-" + end);
return v;
}
我的 Android 应用程序中有一个 InfoWindowAdapter class,它引用包含三个 TextView 的 xml 布局。我在 addMarker()
方法中使用以下代码添加了一个新标记:
mapView.addMarker(new MarkerOptions()
.position(latLon)
.title(titleText)
.snippet(snippetText)
.icon(BitmapDescriptorFactory.fromResource(R.drawable.pin_green)));
mapView.setInfoWindowAdapter(new InfoWindow(getLayoutInflater()));
mapView.setOnInfoWindowClickListener(this);
然后我在 getInfoContents()
方法中使用 marker.getTitle()
和 marker.getSnippet()
设置我的两个信息窗口文本视图的文本:
@Override
public View getInfoContents(Marker marker) {
if (popup == null) {
popup = inflater.inflate(R.layout.infowindow_popup, null);
}
TextView tvTitle = (TextView) popup.findViewById(R.id.title);
tvTitle.setText(marker.getTitle());
TextView tvSnippet = (TextView) popup.findViewById(R.id.snippet) ;
tvSnippet.setText(marker.getSnippet());
TextView tvSnippet2 = (TextView) popup.findViewById(R.id.snippet_2) ;
tvSnippet2.setText("test");
return popup;
}
这对于前两个文本视图来说都很好,但我想知道的是我将第三个字符串传递给 infoWindowContents()
以与 tvSnippet2
一起使用的正确方法是什么?显然我不能使用 .title()
/.snippet()
和 marker.getTitle()
/marker.getSnippet()
因为它们已经被使用并且会重复数据。
信息窗口适配器:
public class InfoWindow implements InfoWindowAdapter {
private View popup = null;
private LayoutInflater inflater = null;
String str;
InfoWindow(LayoutInflater inflater, String s) {
this.inflater = inflater;
str = s;
}
@Override
public View getInfoContents(Marker marker) {
if (popup == null) {
popup = inflater.inflate(R.layout.infowindow_popup, null);
}
TextView tvTitle = (TextView) popup.findViewById(R.id.title);
tvTitle.setText(marker.getTitle());
TextView tvSnippet = (TextView) popup.findViewById(R.id.snippet) ;
tvSnippet.setText(marker.getSnippet());
TextView tvSnippet2 = (TextView) popup.findViewById(R.id.snippet_2) ;
tvSnippet2.setText(str);
return popup;
}
@Override
public View getInfoWindow(Marker marker) {
return null;
}
}
当你在 Map
中添加 Marker
时 concat your all Strings
in your Title 字符串如
String title="First String"+"_"+"Second String";
然后将其添加到您的 title
all = mMap.addMarker(new MarkerOptions()
.icon(BitmapDescriptorFactory
.fromResource(R.drawable.mark_red))
.position(Location)
.title(title)
.snippet(snippet);
现在,当您点击任何 Marker
然后您的 CustomInfoWindow
上升 up.So 您将在 getInfoContents(...)
中解析您的标题,例如
@Override
public View getInfoContents(Marker marker) {
if (popup == null) {
popup = inflater.inflate(R.layout.infowindow_popup, null);
}
String str=marker.getTitle();
final String[] str2=str.split("_");
TextView tvTitle = (TextView) popup.findViewById(R.id.title);
tvTitle.setText(str2[0]);// got first string as title
TextView tvSnippet = (TextView) popup.findViewById(R.id.snippet) ;
tvSnippet.setText(marker.getSnippet());
TextView tvSnippet2 = (TextView) popup.findViewById(R.id.snippet_2) ;
tvSnippet2.setText(str2[1]);// got second string
return popup;
}
有用于额外数据的标签之类的东西
Tag
An Object associated with the marker. For example, the Object can contain data about what the marker represents. This is easier than storing a separate Map<Marker, Object>. As another example, you can associate a String ID corresponding to the ID from a data set. Google Maps Android API neither reads nor writes this property except that when a marker is removed from the map, this property is set to null.
//creating marker
EventEntity entity; // custom object full of data
Marker m = map.addMarker(new MarkerOptions().position(
new LatLng(entity.getLocation().getLat(), entity.getLocation().getLng()))
.title(entity.getEventName())
.icon(BitmapDescriptorFactory.fromResource(R.drawable.map_marker_copy)));
m.setTag(entity); // here we set our custom data as tag
@Override public View getInfoContents(Marker marker) {
EventEntity event = (EventEntity) marker.getTag();
// and here we got EventEntity back so we can get all required data
View v = LayoutInflater.from(getApplicationContext())
.inflate(R.layout.infowindow_map, mapview, false);
MyTextView tvTitle = (MyTextView) v.findViewById(R.id.tv_title);
MyTextView tvDate = (MyTextView) v.findViewById(R.id.tv_date);
MyTextView tvType = (MyTextView) v.findViewById(R.id.tv_type);
MyTextView tvPrice = (MyTextView) v.findViewById(R.id.tv_price);
tvTitle.setText(event.getEventName());
tvType.setText(event.getType() + " event");
tvPrice.setText(event.getPrice() + event.getCurrency());
String date = dfDate.format(new Date(event.getDateStart()));
String start = dfTime.format(new Date(event.getDateStart()));
String end = dfTime.format(new Date(event.getDateEnd()));
tvDate.setText(date + " " + start + "-" + end);
return v;
}