开发者

i cant use startActivity on Mapview

I want to use startActivity() but I can't

How I fix this code?

public class MyItemizedOverlay2 extends BalloonItemizedOverlay2<OverlayItem> {


    private static final Context getContext = null;
    private ArrayList<OverlayItem> m_overlays2 = new ArrayList<OverlayItem>();
    private Context c;
    private  int nIndex=0;
    private Context getbaseContext;

    public MyItemizedOverlay2(Drawable defaultMarker, MapView mapView) {
        super(boundCenter(defaultMarker), mapView);
        c = mapView.getContext();
    }

    public void addOverlay(OverlayItem overlay) {
        m_overlays2.add(overlay);
        populate();
    }

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

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

    @Override
    protected boolean onBalloonTap(int index) {
        Intent i = new Intent ("page1");
        startActivity(i);
     nIndex = index;
     ;
        //Toast.makeText(c, "Overlay2입니다. " + index,
        //      Toast.LENGTH_LONG).show();
        return true;
    }





}




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

    private MapView mapView;
    private BalloonOverlayView2<?> balloonView2;
    private BalloonOverlayView<?> balloonView;
    private View clickRegion;
    private int viewOffset;
    final MapController mc;
    public  int indexN;




    /**
     * 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 BalloonItemizedOverlay2(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;
    }

    /**
     * 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.
     * @return true if you handled the tap, otherwise false.
     */




    protected boolean onBalloonTap(int index) {

        return false;
    }

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

    indexN=index;



        boolean isRecycled;
        final int thisIndex;
        GeoPoint point;

        thisIndex = index;
        point = createItem(index).getPoint();

        if (balloonView2 == null) {
            balloonView2 = new BalloonOverlayView2<Object>(mapView.getContext(), viewOffset);


            clickRegion = (View) balloonView2.findViewById(R.id.balloon_inner_layout);
            isRecycled = false;
        } else {
            isRecycled = true;
        }

        balloonView2.setVisibility(View.GONE);



        switch(thisIndex){
        case 0:
            balloonView2.over21.setVisibility(View.VISIBLE);
            //balloonView.over1.setVisibility(View.INVISIBLE);
            break;
        case 1:
            balloonView2.over22.setVisibility(View.VISIBLE);
            //balloonView.over0.setVisibility(View.INVISIBLE);
            break;
        case 2:
            balloonView2.over23.setVisibility(View.VISIBLE);
            break;
        case 3:
            balloonView2.over24.setVisibility(View.VISIBLE);
            break;
        case 4:
            balloonView2.over25.setVisibility(View.VISIBLE);
            break;
        case 5:
            balloonView2.over26.setVisibility(View.VISIBLE);
            break;
        case 6:
            balloonView2.over27.setVisibility(View.VISIBLE);
            break;
        case 7:
            balloonView2.over28.setVisibility(View.VISIBLE);
            break;
        case 8:
            balloonView2.over29.setVisibility(View.VISIBLE);
            break;

        case 9:
            balloonView2.over30.setVisibility(View.VISIBLE);
            break;
        case 10:
            balloonView2.over31.setVisibility(View.VISIBLE);
            break;
        case 11:
            balloonView2.over32.setVisibility(View.VISIBLE);
            break;
        case 12:
            balloonView2.over33.setVisibility(View.VISIBLE);
            break;
        case 13:
            balloonView2.over34.setVisibility(View.VISIBLE);
            break;
        case 14:
            balloonView2.over35.setVisibility(View.VISIBLE);
            break;
        case 15:
            balloonView2.over36.setVisibility(View.VISIBLE);
            break;
        case 16:
            balloonView2.over37.setVisibility(View.VISIBLE);
            break;
        case 17:
            balloonView2.over38.setVisibility(View.VISIBLE);
            break;
        case 18:
            balloonView2.over39.setVisibility(View.VISIBLE);
            break;
        case 19:
            balloonView2.over40.setVisibility(View.VISIBLE);
            break;
        case 20:
            balloonView2.over41.setVisibility(View.VISIBLE);
            break;
        case 21:
            balloonView2.over42.setVisibility(View.VISIBLE);
            break;
        case 22:
            balloonView2.over43.setVisibility(View.VISIBLE);
            break;
        case 23:
            balloonView2.over44.setVisibility(View.VISIBLE);
            break;
        case 24:
            balloonView2.over45.setVisibility(View.VISIBLE);
            break;
        case 25:
            balloonView2.over46.setVisibility(View.VISIBLE);
            break;
        case 26:
            balloonView2.over47.setVisibility(View.VISIBLE);
            break;
        case 27:
            balloonView2.over48.setVisibility(View.VISIBLE);
            break;
        case 28:
            balloonView2.over49.setVisibility(View.VISIBLE);
            break;
        case 29:
            balloonView2.over50.setVisibility(View.VISIBLE);
            break;
        case 30:
            balloonView2.over51.setVisibility(View.VISIBLE);
            break;
        case 31:
            balloonView2.over52.setVisibility(View.VISIBLE);
            break;
        case 32:
            balloonView2.over32.setVisibility(View.VISIBLE);
            break;
        case 33:
            balloonView2.over33.setVisibility(View.VISIBLE);
            break;
        case 34:
            balloonView2.over34.setVisibility(View.VISIBLE);
            break;
        case 35:
            balloonView2.over35.setVisibility(View.VISIBLE);
            break;
        case 36:
            balloonView2.over36.setVisibility(View.VISIBLE);
            break;
        case 37:
            balloonView2.over37.setVisibility(View.VISIBLE);
            break;
        case 38:
            balloonView2.over38.setVisibility(View.VISIBLE);
            break;
        case 39:
            balloonView2.over39.setVisibility(View.VISIBLE);
            break;
        case 40:
            balloonView2.over40.setVisibility(View.VISIBLE);
            break;
        case 441:
            balloonView2.over41.setVisibility(View.VISIBLE);
            break;
        case 42:
            balloonView2.over42.setVisibility(View.VISIBLE);
            break;
        case 43:
            balloonView2.over43.setVisibility(View.VISIBLE);
            break;
        case 44:
            balloonView2.over44.setVisibility(View.VISIBLE);
            break;
        case 45:
            balloonView2.over45.setVisibility(View.VISIBLE);
            break;
        case 46:
            balloonView2.over46.setVisibility(View.VISIBLE);
            break;
        case 47:
            balloonView2.over47.setVisibility(View.VISIBLE);
            break;
        case 48:
            balloonView2.over48.setVisibility(View.VISIBLE);
            break开发者_运维问答;
        case 49:
            balloonView2.over49.setVisibility(View.VISIBLE);
            break;
        case 50:
            balloonView2.over50.setVisibility(View.VISIBLE);
            break;
        case 51:
            balloonView2.over51.setVisibility(View.VISIBLE);
            break;
        case 52:
            balloonView2.over52.setVisibility(View.VISIBLE);
            break;


        }
        //////////////////////////////


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

        balloonView2.setData(createItem(index));

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

        setBalloonTouchListener(thisIndex);

        balloonView2.setVisibility(View.VISIBLE);

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


        mc.animateTo(point);

        return true;
    }

    /**
     * Sets the visibility of this overlay's balloon view to GONE. 
     */
    private void hideBalloon() {
        if (balloonView2 != null) {

            balloonView2.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 BalloonItemizedOverlay2<?> && overlay != this) {
                ((BalloonItemizedOverlay2<?>) overlay).hideBalloon();

            }

        }

    }

    /**
     * Sets the onTouchListener for the balloon being displayed, calling the
     * overridden onBalloonTap if implemented.
     * 
     * @param thisIndex - The index of the item whose balloon is tapped.
     */
    private void setBalloonTouchListener(final int thisIndex) {

        try {
            @SuppressWarnings("unused")
            Method m = this.getClass().getDeclaredMethod("onBalloonTap", int.class);

            clickRegion.setOnTouchListener(new OnTouchListener() {
                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();
                        }
                        return true;
                    } else if (event.getAction() == MotionEvent.ACTION_UP) {
                        int newStates[] = {};
                        if (d.setState(newStates)) {
                            d.invalidateSelf();
                        }
                        // call overridden method
                        onBalloonTap(thisIndex);
                        return true;
                    } else {
                        return false;
                    }

                }
            });

        } catch (SecurityException e) {
            Log.e("BalloonItemizedOverlay", "setBalloonTouchListener reflection SecurityException");
            return;
        } catch (NoSuchMethodException e) {
            // method not overridden - do nothing
            return;
        }

    }



}


Try this:

mapView.getContext().startActivity();
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜