2013-01-16 138 views
31

那么,我的应用程序中的每个标记都将代表一个用户,因此当我单击信息窗口从Internet获取其数据时,需要标识该用户,并且我可以出于显而易见的原因,不要通过名称来标识它们。是否可以向标记对象添加额外的属性?谢谢!在Google地图v2 api上为标记添加标识android

+0

你是如何添加标记的?作为覆盖? –

+0

您可以使用Marker类的片段字段。 –

+0

我在片段字段中有一个字幕,所以这不是一个选项。你是什​​么意思覆盖?我将它们添加到map.addMarker(... – vdrg

回答

8

是否可以向标记对象添加额外的属性?

No. Markerfinal。另外,您创建的Marker对象快速消失,因为它们仅用于某些IPC而不是Google Play服务应用。您在OnInfoWindowClickListener中获得的Marker对象似乎是重新创建的副本。

我在片段字段中有一个字幕,所以这不是一个选项。

当然可以。将字幕存储在其他地方,并将您的密钥放在字幕中的用户。当您从InfoWindowAdapter呈现InfoWindow时,请拉取字幕。

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; 
} 
} 

你负责,你想传递给信息窗口的任何数据,但你可以在这里看到我传递收视率的哈希值。只是一个原型,绝不是最好的解决方案,但这应该让任何人开始。

+0

保存我的一天..谢谢哥们 – Noman

5

我不认为这是一个好主意,通过地图保持对标记的强引用。由于无论如何,使用自定义窗口适配器来呈现内容,您可以“滥用”MarkerOptions上的片段()或标题()来存储您的信息。它们都是字符串,所以依赖于存储的信息会略微使用更多的内存,另一方面,通过对标记进行强引用,可以避免内存泄漏。

此外,您还可以兼容地图在停止和恢复期间如何管理它的持续性。

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); 
相关问题