好吧,我的应用程序上的每个标记都代表一个用户,因此当我点击信息窗口从互联网上获取数据时,我需要识别该用户,并且由于显而易见的原因我无法通过名称识别它们。是否可以向标记对象添加额外属性?谢谢!
答案 0 :(得分:38)
您可以制作HashMap<Marker, User>
查看本教程: http://bon-app-etit.blogspot.be/2012/12/add-informationobject-to-marker-in.html
答案 1 :(得分:8)
是否可以向标记对象添加额外属性?
没有。 Marker
是final
。此外,您创建的Marker
个对象很快就会消失,因为它们仅用于某些IPC而非Google Play Services应用。您在Marker
中获得的OnInfoWindowClickListener
对象似乎是重组后的副本。
我在代码段字段中有一个字幕,所以这不是一个选项。
当然可以。将字幕存储在其他位置,并将您的密钥放在字幕中。当您从InfoWindow
呈现InfoWindowAdapter
时,请提取副标题。
答案 2 :(得分:5)
我认为通过地图保持对标记的强引用是个好主意。由于您仍然使用自定义窗口适配器来呈现内容,因此您可以“滥用”MarkerOptions上的代码段()或title()来存储您的信息。它们都是字符串,因此依赖于存储的信息会略微使用更多内存,另一方面,通过保持对标记的强引用,您可以避免内存泄漏。
另外,您可以与地图在停止和恢复期间管理持久性的方式兼容。
答案 3 :(得分:2)
这是我实施的一个稍微简单的解决方案。你所要做的就是创建一个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;
}
}
您要对要传递到信息窗口的任何数据负责,但您可以在此处看到我传递的是评分哈希值。只是一个原型,绝不是最好的解决方案,但这应该让任何人都开始。
答案 4 :(得分:0)
我正在使用一个额外的类来将一些信息和功能与每个标记相关联。我不认为这是最好的方法,但它是一种选择。特别是如果您想拥有的不仅仅是与每个地图标记相关的信息。这是我用于此的基本结构。
// 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);