2013-01-16 140 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); 
相關問題