【发布时间】:2015-07-28 09:06:01
【问题描述】:
事情是这样的: 我有一张带有标记的地图(由 ClusterManager 管理),每次单击它们时都会显示一个信息窗口。我还在他们被点击的时候获取他们的地址,所以一旦我得到它,我会再次在标记上调用 showInfoWindow() 以更新它。问题是地址不会显示在信息窗口中。这是我的 InfoWindowAdapter(根据单击的标记类型,我有两个视图):
class CabinetInfoWindowAdapter implements GoogleMap.InfoWindowAdapter {
public CabinetInfoWindowAdapter () {
}
@Override
public View getInfoWindow(Marker marker) {
// Use the default info window frame
return null;
}
@Override
public View getInfoContents(Marker marker) {
Cabinet cabinet = clickedCabinetMarker.getCabinet();
Log.i("fttxgr", "info contents, cabinet type: " + cabinet.getType().toString());
switch (cabinet.getType()) {
case ADSL:
case VDSL:
return getCabinetView(cabinet, marker);
case DSLAM:
return getDslamView(cabinet, marker);
default: return null;
}
}
private final View getCabinetView (final Cabinet cabinet, final Marker marker) {
View view = getLayoutInflater().inflate(R.layout.cabinet_info_window, null);
TextView cabinetId = (TextView) view.findViewById(R.id.cabinet_id);
TextView cabinetType = (TextView) view.findViewById(R.id.cabinet_type);
TextView cabinetAddress = (TextView) view.findViewById(R.id.cabinet_address);
TextView cabinetCoordinates = (TextView) view.findViewById(R.id.cabinet_coordinates);
TextView cabinetUserNick = (TextView) view.findViewById(R.id.cabinet_user_nick);
ImageView cabinetImage = (ImageView) view.findViewById(R.id.cabinet_image);
View header = view.findViewById(R.id.header);
cabinetId.setText (cabinet.getId() + " - " + cabinet.getCabinetNumber());
cabinetType.setText (cabinet.getType().toString());
switch (cabinet.getType()) {
case ADSL:
header.setBackgroundColor(getResources().getColor(R.color.adsl_red));
break;
case VDSL:
header.setBackgroundColor(getResources().getColor(R.color.vdsl_green));
break;
}
cabinetCoordinates.setText(cabinet.getCoordinates().toString());
if (cabinet.getImage() == null) {
loadCabinetImage(marker);
} else {
cabinetImage.setImageBitmap(cabinet.getImage());
}
if (cabinet.getUserNick() == null) {
loadCabinetUserNick(cabinet, cabinet.getUserId(), cabinet.getmUserSite(), marker);
} else {
cabinetUserNick.setText("Added by user: " + cabinet.getUserNick());
}
if (cabinet.getAddress() == null) {
loadCabinetAddress(cabinet.getCoordinates().latitude,
cabinet.getCoordinates().longitude, marker);
} else {
cabinetAddress.setText("Address: " + cabinet.getAddress());
}
return view;
}
private final View getDslamView (final Cabinet cabinet, final Marker marker) {
View view = getLayoutInflater().inflate(R.layout.dslam_info_window, null);
TextView dslamId = (TextView) view.findViewById(R.id.dslam_id);
TextView dslamAddress = (TextView) view.findViewById (R.id.dslam_address);
TextView dslamCoordinates = (TextView) view.findViewById (R.id.dslam_coordinates);
dslamId.setText(cabinet.getId() + " - " + cabinet.getCabinetNumber());
dslamCoordinates.setText(cabinet.getCoordinates().toString());
if (cabinet.getAddress() == null) {
Log.i("fttxgr", "address is null");
loadCabinetAddress(cabinet.getCoordinates().latitude,
cabinet.getCoordinates().longitude, marker);
} else {
Log.i("fttxgr", "address is there: " + cabinet.getAddress());
dslamAddress.setText("Address: " + cabinet.getAddress());
}
return view;
}
}
还有 loadCabinetAddress() 函数:
private void loadCabinetAddress (final double lat, final double lng, final Marker marker) {
Geocoder geocoder;
List<Address> addresses;
geocoder = new Geocoder(this, Locale.getDefault());
try {
addresses = geocoder.getFromLocation(lat, lng, 1);
String address = addresses.get(0).getAddressLine(0);
String city = addresses.get(0).getLocality();
String country = addresses.get(0).getCountryName();
String postalCode = addresses.get(0).getPostalCode();
String concat = "" +
((address != null) ? address : "") + " - " +
((city != null) ? city : "") + " - " +
((country != null) ? country : "") + " - " +
((postalCode != null) ? postalCode : "");
clickedCabinetMarker.getCabinet().setAddress(concat);
Log.i("fttxgr", "address loaded, showing info window again");
marker.showInfoWindow();
} catch (IOException e) {
e.printStackTrace();
}
}
所有日志都表明一切都按预期进行。那么为什么信息窗口没有正确更新呢?
附:有趣的是,对于第一个视图(由 getCabinetView() 返回的那个),更新是有效的!但是我也异步加载了一张图片和一个用户昵称,我调用了 3 次 showInfoWindow() 来更新它们。
【问题讨论】:
标签: android google-maps infowindow