Android mapView ItemizedOverlay setFocus 不能正常工作

标签 android google-maps setfocus itemizedoverlay

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/

相关文章:

android - 显示错误消息的最佳实践

java - 我想从我的服务启动线程,这样操作就不会在 android 的主线程中运行

android - 无法将类型 'Android.Support.V4.App.Fragment' 转换为 'Android.Gms.Maps.MapFragment'

javascript - Google 会阻止使用客户端 jsapi 调用获取用户位置的尝试吗?

Android 谷歌地图 v2 永久标记信息窗口

c# - 单击wpf中的RepeatButton时如何将光标聚焦在文本框中?

c++ - winapi C++ 处理焦点

javascript - 将焦点设置为 jHtmlArea jQuery 插件

android - React Native App 在启动时在 android 11 上崩溃而没有给出错误

java - 无法将 gson 转换为我的对象类型