在mapview中锁定气球?

时间:2012-01-17 10:23:21

标签: android android-layout android-mapview

我有关于谷歌地图的问题。

当我触摸屏幕时,我在谷歌地图中添加了针脚,当我触摸针头气球正在打开时。从现在开始一切都还可以。

我的问题是当我点击气球添加别针时。

我不想这样做。

如何锁定这种情况?

这是截图:

enter image description here

我的代码:`public void AddPoint(Drawable drawable,MapView mapView,MotionEvent事件){

    p = mapView.getProjection().fromPixels((int)event.getX(),(int)event.getY());
    final MapController mc = mapView.getController();
    mc.setZoom(16);
    CustomItemizedOverlay<CustomOverlayItem> itemizedOverlay = new CustomItemizedOverlay<CustomOverlayItem>(drawable, mapView);

    itemizedOverlay.addOverlay(new CustomOverlayItem(p,"","",""));
    mapView.getOverlays().add(itemizedOverlay);
    mc.animateTo(p);
    mapView.postInvalidate();

}`

onTap方法:`

package com.example;



import android.graphics.drawable.Drawable;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.ViewGroup.LayoutParams;
import com.google.android.maps.*;

import java.util.List;


public abstract class BalloonItemizedOverlay<Item extends OverlayItem> extends ItemizedOverlay<Item> {

    private MapView mapView;
    private BalloonOverlayView<Item> balloonView;
    private View clickRegion;
    private int viewOffset;
    final MapController mc;
    private Item currentFocussedItem;
    private int currentFocussedIndex;

    /**
     * Create a new BalloonItemizedOverlay
     * 
     * @param defaultMarker - A bounded Drawable to be drawn on the map for each item in the overlay.
     * @param mapView - The view upon which the overlay items are to be drawn.
     */
    public BalloonItemizedOverlay(Drawable defaultMarker, MapView mapView) {
        super(defaultMarker);
        this.mapView = mapView;
        viewOffset = 0;
        mc = mapView.getController();
    }

    /**
     * Set the horizontal distance between the marker and the bottom of the information
     * balloon. The default is 0 which works well for center bounded markers. If your
     * marker is center-bottom bounded, call this before adding overlay items to ensure
     * the balloon hovers exactly above the marker. 
     * 
     * @param pixels - The padding between the center point and the bottom of the
     * information balloon.
     */
    public void setBalloonBottomOffset(int pixels) {
        viewOffset = pixels;
    }
    public int getBalloonBottomOffset() {
        return viewOffset;
    }

    /**
     * Override this method to handle a "tap" on a balloon. By default, does nothing 
     * and returns false.
     * 
     * @param index - The index of the item whose balloon is tapped.
     * @param item - The item whose balloon is tapped.
     * @return true if you handled the tap, otherwise false.
     */
    protected boolean onBalloonTap(int index, Item item) {
        return false;
    }

    /* (non-Javadoc)
     * @see com.google.android.maps.ItemizedOverlay#onTap(int)
     */
    @Override
    protected final boolean onTap(int index) {

        currentFocussedIndex = index;
        currentFocussedItem = createItem(index);

        createAndDisplayBalloonOverlay();

        mc.animateTo(currentFocussedItem.getPoint());

        return true;
    }

    /**
     * Creates the balloon view. Override to create a sub-classed view that
     * can populate additional sub-views.
     */
    protected BalloonOverlayView<Item> createBalloonOverlayView() {
        return new BalloonOverlayView<Item>(getMapView().getContext(), getBalloonBottomOffset());
    }

    /**
     * Expose map view to subclasses.
     * Helps with creation of balloon views. 
     */
    protected MapView getMapView() {
        return mapView;
    }

    /**
     * Sets the visibility of this overlay's balloon view to GONE. 
     */
    protected void hideBalloon() {
        if (balloonView != null) {
            balloonView.setVisibility(View.GONE);
        }
    }

    /**
     * Hides the balloon view for any other BalloonItemizedOverlay instances
     * that might be present on the MapView.
     * 
     * @param overlays - list of overlays (including this) on the MapView.
     */
    private void hideOtherBalloons(List<Overlay> overlays) {

        for (Overlay overlay : overlays) {
            if (overlay instanceof BalloonItemizedOverlay<?> && overlay != this) {
                ((BalloonItemizedOverlay<?>) overlay).hideBalloon();
            }
        }

    }

    /**
     * Sets the onTouchListener for the balloon being displayed, calling the
     * overridden {@link #onBalloonTap} method.
     */
    private OnTouchListener createBalloonTouchListener() {
        return new OnTouchListener() {

            float startX;
            float startY;

            public boolean onTouch(View v, MotionEvent event) {

                View l =  ((View) v.getParent()).findViewById(R.id.balloon_main_layout);
                Drawable d = l.getBackground();

                if (event.getAction() == MotionEvent.ACTION_DOWN) {
                    int[] states = {android.R.attr.state_pressed};
                    if (d.setState(states)) {
                        d.invalidateSelf();
                    }
                    startX = event.getX();
                    startY = event.getY();
                    return true;
                } else if (event.getAction() == MotionEvent.ACTION_UP) {
                    int newStates[] = {};
                    if (d.setState(newStates)) {
                        d.invalidateSelf();
                    }
                    if (Math.abs(startX - event.getX()) < 40 &&
                            Math.abs(startY - event.getY()) < 40 ) {
                        // call overridden method
                        onBalloonTap(currentFocussedIndex, currentFocussedItem);
                    }
                    return true;
                } else {
                    return false;
                }

            }
        };
    }

    /* (non-Javadoc)
     * @see com.google.android.maps.ItemizedOverlay#getFocus()
     */
    @Override
    public Item getFocus() {
        return currentFocussedItem;
    }

    /* (non-Javadoc)
     * @see com.google.android.maps.ItemizedOverlay#setFocus(Item)
     */
    @Override
    public void setFocus(Item item) {
        currentFocussedItem = item;

        if (currentFocussedItem == null){
            hideBalloon();
        }
        else{
            createAndDisplayBalloonOverlay();
        }   
    }


    private boolean createAndDisplayBalloonOverlay(){
        boolean isRecycled;
        if (balloonView == null) {
            balloonView = createBalloonOverlayView();
            clickRegion = (View) balloonView.findViewById(R.id.balloon_inner_layout);
            clickRegion.setOnTouchListener(createBalloonTouchListener());
            isRecycled = false;
        } else {
            isRecycled = true;
        }

        balloonView.setVisibility(View.GONE);

        List<Overlay> mapOverlays = mapView.getOverlays();
        if (mapOverlays.size() > 1) {
            hideOtherBalloons(mapOverlays);
        }

        if (currentFocussedItem != null)
            balloonView.setData(currentFocussedItem);

        GeoPoint point = currentFocussedItem.getPoint();
        MapView.LayoutParams params = new MapView.LayoutParams(
                LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, point,
                MapView.LayoutParams.BOTTOM_CENTER);
        params.mode = MapView.LayoutParams.MODE_MAP;

        balloonView.setVisibility(View.VISIBLE);

        if (isRecycled) {
            balloonView.setLayoutParams(params);
        } else {
            mapView.addView(balloonView, params);
        }

        return isRecycled;
    }

}
`

1 个答案:

答案 0 :(得分:0)

对于添加pin,我认为你使用了叠加,你正在使用Overlay类的onDraw()和onTap()方法。你在onTap()方法中同时进行固定和打开气球。因此,为了固定句柄onTap()。并且用于打开气球把手上的点击事件。