Android itemizedOverlay 的 onTap 操作被覆盖

发布于 2024-11-28 15:42:24 字数 2658 浏览 1 评论 0原文

我有一个类:

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;
    }
}

我使用以下功能在地图上添加一些叠加层:

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 函数,则一切正常 - 如果我单击地图上的某处,我可以获取地址,也可以获取一个对话框如果我单击该地点上方的图标,则会显示该地点的标题和内容。 但我想让这两个功能一起工作,应用程序检测单击是否位于地图上的空白位置或标记(可绘制集)上并显示其信息。我怎样才能实现这个目标?

I have a class :

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);
      }

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

累赘 2024-12-05 15:42:24

找到了答案 - 必须

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

public boolean onTap (final GeoPoint p, Final MapView mapView) 函数的开头包含以下内容。

Found the answer - had to include the:

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

in the beginning of the public boolean onTap (final GeoPoint p, final MapView mapView) function.

爱,才寂寞 2024-12-05 15:42:24

你可以在这里使用 onTouch 方法

@Override
        public boolean onTouchEvent(MotionEvent event, final MapView mapView) {
            final int action=event.getAction();
            final int x=(int)event.getX();
            final int y=(int)event.getY();
            result = false;
            if (action==MotionEvent.ACTION_DOWN) {
                downPressed = true;
                drag = false;
/* here check for the items is null or not and then after get all the items 
   so if you click on map and on that place if the item on placed then it will
   check for the item hit other it refer the user click on map not on marker 

*/
                if(items!=null){
                    for(int i=0;i<items.size();i++){
                        OverlayItem item = items.get(i);
                        Point mp=new Point(0,0);
                        mapView.getProjection().toPixels(item.getPoint(), mp);
                        xDragTouchOffset=x-mp.x;
                        yDragTouchOffset=y-mp.y;
                        if (hitTest(item, marker, x-(mp.x-(xDragImageOffset+xDragTouchOffset*2)), y-(mp.y-((yDragImageOffset/2)+yDragTouchOffset)))) {
                            result = true;
                            markerIndex = i;
                            task_id = Long.parseLong(item.getTitle());
                            downPressed = false;
                            markerPressed = true;
                            break;
                        }
                    }
                }
        }
        else if (action==MotionEvent.ACTION_MOVE) {
     // here user pressed and drag the downPressed set to false so it will indicate that user want to move the map and drag set to true;
            downPressed = false;
            drag=true;
        }
        else if (action==MotionEvent.ACTION_UP) {
    // if user not drag then this downPressed is true and it will return the screen and map coordinate
            if(downPressed){

                    tempPoint = mapView.getProjection().fromPixels(x, y);
                    markerLat = tempPoint.getLatitudeE6()/1e6;
                    markerLng = tempPoint.getLongitudeE6()/1e6;
                    mapView.invalidate();
            }

            drag = false;
            downPressed = false;
        }
        return(result | super.onTouchEvent(event, mapView));
    }

我可以这样做希望你能得到一些想法

you can use onTouch method

@Override
        public boolean onTouchEvent(MotionEvent event, final MapView mapView) {
            final int action=event.getAction();
            final int x=(int)event.getX();
            final int y=(int)event.getY();
            result = false;
            if (action==MotionEvent.ACTION_DOWN) {
                downPressed = true;
                drag = false;
/* here check for the items is null or not and then after get all the items 
   so if you click on map and on that place if the item on placed then it will
   check for the item hit other it refer the user click on map not on marker 

*/
                if(items!=null){
                    for(int i=0;i<items.size();i++){
                        OverlayItem item = items.get(i);
                        Point mp=new Point(0,0);
                        mapView.getProjection().toPixels(item.getPoint(), mp);
                        xDragTouchOffset=x-mp.x;
                        yDragTouchOffset=y-mp.y;
                        if (hitTest(item, marker, x-(mp.x-(xDragImageOffset+xDragTouchOffset*2)), y-(mp.y-((yDragImageOffset/2)+yDragTouchOffset)))) {
                            result = true;
                            markerIndex = i;
                            task_id = Long.parseLong(item.getTitle());
                            downPressed = false;
                            markerPressed = true;
                            break;
                        }
                    }
                }
        }
        else if (action==MotionEvent.ACTION_MOVE) {
     // here user pressed and drag the downPressed set to false so it will indicate that user want to move the map and drag set to true;
            downPressed = false;
            drag=true;
        }
        else if (action==MotionEvent.ACTION_UP) {
    // if user not drag then this downPressed is true and it will return the screen and map coordinate
            if(downPressed){

                    tempPoint = mapView.getProjection().fromPixels(x, y);
                    markerLat = tempPoint.getLatitudeE6()/1e6;
                    markerLng = tempPoint.getLongitudeE6()/1e6;
                    mapView.invalidate();
            }

            drag = false;
            downPressed = false;
        }
        return(result | super.onTouchEvent(event, mapView));
    }

here i can do this way hope you get some idea

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文