본문 바로가기

Study/Android

[android] map view 위에 풍선 달아주기

BalloonOverlayItem.java

public class BalloonOverlayItem extends OverlayItem {
 public BalloonOverlayItem(GeoPoint geoPoint, String title, String snippet) {
  super(geoPoint, title, snippet);
 }
}

 

 

BalloonOverlayView.java

public class BalloonOverlayView extends LinearLayout {
 private LinearLayout layout = null;
 private TextView title = null;
 
 public BalloonOverlayView(Context context, int viewOffset) {
  super(context);
  
  setPadding(0, 0, 0, viewOffset);
  LinearLayout.LayoutParams params = 
    new LinearLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
  layout = new LinearLayout(context);
  layout.setVisibility(View.VISIBLE);
  
  LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
  View v = inflater.inflate(R.layout.overlay_balloon, layout);
  title = (TextView) v.findViewById(R.id.overlay_text);

  addView(layout, params);
 }
 
 public void MapBalloonViewHide() {
  if (layout != null) {
   layout.setVisibility(View.GONE);
  }
 }
 
 public void setData(final DataParkingItem item) {
  layout.setVisibility(View.VISIBLE);
  title.setText(item.title);
  title.setOnClickListener(new View.OnClickListener() {
   @Override // 풍선을 클릭하였을 때
   public void onClick(View v) {
    new DetailDialog(getContext(), item);
   }
  });
 }
}

 

 

BalloonItemizedOverlay .java

public class BalloonItemizedOverlay extends ItemizedOverlay<BalloonOverlayItem> {
 private final List<BalloonOverlayItem> mList;
 private final Context context;
 private final MapView mapView;
 private BalloonOverlayView balloonView;
 private View clickRegion;
// private int viewOffset;    변수로는 필요 없어서 숫자를 정함


 public BalloonItemizedOverlay(MapView mapView, List<BalloonOverlayItem> list) {
  super(boundCenterBottom(mapView.getContext().getResources().getDrawable(R.drawable.pin)));
  
  this.context = mapView.getContext();
  this.mList = list;
  this.mapView = mapView;
//  this.viewOffset = 32;
  
  populate();
 }
 
 /**
  * @설명  : 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 px - The padding between the center point and the bottom of the information balloon.
  */
// public void setBalloonBottomOffset(int px) {
//  this.viewOffset = px;
// }
 
 /**
  * @설명  : Override this method to handle a "tap" on a balloon.
  *  By default, does nothing and returns false.
  * @param i - The index of the item whose balloon is tapped.
  * @return true if you handled the tap, otherwise false.
  */
 protected boolean onBalloonTap(int i) {
  // add tab content
  return false;
 }
 
 /*
  * (non-Javadoc)
  * @see com.google.android.maps.ItemizedOverlay#onTap(int)
  */
 @Override
 protected boolean onTap(int index) {
  boolean isRecycled;
  final int thisIndex;
  
  GeoPoint point;
  thisIndex = index;
  BalloonOverlayItem oItem = getItem(index);
  point = oItem.getPoint();
  
  if (balloonView == null) {
   balloonView = new BalloonOverlayView(context, 42);
   clickRegion = (View) balloonView.findViewById(R.id.overlay_layout);
   isRecycled = false;
  } else {
   isRecycled = true;
  }
  
  balloonView.setVisibility(View.GONE);
  balloonView.setData(item);
  
  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);
  }
  return super.onTap(index);
 }
 
 private void setBalloonTouchListener(final int index) {
  try {
   @SuppressWarnings("unused")
   Method m = this.getClass().getDeclaredMethod("onBalloonTap", int.class);
   clickRegion.setOnTouchListener(new View.OnTouchListener() {
    @Override
    public boolean onTouch(View v, MotionEvent event) {
     View parent = ((View) v.getParent()).findViewById(R.id.overlay_layout);
     Drawable d = parent.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();
      }
      onBalloonTap(index);
      return true;
     } else {
      return false;
     }
    }
   });
  } catch (SecurityException e) {
   return;
  } catch (NoSuchMethodException e) {
   return;
  }
 }

 @Override
 protected BalloonOverlayItem createItem(int i) {
  return mList.get(i);
 }

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

 

 

overlay_balloon.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:id="@+id/overlay_layout"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content" android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:background="@drawable/map_balloon"
    android:paddingLeft="3dip"
    android:paddingRight="3dip">

 <TextView android:id="@+id/overlay_text"
     android:layout_width="fill_parent" android:layout_height="wrap_content"
     android:text="가나다라마바사아자차카타파하"
     android:textColor="@color/white"
     android:paddingLeft="2dip"
     android:paddingRight="3dip"
     android:layout_gravity="center_vertical"
     />
 <ImageView android:id="@+id/overlay_next"
     android:layout_width="wrap_content" android:layout_height="wrap_content"
     android:background="@drawable/balloon_next"
     android:paddingLeft="2dip"
     android:paddingRight="2dip"
     android:layout_gravity="center_vertical"
     />

</LinearLayout>

 

 

출처 : 

http://seuny.tistory.com/15