我一直在寻找有关扩展 ItemizedOverlay 和 OverlayItem 的正确方法的明确信息,但我得到的结果并不令我满意。 简而言之,我需要在 map 上显示与多个位置相关的不同类型的标记;要显示的标记的类型由位置本身的特定属性决定。我还需要在选择标记时显示某个标记,在未选择或未聚焦时显示另一个标记。 据我了解,我是这样写的:
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 进行设置(将图像的中间底部点设置为指向 map 中的指定点)。当 MyOverlayItem 类中的 getMarker 方法不返回 null 时,使用了另一个标记,图像显示错误(例如,阴影不跟随图像!)。在 getMarker 方法中,您必须为可绘制对象定义一个边界框(使用 drawable.setBounds);但是我认为通过这种方式,您没有指定必须放置在 map 点中的点应该是什么。
因此,问题是:对于从 OverlayItem 对象的 getMarker 方法返回到 ItemizedOverlay 的标记可绘制对象,我如何指定用于放置在 map 中的边界(就像 MyItemizedOverlay 中的 boundCenterBottom 所做的那样)默认可绘制对象)?最好的方法是什么?
;)
最佳答案
好的,经过反复测试似乎是这样的:
drawable.setBounds(-drawable.getIntrinsicWidth()/2, -drawable.getIntrinsicHeight(), drawable.getIntrinsicWidth() /2, 0);
放置在 OverlayItem 的 getMarker 方法中,用于必须返回的可绘制对象(如 boundBottomCenter 在 ItemizedOverlay 中所做的那样)。现在一切正常,甚至非默认标记也能正确显示。
干杯!
关于android - Google Maps : custom ItemizedOverlay and OverlayItem, 显示不同标记的正确方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6173388/