安卓itemizedOverlay的行动的onTap overrided [英] Android itemizedOverlay's onTap action overrided

查看:170
本文介绍了安卓itemizedOverlay的行动的onTap overrided的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个类:

class MapItemizedOverlay extends com.google.android.maps.ItemizedOverlay<OverlayItem> { 
    private Context context;
    private ArrayList items = new ArrayList();

    public MapItemizedOverlay(Context aContext, Drawable marker) {
        super(boundCenterBottom(marker));
        context = aContext;
    }

    public void addOverlayItem(OverlayItem item) {
        items.add(item);
        populate();
    }

    @Override
    protected OverlayItem createItem(int i) {
        return (OverlayItem) items.get(i);
    }

    @Override
    public int size() {
        return items.size();
    }

    @Override
    protected boolean onTap(int index) {
       OverlayItem item = (OverlayItem) items.get(index);
       AlertDialog.Builder dialog = new AlertDialog.Builder(context);
       dialog.setTitle(item.getTitle());
       dialog.setMessage(item.getSnippet());
       dialog.show();
       return true;
    }

    @Override
    public boolean onTap (final GeoPoint p, final MapView mapView) {
        Geocoder geoCoder = new Geocoder(getBaseContext(), Locale.getDefault());
        try {
            List<Address> addresses = geoCoder.getFromLocation(p.getLatitudeE6() / 1E6,
                        p.getLongitudeE6() / 1E6, 1);
            String address = "";
            if (addresses.size() > 0) {
            for (int i=0; i<addresses.get(0).getMaxAddressLineIndex(); i++)
                    address += addresses.get(0).getAddressLine(i) + "\n";
            }   
            address.cancel();
            address.setText(address);
            address.show(); 
        }
        catch (IOException e) {
            e.printStackTrace();
        }
        return true;
    }
}

我添加一些重叠的功能上图:

I add some overlays on the map with the function:

private void initialiseOverlays() {
        // Create an ItemizedOverlay to display a list of markers
        Drawable defaultMarker = getResources().getDrawable(R.drawable.marker);
        MapItemizedOverlay mapItemizedOverlay = new MapItemizedOverlay(this, defaultMarker);

        mapItemizedOverlay.addOverlayItem(new OverlayItem(new GeoPoint((int) (12.345678 * 1E6), (int) (23.456789 * 1E6)), "Point 1", "some-random-text"));
        mapItemizedOverlay.addOverlayItem(new OverlayItem(new GeoPoint((int) (89.012345 * 1E6), (int) (67.890123 * 1E6)), "Point number 2", "more-random-text"));        
        // Add the overlays to the map
        mapView.getOverlays().add(mapItemizedOverlay);
      }

如果只有一个和Ontap功能是指一切工作正常 - 我可以得到的地址,如果我点击的地方在地图上还是可以拿到的地方的标题和内容的对话框,如果我点击这个图标的地方。 但是,我想有两个协同工作的功能,该应用程序来检测,如果点击是在地图(可绘制集)或通过标记一个空位置,并显示它的信息。我怎样才能做到这一点?

If only one of the onTap functions is defined everything works fine - I can either get the address if I click somewhere over the map or I can get a dialog with the place's title and content if I click on the icon over the place. But I want to have both of the functions working together, the application to detect if the click was over an empty place on the map or over a marker(the drawable set) and show it's information. How can I achieve this?

推荐答案

找到了答案 - 必须包括:

Found the answer - had to include the:

if(super.onTap(p, mapView)) {
                return true;
            }

公共布尔中的onTap(最终的GeoPoint磷,最终的MapView MapView类)函数的开始。

这篇关于安卓itemizedOverlay的行动的onTap overrided的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆