Android逐项重叠onTap操作覆盖

时间:2011-08-09 11:28:03

标签: android maps itemizedoverlay overlayitem

我有一个班级:

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函数,一切正常 - 如果我点击地图上的某个地方,我可以获取地址,或者如果我点击地图上的图标,我可以得到一个包含地点标题和内容的对话框。 但是我希望两个函数一起工作,应用程序检测点击是否在地图上的空位或标记(可绘制的集合)上并显示它的信息。我怎样才能做到这一点?

2 个答案:

答案 0 :(得分:1)

找到答案 - 必须包括:

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

public boolean onTap (final GeoPoint p, final MapView mapView)函数的开头。

答案 1 :(得分:0)

你可以使用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));
    }

我可以这样做,希望你有所了解