【问题标题】:how to add infoWindowAdapters for multiple markers having different values?如何为具有不同值的多个标记添加 infoWindowAdapters?
【发布时间】:2019-10-16 10:30:34
【问题描述】:
Marker drMark=mMap.addMarker(new MarkerOptions().position(drLoc).title("Lahore").icon(BitmapDescriptorFactory.fromResource(R.drawable.marker)));
Marker selfMark=mMap.addMarker(new MarkerOptions().position(curLocation).title("Lahore").icon(BitmapDescriptorFactory.fromResource(R.drawable.passengermarker)));
我正在尝试将 infoWindow 添加到地图活动中的多个标记。但是 infoWindow 适配器在 onMapsReady 中初始化后在所有标记上显示相同的数据。我想在所有带有自己信息的标记上显示 infoWindow,即要单独初始化标记 infoWindows,以便数据变得相关。
【问题讨论】:
标签:
java
android
marker
infowindow
【解决方案1】:
一种方法是使用标记标记来区分 infoWindowAdapter 部分中的标记。我认为这是完成这项工作的一种更简单的方法。
selfMark=mMap.addMarker(new MarkerOptions().position(curLocation).title("Lahore").icon(BitmapDescriptorFactory.fromResource(R.drawable.passengermarker)));
selfMark.setTag("user");
drMark=mMap.addMarker(new MarkerOptions().position(drLoc).title("Lahore").icon(BitmapDescriptorFactory.fromResource(R.drawable.marker)));
drMark.setTag("driver");
InfoWinfoAdapter 的这一部分:
mMap.setInfoWindowAdapter(new GoogleMap.InfoWindowAdapter() {
@Override
public View getInfoWindow(Marker marker) {
return null;
}
@Override
public View getInfoContents(final Marker marker) {
View view = getLayoutInflater().inflate(R.layout.driver_inf, null);
final TextView tvEmail = view.findViewById(R.id.emailDrive);
final TextView tvname = view.findViewById(R.id.nameDrive);
final TextView tvlocality = view.findViewById(R.id.localityDrive);
if(marker.getTag().equals("driver")){
tvEmail.setText("Driver");
}
else if (marker.getTag().equals("user")){
tvEmail.setText("Passenger");
}
tvname.setText(dC.getName());
Geocoder gc = new Geocoder(passengerAct.this, Locale.getDefault());
try {
List<Address> adr = gc.getFromLocation(marker.getPosition().latitude, marker.getPosition().longitude, 1);
Address address = adr.get(0);
tvlocality.setText(address.getLocality() + "," + address.getCountryName());
} catch (IOException e) {
e.printStackTrace();
}
return view;
}
});