2011-05-30 22 views
2

我一直在搜索有關擴展ItemizedOverlay和OverlayItem的正確方法的詳細信息,但得到的結果還不滿足我。 簡而言之,我需要在地圖上顯示與多個位置相關的不同類型的標記;要顯示的標記的類型由位置本身的特定屬性決定。我還需要在選擇標記時顯示某個標記,而在標記未選中或未聚焦時顯示另一個標記。 根據我的理解,這是我寫的:創建並在主MapActivity填充MyOverlayItem的GoogleMaps:自定義ItemizedOverlay和OverlayItem,顯示不同標記的正確方法

public class MyItemizedOverlay extends ItemizedOverlay<MyOverlayItem> { 

ArrayList<MyOverlayItem> items; 
//... 

public MyItemizedOverlay(Drawable drawable, /*...*/) { 
    super(boundCenterBottom(drawable)); 
    //... 
    populate(); 
} 

public void addOverlay(MyOverlayItem overlay) { 
    return this.items.add(overlay); 
    populate(); 

@Override 
protected MyOverlayItem createItem(int index) { 
    return this.items.get(index); 
} 

@Override 
public int size() { 
    return this.items.size(); 
}} 

    public class MyOverlayItem extends OverlayItem { 

//... 

public Drawable getMarker(int stateBitset) { 
    Drawable drawable; 
    try { 
     if (stateBitset == 0) { 
      if (this.property.Equals("ON")) { 
       drawable = this.context.getResources().getDrawable(R.drawable.on); 
       drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight()); 
       return drawable; 
      } else if (this.property.Equals("OFF")) { 
       drawable = this.context.getResources().getDrawable(R.drawable.off); 
       drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight()); 
       return drawable; 
      } else { 
       drawable = this.context.getResources().getDrawable(R.drawable.generic); 
       drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight()); 
       return drawable; 
      } 
     } else { 
      //same thing as above, just with different drawables. 
     } 
    } catch (Exception e) { 
     //... 
    } 

    return null; 
} 

MyItemizedOverlay。現在: 正確放置的唯一標記是默認標記。也就是說,那些可以繪製傳遞給MyItemizedOverlay的構造函數的元素,並且使用boundCenterBottom(將圖像的中間底點設置爲指向地圖中的指定點)進行設置。當MyOverlayItem類中的getMarker方法不返回null,並且使用另一個標記時,圖像顯示錯誤(例如,陰影不跟隨圖像!)。在getMarker方法中,您必須爲drawable定義一個邊界框(帶drawable.setBounds);但是我認爲,通過這種方式,您並未指定在地圖上必須放置的點。

因此,問題是:如何指定一個從OverlayItem對象的getMarker方法返回給ItemizedOverlay的標記drawable,用於放置在映射中的邊界(與MyItemizedOverlay中的boundCenterBottom一樣爲默認drawable)?什麼是最好的方法來做到這一點?

;)

回答

12

好了,經過反覆的測試中,它似乎是這樣的:

drawable.setBounds(-drawable.getIntrinsicWidth()/2, -drawable.getIntrinsicHeight(), drawable.getIntrinsicWidth() /2, 0); 

放在OverlayItem的getMarker方法對於具有BER返回繪製做這項工作(如boundBottomCenter不中ItemizedOverlay)。現在所有的工作都很好,甚至非默認的標記也能正確顯示。

乾杯!

相關問題