Android Open Source - SmartMap S M Map Fragment






From Project

Back to project page SmartMap.

License

The source code is released under:

Apache License

If you think the Android project SmartMap 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

package com.dennytech.smartmap;
/* w w  w .jav  a  2  s . co  m*/
import android.app.Activity;
import android.content.Intent;
import android.content.res.Resources;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;

import com.dennytech.smartmap.impl.autonavi.ANGeoPoint;
import com.dennytech.smartmap.impl.autonavi.ANMapView;
import com.dennytech.smartmap.impl.autonavi.ANOverlayItem;
import com.dennytech.smartmap.impl.google.GoogleGeoPoint;
import com.dennytech.smartmap.impl.google.GoogleMapView;
import com.dennytech.smartmap.impl.google.GoogleOverlayItem;

/**
 * Base class for display map
 * 
 * @author Jun.Deng
 * 
 */
public abstract class SMMapFragment implements IMapFragment {

  Activity mActivity;

  IMapView mapView;
  boolean isGoogleMap;

  public IMapView mapView() {
    return mapView;
  }

  public void attachActivity(Activity activity) {
    if (mActivity != null)
      throw new IllegalStateException();

    mActivity = activity;
    init();
  }

  protected void init() {
    if (mActivity == null)
      throw new IllegalStateException();

    mapView = creatMapView(mActivity);
    mapView.setBuiltInZoomControls(true);
    mapView.setClickable(true);
  }

  protected IMapView creatMapView(Activity a) {
    IMapView mapView;
    if (a instanceof com.amap.mapapi.map.MapActivity) {
      mapView = new ANMapView(a, null);
      isGoogleMap = false;
    } else {
      mapView = new GoogleMapView(a, keyForGoogleMap());
      isGoogleMap = true;
    }

    mapView.setLayoutParams(new LayoutParams(
        android.view.ViewGroup.LayoutParams.MATCH_PARENT,
        android.view.ViewGroup.LayoutParams.MATCH_PARENT));
    return mapView;
  }

  /**
   * old google map api need a key for it's MapView
   * <p>
   * Note: you should apply different key for debug and release version.
   * 
   * @return key for google map
   */
  protected abstract String keyForGoogleMap();

  public void onAttach(Activity activity) {

  }

  public void onCreate(Bundle savedInstanceState) {

  }

  /**
   * create content view
   * <p>
   * the same as {@link android.app.Fragment#onCreateView}
   * 
   * <p>
   * if you just want an empty map view, return #mapView().getView()#
   */
  final public View onCreateView(LayoutInflater inflater,
      Bundle savedInstanceState) {
    View view = inflater.inflate(layoutResId(), null, false);
    ViewGroup mapContainer = (ViewGroup) view
        .findViewById(R.id.map_container);
    if (mapContainer == null) {
      throw new IllegalArgumentException(
          "the layout you apply by #layoutResId() must contains a "
              + "viewgroup whose id is equal to R.id.map_container");
    }
    mapContainer.addView(mapView.real());
    return view;
  }

  /**
   * return the base content view res ID.
   * 
   * Note:the layout must contains a group view whose id is equal to
   * R.id.map_container
   */
  public abstract int layoutResId();

  public void onActivityCreated(Bundle savedInstanceState) {

  }

  public void onStart() {

  }

  public void onResume() {

  }

  public void onPause() {

  }

  public void onStop() {

  }

  public void onDestroy() {

  }

  public void onDetach() {

  }

  public void onActivityResult(int requestCode, int resultCode, Intent data) {

  }

  final public Resources getResources() {
    if (mActivity == null) {
      throw new IllegalStateException("Fragment " + this
          + " not attached to Activity");
    }
    return mActivity.getResources();
  }

  final public Activity getActivity() {
    return mActivity;
  }

  final public void startActivity(Intent intent) {
    mActivity.startActivity(intent);
  }

  final public void startActivityForResult(Intent intent, int requestCode) {
    mActivity.startActivityForResult(intent, requestCode);
  }

  final public void setResult(int resultCode) {
    mActivity.setResult(resultCode);
  }

  final public void setResult(int resultCode, Intent data) {
    mActivity.setResult(resultCode, data);
  }

  @Override
  public void setTitle(CharSequence title) {
    mActivity.setTitle(title);
  }

  // ////////////////////////////
  // create base object for map
  // /////////////////////////////

  /**
   * Create a common GeoPoint
   * 
   * @param latitudeE6
   * @param longitudeE6
   * @return
   */
  public IGeoPoint createGeoPoint(int latitudeE6, int longitudeE6) {
    if (isGoogleMap) {
      return new GoogleGeoPoint(latitudeE6, longitudeE6);
    }
    return new ANGeoPoint(latitudeE6, longitudeE6);
  }

  /**
   * Create a common OverlayItem
   * 
   * @param point
   * @param title
   * @param snippet
   * @return
   */
  public IOverlayItem createOverlayItem(IGeoPoint point, String title,
      String snippet) {
    if (isGoogleMap) {
      return new GoogleOverlayItem(point, title, snippet);
    }
    return new ANOverlayItem(point, title, snippet);
  }

  /**
   * Create a common LayoutParams for MapView
   * 
   * @param width
   * @param height
   * @param point
   * @param alignment
   * @return
   */
  public IMapViewLayoutParams createMapViewLayoutParams(int width,
      int height, IGeoPoint point, int alignment) {
    if (isGoogleMap) {
      return new GoogleMapView.MVLayoutParams(width, height, point,
          alignment);
    }
    return new ANMapView.MVLayoutParams(width, height, point, alignment);
  }

  /**
   * 
   * @param params
   *            type of MapView.LayoutParams
   * @return
   */
  public IMapViewLayoutParams createMapViewLayoutParams(LayoutParams params) {
    if (isGoogleMap) {
      return new GoogleMapView.MVLayoutParams(
          (com.google.android.maps.MapView.LayoutParams) params);
    }
    return new ANMapView.MVLayoutParams(
        (com.amap.mapapi.map.MapView.LayoutParams) params);
  }

}




Java Source Code List

com.dennytech.smartmap.IGeoPoint.java
com.dennytech.smartmap.IItemizedOverlay.java
com.dennytech.smartmap.IMapController.java
com.dennytech.smartmap.IMapFragment.java
com.dennytech.smartmap.IMapViewLayoutParams.java
com.dennytech.smartmap.IMapView.java
com.dennytech.smartmap.IMyLocationOverlay.java
com.dennytech.smartmap.IOverlayItem.java
com.dennytech.smartmap.IOverlay.java
com.dennytech.smartmap.IProjection.java
com.dennytech.smartmap.ItemizedOverlayCallBack.java
com.dennytech.smartmap.MapUtils.java
com.dennytech.smartmap.MapViewCallback.java
com.dennytech.smartmap.OverlayCallBack.java
com.dennytech.smartmap.SMItemizedOverlay.java
com.dennytech.smartmap.SMMapFragment.java
com.dennytech.smartmap.SMMyLocationOverlay.java
com.dennytech.smartmap.SMOverlay.java
com.dennytech.smartmap.impl.autonavi.ANGeoPoint.java
com.dennytech.smartmap.impl.autonavi.ANItemizedOverlay.java
com.dennytech.smartmap.impl.autonavi.ANMapActivity.java
com.dennytech.smartmap.impl.autonavi.ANMapController.java
com.dennytech.smartmap.impl.autonavi.ANMapViewLayoutParams.java
com.dennytech.smartmap.impl.autonavi.ANMapView.java
com.dennytech.smartmap.impl.autonavi.ANMyLocationOverlay.java
com.dennytech.smartmap.impl.autonavi.ANOverlayItem.java
com.dennytech.smartmap.impl.autonavi.ANOverlay.java
com.dennytech.smartmap.impl.autonavi.ANProjection.java
com.dennytech.smartmap.impl.autonavi.myANMapView.java
com.dennytech.smartmap.impl.google.GoogleGeoPoint.java
com.dennytech.smartmap.impl.google.GoogleItemizedOverlay.java
com.dennytech.smartmap.impl.google.GoogleMapActivity.java
com.dennytech.smartmap.impl.google.GoogleMapController.java
com.dennytech.smartmap.impl.google.GoogleMapView.java
com.dennytech.smartmap.impl.google.GoogleMyLocationOverlay.java
com.dennytech.smartmap.impl.google.GoogleOverlayItem.java
com.dennytech.smartmap.impl.google.GoogleOverlay.java
com.dennytech.smartmap.impl.google.GoogleProjection.java
com.dennytech.smartmap.impl.google.myGoogleMapView.java