2011-10-10 78 views
1

我在我的应用程序中创建了mapview,还有一些标记在mapview中,它从我的json解析中获取值,并在弹出式窗口中显示它..再次我需要在点击时开始一个活动该气球图像中我应该开始一个新的活动..请帮我找到一个解决方案。这是我的BallonItemizedOverlayclass在地图视图中点击气球应该开始另一个活动

package com.smartmedia.salonaudi.map; 

import java.lang.reflect.Method; 
import java.util.List; 

import android.app.Activity; 
import android.content.Context; 
import android.graphics.drawable.Drawable; 
import android.util.Log; 
import android.view.MotionEvent; 
import android.view.View; 
import android.view.View.OnTouchListener; 

import com.google.android.maps.GeoPoint; 
import com.google.android.maps.ItemizedOverlay; 
import com.google.android.maps.MapController; 
import com.google.android.maps.MapView; 
import com.google.android.maps.MapView.LayoutParams; 
import com.google.android.maps.Overlay; 
import com.google.android.maps.OverlayItem; 
import com.smartmedia.salonaudi.R; 

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

    private MapView mapView; 
    private BalloonOverlayView<Item> balloonView; 
    private View clickRegion; 
    private int viewOffset; 
    private Context mContext; 
     private Activity mActivity; 
     private boolean mCheckIn; 
    final MapController mc; 

    /** 
    * 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; 
     mContext = mapView.getContext(); 
     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. 
    * @return true if you handled the tap, otherwise false. 
    */ 
    protected boolean onBalloonTap(int index) { 

     return true; 
    } 

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

     boolean isRecycled; 
     final int thisIndex; 
     GeoPoint point; 

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

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

     balloonView.setVisibility(View.GONE); 

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

     balloonView.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); 

     balloonView.setVisibility(View.VISIBLE); 

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

     mc.animateTo(point); 

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

    } 

} 
+0

你应该在onBalloonTap()方法内开始你的活动。那么你遇到的问题是什么? – Sujit

+0

[在Google地图上绘制线条/路径]的可能重复(http://stackoverflow.com/questions/2176397/drawing-a-line-path-on-google-maps) – harish

回答

2
protected boolean onBalloonTap(int index) { 
      Intent myintent = new Intent(this, 
        secondactivity.class); 
      mContext.startActivity(myintent); 
      return true; 
     } 
+0

该类不会扩展活动,然后我们将如何添加startActivity – harish

+0

在你的主要活动中添加这个类作为内部类它的作品对我来说是yar ......... –

+0

可以提供一些代码..我尝试了很多..但是... – harish

0
public class CustomItemizedOverlay extends BalloonItemizedOverlay<OverlayItem> { 

    private ArrayList<OverlayItem> m_overlays = new ArrayList<OverlayItem>(); 
    private Context c; 

    private int overlayIndex; 
    private OnBalloonTapListener listener; 

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

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

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

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

    @Override 
    protected boolean onBalloonTap(int index) { 
     if (listener!=null){ 
      listener.onBalloonTap(overlayIndex);    
     } 
     return true; 
    } 

    public void setOnBallonTapListener(int overlayIndex, OnBalloonTapListener listener){ 
     this.overlayIndex = overlayIndex; 
     this.listener = listener;  
    } 

    public interface OnBalloonTapListener{ 

     public void onBalloonTap(int balloonIndex); 

    } 

} 

这是我听的气球自来水定制覆盖。我用它是这样的:

CustomItemizedOverlay overlay = new CustomItemizedOverlay(marker, mapView); 
     overlay.setOnBallonTapListener(overlayIndex, new OnBalloonTapListener() { 

      @Override 
      public void onBalloonTap(int balloonIndex) { 
       //TODO something 
      } 
     }); 

它适用于我很好。

-1
GeoPoint adrpoint = new GeoPoint((int) (Lat() *1E6) , (int) (Lon() *1E6)); 
OverlayItem adroverlayitem = new OverlayItem(adrpoint, Title, Message); 
final MyItemizedOverlay itemizedOverlayFind = new MyItemizedOverlay(drawable, mapView); 
itemizedOverlayFind.addOverlay(adroverlayitem); 
itemizedOverlayFind.setOnBallonTapListener(itemizedOverlayFind.size()-1, new OnBalloonTapListener() { 
    @Override 
    public void onBalloonTap(int balloonIndex) { 
     String msg = itemizedOverlayFind.getItem(balloonIndex).getSnippet(); 
     Toast.makeText(getBaseContext(), msg + ",Lat: " + itemizedOverlayFind.getItem(balloonIndex).getPoint().getLatitudeE6() + ", Lon: " + itemizedOverlayFind.getItem(balloonIndex).getPoint().getLongitudeE6(), Toast.LENGTH_LONG).show(); 
     //TODO something 
    } 
}); 
mapOverlays.add(itemizedOverlayFind); 
itemizedOverlayFind.onTap(itemizedOverlayFind.size()-1); 
mc.animateTo(adrpoint); 
mc.setCenter(adrpoint); 
+1

尝试不只是张贴代码,但一个解释。 – CoffeeRain

相关问题