我正在关注这个guide ,直到我意识到他直接将标记添加到 map 上,而不是让集群管理器处理它。
我创建了一个实现 InfoWindowAdapter
的 CustomInfo
类。在 getInfoContents(Markermarker)
中,我使用 getTag()
从标记获取数据。问题是我从来没有为我的标记设置标签(我会在哪里?)。
这是我读取位置对象时的代码:
private void generateShelterObjects()
{
Log.i(TAG, "METHOD generateShelterObjects() STARTED");
InputStream is = getResources().openRawResource(R.raw.shelters_csv_file);
BufferedReader reader = new BufferedReader(
new InputStreamReader(is, Charset.forName("UTF-8")));
String line = "";
try {
while ((line = reader.readLine()) != null) {
// Split the line into different tokens (using the comma as a separator).
String[] tokens = line.split(",");
String address = tokens[0];
double latitude = Double.parseDouble(tokens[1]);
double longitude = Double.parseDouble(tokens[2]);
LatLng latLng = new LatLng(latitude, longitude);
String numberOfOccupants = tokens[3];
MarkerOptions markerOptions = new MarkerOptions().position(latLng).title(address).snippet("Antal platser: " + numberOfOccupants);
ShelterObject obj = new ShelterObject(latLng, address, numberOfOccupants);
listMarkers.add(markerOptions);
}
} catch (IOException e1) {
Log.e(TAG, "Error" + line, e1);
e1.printStackTrace();
}
}
为了向 map 添加标记,我在集群管理器中使用了此方法:
private void addClusterItems()
{
for (MarkerOptions markerOptions : listMarkers) {
ShelterObject clusterItem = new ShelterObject(markerOptions.getPosition(), markerOptions.getTitle(), markerOptions.getSnippet());
clusterManager.addItem(clusterItem);
}
}
提前致谢!
最佳答案
检查一下,
private void setUpcustomInfoWindow(){
myMap.setInfoWindowAdapter(new GoogleMap.InfoWindowAdapter() {
@Override
public View getInfoWindow(Marker arg0) {
return null;
}
@Override
public View getInfoContents(Marker marker) {
View myContentView =getActivity().getLayoutInflater().inflate(
R.layout.custom_info_window, null);
TextView tvAddress = ((TextView) myContentView
.findViewById(R.id.tv_address_info_window));
TextView tvAveragePrice = ((TextView) myContentView
.findViewById(R.id.tv_averageprice_info_window));
TextView tvAverageSqft = ((TextView) myContentView
.findViewById(R.id.tv_averagesqft_info_window));
MicroLocality microLocality = (MicroLocality) marker.getTag();
tvAddress.setText(microLocality.getMicroLocality());
tvAveragePrice.setText(getActivity().getString(R.string.Rs)+" "+microLocality.getPricerange());
tvAverageSqft.setText(getActivity().getString(R.string.Rs)+" "+microLocality.getPricePerSqft());
return myContentView;
}
});
}
关于java - 使用集群管理器时为标记创建自定义信息窗口?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56077176/