在 ItemizedOverlay
上调用 setFocus(null)
不会“取消聚焦”当前标记。根据文档:
... If the Item is not found, this is a no-op. You can also pass null to remove focus.
这是我的代码:
MapItemizedOverlay
public class MapItemizedOverlay extends ItemizedOverlay<OverlayItem> {
private ArrayList<OverlayItem> items = new ArrayList<OverlayItem>();
public MapItemizedOverlay(Drawable defaultMarker) {
super(defaultMarker);
}
public void addOverlay(OverlayItem overlay) {
items.add(overlay);
populate();
}
@Override
protected OverlayItem createItem(int i) {
return items.get(i);
}
@Override
public int size() {
return items.size();
}
}
创建 map 叠加层和一个标记:
StateListDrawable youIcon = (StateListDrawable)getResources().getDrawable(R.drawable.marker_icon);
int width = youIcon.getIntrinsicWidth();
int height = youIcon.getIntrinsicHeight();
youIcon.setBounds(-13, 0-height, -13+width, 0);
GeoPoint location = new GeoPoint(40800816,-74122009);
MapItemizedOverlay overlay = new MapItemizedOverlay(youIcon);
OverlayItem item = new OverlayItem(location, "title", "snippet");
overlay.addOverlay(item);
mapView.getOverlays().add(overlay);
R.drawable.marker_icon
定义如下:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_focused="true" android:drawable="@drawable/marker_selected" />
<item android:state_selected="true" android:drawable="@drawable/marker_selected" />
<item android:drawable="@drawable/marker_normal" />
</selector>
现在,为了测试 setFocus() 行为,我将按钮放在 Activity 窗口上,并使用以下 onClick 监听器:
Button focusBtn = (Button)findViewById(R.id.focusbtn);
focusBtn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
for(Overlay ov : mapView.getOverlays())
{
if(ov.getClass().getSimpleName().equals("MapItemizedOverlay") == true)
{
MapItemizedOverlay miv = (MapItemizedOverlay)ov;
if(miv.getFocus() == null)
miv.setFocus(miv.getItem(0));
else
miv.setFocus(null);
break;
}
}
mapView.invalidate();
}
});
预期的行为是:单击按钮切换标记选择。
它只工作一次 - 第一次点击它会选择标记,再次点击不会取消选择标记。最奇怪的是,在调用 setFocus(null) 之后,getFocus() 也返回 null - 就像叠加层没有焦点项(我调试过)。但即使在调用 mapView.invalidate() 之后,标记仍处于“选定”(聚焦)状态。
最佳答案
正如 Rpond 在对我的问题的评论中所说,它看起来像是 API 中的一个开放错误。
同时我自己解决了它。下面是解决方法代码。您需要扩展 OverlayItem
类并检查 overlay.getFocus()
返回的内容。
public class MapOverlayItem extends OverlayItem {
MapItemizedOverlay overlay = null;
public MapOverlayItem(GeoPoint point, MapItemizedOverlay ov)
{
super(point, null, null);
this.overlay = ov;
}
public MapOverlayItem(GeoPoint point, String title, String snippet) {
super(point, title, snippet);
}
@Override
public Drawable getMarker(int stateBitset) {
Drawable icon = overlay.getDefaultMarker();
if(stateBitset == 0)
return icon;
OverlayItem focusedItem = overlay.getFocus();
if(focusedItem == null) {
OverlayItem.setState(icon, 0);
return icon;
}
if(focusedItem.equals(this) == true)
OverlayItem.setState(icon, stateBitset);
else
OverlayItem.setState(icon, 0);
return icon;
}
}
关于Android mapView ItemizedOverlay setFocus 不能正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2737445/