Android Open Source - WhatsUp Balloon Itemized Overlay






From Project

Back to project page WhatsUp.

License

The source code is released under:

GNU General Public License

If you think the Android project WhatsUp listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

/***
 * Copyright (c) 2010 readyState Software Ltd
 * /*w  w w .  j  a v a2  s .c  o  m*/
 * Licensed under the Apache License, Version 2.0 (the "License"); you may
 * not use this file except in compliance with the License. You may obtain
 * a copy of the License at
 * http://www.apache.org/licenses/LICENSE-2.0
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 
 */

package nu.placebo.whatsup.balloon;



import java.util.List;

import nu.placebo.whatsup.R;
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.GeoPoint;
import com.google.android.maps.ItemizedOverlay;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;
import com.google.android.maps.OverlayItem;

/**
 * NOTICE!!! NOT OUR CODE!!!
 * Link to original code: https://github.com/jgilfelt/android-mapviewballoons 
 * 
 * An abstract extension of ItemizedOverlay for displaying an information balloon
 * upon screen-tap of each marker overlay.
 * 
 * @author Jeff Gilfelt
 */
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() {
      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(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();
    }  
  }
  

  /**
   * Creates and displays the balloon overlay by recycling the current 
   * balloon or by inflating it from xml. 
   * @return true if the balloon was recycled false otherwise 
   */
  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);
    }
    GeoPoint point = null;
    if (currentFocussedItem != null) {
      balloonView.setData(currentFocussedItem);
      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;
  }
  
}




Java Source Code List

nu.placebo.whatsup.activity.AnnotationActivity.java
nu.placebo.whatsup.activity.CreateAnnotationActivity.java
nu.placebo.whatsup.activity.CreateCommentActivity.java
nu.placebo.whatsup.activity.ListViewActivity.java
nu.placebo.whatsup.activity.LogInActivity.java
nu.placebo.whatsup.activity.LoginRegTabActivity.java
nu.placebo.whatsup.activity.MapViewActivity.java
nu.placebo.whatsup.activity.Marker.java
nu.placebo.whatsup.activity.MenuHelper.java
nu.placebo.whatsup.activity.PositionPickerActivity.java
nu.placebo.whatsup.activity.RefPointActivity.java
nu.placebo.whatsup.activity.RegisterActivity.java
nu.placebo.whatsup.android.os.AsyncTask.java
nu.placebo.whatsup.balloon.BalloonItemizedOverlay.java
nu.placebo.whatsup.balloon.BalloonOverlayView.java
nu.placebo.whatsup.constants.Constants.java
nu.placebo.whatsup.ctrl.SessionHandler.java
nu.placebo.whatsup.datahandling.DataProvider.java
nu.placebo.whatsup.datahandling.DataReturnListener.java
nu.placebo.whatsup.datahandling.DataReturn.java
nu.placebo.whatsup.datahandling.DatabaseConnectionLayer.java
nu.placebo.whatsup.datahandling.DatabaseHelper.java
nu.placebo.whatsup.model.Annotation.java
nu.placebo.whatsup.model.Comment.java
nu.placebo.whatsup.model.ExtendedOverlayItem.java
nu.placebo.whatsup.model.GeoLocation.java
nu.placebo.whatsup.model.ListMarker.java
nu.placebo.whatsup.model.ReferencePoint.java
nu.placebo.whatsup.model.SessionInfo.java
nu.placebo.whatsup.network.AbstractNetworkOperation.java
nu.placebo.whatsup.network.Action.java
nu.placebo.whatsup.network.AnnotationCreate.java
nu.placebo.whatsup.network.AnnotationRetrieve.java
nu.placebo.whatsup.network.CommentCreate.java
nu.placebo.whatsup.network.GeoLocationsRetrieve.java
nu.placebo.whatsup.network.Login.java
nu.placebo.whatsup.network.NetworkCalls.java
nu.placebo.whatsup.network.NetworkOperationListener.java
nu.placebo.whatsup.network.NetworkOperation.java
nu.placebo.whatsup.network.NetworkTask.java
nu.placebo.whatsup.network.OperationResult.java
nu.placebo.whatsup.network.RegisterOperation.java
nu.placebo.whatsup.network.SessionTest.java
nu.placebo.whatsup.util.GeoPointUtil.java
nu.placebo.whatsup.util.Geodetics.java
nu.placebo.whatsup.util.ValidationUtil.java
nu.placebo.whatsuptest.TestStarter.java
nu.placebo.whatsuptest.activitytest.AnnotationActivityTest.java
nu.placebo.whatsuptest.activitytest.ListViewActivityTest.java
nu.placebo.whatsuptest.activitytest.MapViewActivityTest.java
nu.placebo.whatsuptest.networktest.NetworkTest.java
nu.placebo.whatsuptest.utiltest.GeoPointUtilTest.java
nu.placebo.whatsuptest.utiltest.GeodeticsTest.java
nu.placebo.whatsuptest.utiltest.ValidationTest.java