我一直在寻找关于扩展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在主MapActivity中创建并填充了MyOverlayItem。现在: 正确放置的唯一标记是默认标记。也就是说,那些具有可传递给MyItemizedOverlay的构造函数的那个,并且使用boundCenterBottom(它将图像的中间底点设置为指向地图中的指定点)进行设置。当MyOverlayItem类中的getMarker方法不返回null,并且使用另一个标记时,图像显示错误(例如,阴影不跟随图像!)。在getMarker方法中,必须为drawable定义一个边界框(使用drawable.setBounds);但是我认为,不过这样,你没有指定必须放在地图上的点。
因此,问题是:对于从OverlayItem对象的getMarker方法返回到ItemizedOverlay的标记drawable,我如何指定用于放置在地图中的边界(与MyItemizedOverlay中的boundCenterBottom一样)默认的drawable)?什么是最好的方法呢?
)
答案 0 :(得分:12)
好的,经过反复测试,似乎是这样:
drawable.setBounds(-drawable.getIntrinsicWidth()/2, -drawable.getIntrinsicHeight(), drawable.getIntrinsicWidth() /2, 0);
放置在OverlayItem的getMarker方法中,用于必须返回的drawable执行作业(如在boundizedOverlay中的boundBottomCenter)。现在一切正常,甚至非默认标记也能正确显示。
干杯!