GoogleMaps:自定义ItemizedOverlay和OverlayItem,这是显示不同标记的正确方法
||
我一直在寻找有关扩展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设置(将boundingCenterBottom设置为将图像的中间底部指向地图中的指定点)。当MyOverlayItem类中的getMarker方法不返回null并使用另一个标记时,该图像将显示错误(例如,阴影不跟随该图像!)。在getMarker方法中,必须为drawable(带有drawable.setBounds)定义一个边界框。但是我认为以这种方式,您无需指定必须放置在地图点上的点。
因此,问题是:对于从OverlayItem对象的getMarker方法返回到ItemizedOverlay的标记可绘制对象,我如何指定用于放置在地图中的边界(如MyItemizedOverlay中的boundCenterBottom用于默认可绘制对象)?最好的方法是什么?
;)
没有找到相关结果
已邀请:
1 个回复
嗜蒂谷尘旱
放置在OverlayItem的getMarker方法中,以确保必须返回的可绘制对象完成该工作(就像boundBottomCenter在ItemizedOverlay中所做的那样)。现在,一切正常,甚至非默认标记也可以正确显示。 干杯!