好吧,我的应用程序上的每个标记都将代表一个用户,因此当我单击信息窗口以从 Internet 获取其数据时,我需要识别该用户,并且由于显而易见的原因,我无法通过名称识别他们。是否可以向标记对象添加额外的属性?谢谢!
5 回答
你可以做一个HashMap<Marker, User>
检查本教程: http ://bon-app-etit.blogspot.be/2012/12/add-informationobject-to-marker-in.html
是否可以向标记对象添加额外的属性?
不是。Marker
_ final
此外,Marker
您创建的对象很快就会消失,因为它们仅用于某些 IPC 到 Google Play 服务应用程序。Marker
你得到的对象OnInfoWindowClickListener
似乎是一个重组的副本。
我在片段字段中有一个字幕,所以这不是一个选项。
是的。将字幕存储在其他地方,并将您的用户密钥放在字幕中。当您InfoWindow
从中渲染时InfoWindowAdapter
,拉入字幕。
我认为通过地图保持对标记的强烈引用不是一个好主意。由于您无论如何都使用自定义窗口适配器来呈现内容,因此您可以“滥用” MarkerOptions 上的 snippet() 或 title() 来存储您的信息。它们都是字符串,因此取决于要存储的信息,您会稍微使用更多内存,另一方面,通过持有对标记的强引用,您可以避免内存泄漏。
此外,您还可以兼容 Maps 在停止和恢复期间管理其持久性的方式。
这是我实施的一个稍微简单的解决方案。您所做的只是创建一个 InfoWindowAdapter,它将您想要在其构造函数中传递给窗口的内容。
class CustomWindowAdapter implements InfoWindowAdapter{
LayoutInflater mInflater;
private HashMap<Marker, Double> mRatingHash;
public CustomWindowAdapter(LayoutInflater i, HashMap<Marker, Double> h){
mInflater = i;
mRatingHash = h;
}
@Override
public View getInfoContents(Marker marker) {
// Getting view from the layout file
View v = mInflater.inflate(R.layout.custom_info_window, null);
TextView title = (TextView) v.findViewById(R.id.tv_info_window_title);
title.setText(marker.getTitle());
TextView description = (TextView) v.findViewById(R.id.tv_info_window_description);
description.setText(marker.getSnippet());
RatingBar rating = (RatingBar) v.findViewById(R.id.rv_info_window);
Double ratingValue = mRatingHash.get(marker);
rating.setRating(ratingValue.floatValue());
return v;
}
@Override
public View getInfoWindow(Marker marker) {
// TODO Auto-generated method stub
return null;
}
}
您对要传递到信息窗口的任何数据负责,但您可以在此处看到我正在传递评级的哈希值。只是一个原型,绝不是最好的解决方案,但这应该让任何人开始。
我正在使用一个附加类将一些信息和功能与每个标记相关联。我不认为这是最好的方法,但它是一种选择。特别是如果您想要的不仅仅是与每个地图标记相关联的信息。这是我用于此的基本结构。
// Make an array list of for all of your things
ArrayList<Thing> things;
class Thing {
long thing_key;
String thing_string;
int thingRadius;
Double coord_long;
Double coord_lat;
Marker marker;
}
// Then to use this to start your list.
things = new ArrayList<>();
// Create the thing object and save all the data
thing = new Thing();
thing.marker = thingMarker;
thing.thing_key = thing_key;
thing.thing_string = thing_string;
thing.radius = Integer.getInteger(thingRadius.getText().toString());
// Save the thing to the thing ArrayList
things.add(thing);