Android Open Source - arcgis-runtime-samples-android Main Activity






From Project

Back to project page arcgis-runtime-samples-android.

License

The source code is released under:

Apache License

If you think the Android project arcgis-runtime-samples-android 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 2014 ESRI
 */*from www .j  a  v a2  s.  c  o  m*/
 * All rights reserved under the copyright laws of the United States
 * and applicable international laws, treaties, and conventions.
 *
 * You may freely redistribute and use this sample code, with or
 * without modification, provided you include the original copyright
 * notice and use restrictions.
 *
 * See the Sample code usage restrictions document for further information.
 *
 */

package com.esri.arcgis.samples.offlineanalysis;

import java.io.File;
import java.io.FileNotFoundException;

import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.widget.Toast;

import com.esri.android.map.GraphicsLayer;
import com.esri.android.map.Layer;
import com.esri.android.map.MapOnTouchListener;
import com.esri.android.map.MapView;
import com.esri.android.map.RasterLayer;
import com.esri.android.map.event.OnZoomListener;
import com.esri.core.analysis.LineOfSight;
import com.esri.core.analysis.Viewshed;
import com.esri.core.geometry.Point;
import com.esri.core.map.Graphic;
import com.esri.core.raster.FileRasterSource;
import com.esri.core.raster.FunctionRasterSource;
import com.esri.core.symbol.SimpleMarkerSymbol;
import com.esri.core.symbol.SimpleMarkerSymbol.STYLE;

/**
 * The Offline Analysis sample shows how to do Line of Sight and Viewshed
 * analysis on raster DEM files on device. The sample shows how to extend the
 * MapOnTouchListener to customize the apps response to tapping on the map.
 *
 */
public class MainActivity extends Activity {

  static final String TAG = "OfflineAnalysis";

  // offline data
  private static File demoDataFile;
  private static String offlineDataSDCardDirName;
  private static String filename;

  // Map objects
  MapView mMapView;
  FileRasterSource mRasterSource;

  private RasterLayer mRasterLayer;
  private String mRaster;
  private GraphicsLayer mGraphicsLayer;
  private LineOfSight mLineOfSight;
  private Layer mLosLayer;
  private FunctionRasterSource functionRS;
  private RasterLayer viewShedLayer;
  private Viewshed mViewshed;

  // ActionBar menu items
  private MenuItem mLOS;
  private MenuItem mVSmenu;

  private double mObserverZOffset = Double.NaN;
  private double mTargetZOffset = Double.NaN;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    // create the path to local raster file
    demoDataFile = Environment.getExternalStorageDirectory();
    offlineDataSDCardDirName = this.getResources().getString(
        R.string.raster_dir);
    filename = this.getResources().getString(R.string.raster_file);

    // create the raster path
    mRaster = demoDataFile + File.separator + offlineDataSDCardDirName
        + File.separator + filename;

    // create the mapview
    mMapView = (MapView) findViewById(R.id.map);

    try {
      // create the raster source
      mRasterSource = new FileRasterSource(mRaster);
      // create the raster layer
      mRasterLayer = new RasterLayer(mRasterSource);
      // add the layer
      mMapView.addLayer(mRasterLayer);
    } catch (IllegalArgumentException e) {
      Log.e(TAG, e.getMessage());
    } catch (FileNotFoundException e) {
      Log.e(TAG, e.getMessage());
    } catch (RuntimeException e) {
      Log.e(TAG, e.getMessage());
    }

    // add graphics layer
    mGraphicsLayer = new GraphicsLayer();
    mMapView.addLayer(mGraphicsLayer);

  }

  @Override
  public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);

    // get the analysis menu items
    mLOS = menu.getItem(0);
    mVSmenu = menu.getItem(1);

    return true;
  }

  @Override
  public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    // Handle menu item selection
    switch (id) {
    case R.id.menu_analysis_los:
      mLOS.setChecked(true);

      Toast toast = Toast.makeText(getApplicationContext(),
          "Line of Sight selected", Toast.LENGTH_LONG);
      toast.show();
      performLOS();
      return true;
    case R.id.menu_analysis_viewshed:
      mVSmenu.setChecked(true);

      Toast toast2 = Toast.makeText(getApplicationContext(),
          "Viewshed selected", Toast.LENGTH_LONG);
      toast2.show();
      calculateViewshed();
      return true;
    default:
      return super.onOptionsItemSelected(item);
    }
  }

  @Override
  protected void onPause() {
    super.onPause();

    // Call MapView.pause to suspend map rendering while the activity is
    // paused, which can save battery usage.
    if (mMapView != null) {
      mMapView.pause();
    }
  }

  @Override
  protected void onResume() {
    super.onResume();

    // Call MapView.unpause to resume map rendering when the activity
    // returns to the foreground.
    if (mMapView != null) {
      mMapView.unpause();
    }
  }

  /*
   * Remove any analysis layers on map
   */
  private void clearFunctionLayers() {
    turnOffLayer(mLosLayer);
    turnOffLayer(viewShedLayer);
    // clear any graphics
    if (mGraphicsLayer != null) {
      mGraphicsLayer.removeAll();
    }
  }

  /*
   * Remove layer and recycle
   * Dispose of analysis functions
   */
  private void turnOffLayer(Layer layer) {
    if (layer != null && !layer.isRecycled()) {
      mMapView.removeLayer(layer);
      layer.recycle();
      
      
      if(mViewshed != null){
        mViewshed.dispose();
        mViewshed = null;
      }
      
      if(mLineOfSight != null){
        mLineOfSight.dispose();
        mLineOfSight = null;
      }
      
    }

  }

  /*
   * Line of Sight Analysis
   */
  private void performLOS() {
    // clear any analysis layers showing
    clearFunctionLayers();

    try {
      mLineOfSight = new LineOfSight(mRaster);

    } catch (IllegalArgumentException e) {
      e.printStackTrace();
    } catch (FileNotFoundException e) {
      e.printStackTrace();
    } catch (RuntimeException e) {
      e.printStackTrace();
    }

    if (mLineOfSight != null) {
      mLosLayer = mLineOfSight.getOutputLayer();
      mMapView.addLayer(mLosLayer);
      // set observer features
      mLineOfSight.setObserver(mMapView.getCenter());
      mLineOfSight.setObserverZOffset(mObserverZOffset);
      mLineOfSight.setTargetZOffset(mTargetZOffset);

      // Set gesture used to change the position of the observer and
      // target.
      // When the position of a target is changed, the task will be
      // executed and
      // the result will be rendered on the map view.
      mMapView.setOnTouchListener(new OnTouchListenerLOS(mMapView
          .getContext(), mMapView, mLineOfSight));
      // Reset the observer to center of map on map zoom
      mMapView.setOnZoomListener(new OnZoomListener() {
        private static final long serialVersionUID = 1L;

        @Override
        public void preAction(float pivotX, float pivotY, double factor) {
        }

        @Override
        public void postAction(float pivotX, float pivotY, double factor) {
          // set the observer to the center of the map
          mLineOfSight.setObserver(mMapView.getCenter());
        }
      });
    } else {
      Toast toast = Toast.makeText(getApplicationContext(),
          "Raster File Not Found", Toast.LENGTH_LONG);
      toast.show();
    }

  }

  /*
   * Viewshed Analysis
   */
  private void calculateViewshed() {
    // clear any analysis layers showing
    clearFunctionLayers();
    // create a viewshed
    try {
      mViewshed = new Viewshed(mRaster);

    } catch (IllegalArgumentException e) {
      e.printStackTrace();
    } catch (FileNotFoundException e) {
      e.printStackTrace();
    } catch (RuntimeException e) {
      e.printStackTrace();
    }

    if (mViewshed != null) {
      functionRS = mViewshed.getOutputFunctionRasterSource();
      viewShedLayer = new RasterLayer(functionRS);
      mMapView.addLayer(viewShedLayer);

      mViewshed.setObserverZOffset(mObserverZOffset);
      // Set gesture used to change the position of the observer
      mMapView.setOnTouchListener(new OnTouchListenerViewshed(mMapView
          .getContext(), mMapView, mViewshed));
    } else {
      Toast toast = Toast.makeText(getApplicationContext(),
          "Raster File Not Found", Toast.LENGTH_LONG);
      toast.show();
    }

  }

  /*
   * Override com.esri.android.map.MapOnTouchListener to customize gesture
   * used to change the position of the observer and target.
   */
  private class OnTouchListenerLOS extends MapOnTouchListener {

    MapView mMap;
    LineOfSight mTask;

    public OnTouchListenerLOS(Context context, MapView map, LineOfSight task) {
      super(context, map);
      mMap = map;
      mTask = task;
    }

    @Override
    public boolean onDragPointerMove(MotionEvent from, MotionEvent to) {
      try {
        Point p = mMap.toMapPoint(to.getX(), to.getY());
        mTask.setTarget(p);
      } catch (Exception e) {
        e.printStackTrace();
      }
      return true;
    }

    @Override
    public boolean onSingleTap(MotionEvent tap) {
      try {
        Point p = mMap.toMapPoint(tap.getX(), tap.getY());
        mTask.setTarget(p);
      } catch (Exception e) {
        e.printStackTrace();
      }

      return true;
    }

    /*
     * Override method to change the observers position in calculating Line
     * of Sight. 
     * 
     * @see
     * com.esri.android.map.MapOnTouchListener#onLongPress(android.view.
     * MotionEvent)
     */
    @Override
    public void onLongPress(MotionEvent tap) {
      Point p = mMap.toMapPoint(tap.getX(), tap.getY());
      mTask.setObserver(p);

    }
  }

  /*
   * Override com.esri.android.map.MapOnTouchListener to customize gesture
   * used to change the position of the observer.
   */
  private class OnTouchListenerViewshed extends MapOnTouchListener {

    private MapView mMap;
    private Viewshed mTask;

    public OnTouchListenerViewshed(Context context, MapView map,
        Viewshed task) {
      super(context, map);
      mMap = map;
      mTask = task;
    }

    @Override
    public boolean onSingleTap(MotionEvent tap) {
      Point mapPoint = mMap.toMapPoint(tap.getX(), tap.getY());
      // clear any graphics
      mGraphicsLayer.removeAll();
      // create a graphic to represent observer position
      Graphic graphic = new Graphic(mapPoint, new SimpleMarkerSymbol(
          Color.YELLOW, 20, STYLE.CROSS));
      // add graphic to map
      mGraphicsLayer.addGraphic(graphic);
      // set observer on viewshed
      mTask.setObserver(mapPoint);

      return true;
    }

  }

}




Java Source Code List

com.arcgis.android.samples.ExportTileCacheTask.ExportTileCacheTaskSampleActivity.java
com.arcgis.android.samples.cloudportal.featureservicetablequery.MainActivity.java
com.arcgis.android.samples.cloudportal.querycloudfeatureservice.MainActivity.java
com.arcgis.android.samples.dynamiclayer.DynamicLayerRendererActivity.java
com.arcgis.android.samples.geometrysample.BufferFragment.java
com.arcgis.android.samples.geometrysample.GeometrySampleActivity.java
com.arcgis.android.samples.geometrysample.GeometryUtil.java
com.arcgis.android.samples.geometrysample.SampleListFragment.java
com.arcgis.android.samples.geometrysample.SpatialRelationshipsFragment.java
com.arcgis.android.samples.geometrysample.UnionDifferenceFragment.java
com.arcgis.android.samples.localdata.localrasterdata.EditTextUtils.java
com.arcgis.android.samples.localdata.localrasterdata.FileBrowserFragment.java
com.arcgis.android.samples.localdata.localrasterdata.HillshadeRendererParametersFragment.java
com.arcgis.android.samples.localdata.localrasterdata.MainActivity.java
com.arcgis.android.samples.localdata.localrasterdata.OnDialogDismissListener.java
com.arcgis.android.samples.localdata.localrasterdata.RendererType.java
com.arcgis.android.samples.localdata.localrasterdata.StretchParametersFragment.java
com.arcgis.android.samples.maps.basemaps.MainActivity.java
com.arcgis.android.samples.maps.fragmentmanagement.BasemapListFragment.java
com.arcgis.android.samples.maps.fragmentmanagement.MainActivity.java
com.arcgis.android.samples.maps.fragmentmanagement.MapFragment.java
com.arcgis.android.samples.maps.helloworld.MainActivity.java
com.arcgis.android.samples.maps.maplegend.LegendDialogFragment.java
com.arcgis.android.samples.maps.maplegend.MainActivity.java
com.arcgis.android.samples.maps.maprotation.Compass.java
com.arcgis.android.samples.maps.maprotation.MainActivity.java
com.arcgis.android.samples.maps.switchmaps.MainActivity.java
com.arcgis.android.samples.maps.switchmaps.MapFragment.java
com.arcgis.android.samples.milsym2525c.MainActivity.java
com.arcgis.android.samples.milsym2525c.Mil2525cMessageParser.java
com.arcgis.android.samples.oauth2sample.MapFragment.java
com.arcgis.android.samples.oauth2sample.OAuth2Sample.java
com.arcgis.android.samples.oauth2sample.UserContentActivity.java
com.arcgis.android.samples.oauth2sample.UserContentArrayAdapter.java
com.arcgis.android.samples.oauth2sample.UserContentFragment.java
com.arcgis.android.samples.oauth2sample.UserWebmaps.java
com.arcgis.android.samples.search.placesearch.MainActivity.java
com.esri.android.sample.closestfacilities.MainActivity.java
com.esri.android.samples.mbtiles.LocalMBTiles.java
com.esri.android.samples.mbtiles.MBTilesLayer.java
com.esri.android.samples.mgrsgrid.LocateMGRSActivity.java
com.esri.arcgis.android.sample.runtimegeodb.CreateRuntimeGeodatabaseActivity.java
com.esri.arcgis.android.sample.simplemap.SimpleMapActivity.java
com.esri.arcgis.android.sample.simplemap.SimpleMapFragment.java
com.esri.arcgis.android.sample.simplemap.SingleFragmentActivity.java
com.esri.arcgis.android.samples.GeoJSONEarthquakeMap.GeoJSONEarthquakeMapActivity.java
com.esri.arcgis.android.samples.PopupUICustomization.LayerQueryTask.java
com.esri.arcgis.android.samples.PopupUICustomization.MyAttachmentsView.java
com.esri.arcgis.android.samples.PopupUICustomization.MyEditAttributesAdapter.java
com.esri.arcgis.android.samples.PopupUICustomization.MyMediaAdapter.java
com.esri.arcgis.android.samples.PopupUICustomization.MyMediaView.java
com.esri.arcgis.android.samples.PopupUICustomization.MyReadOnlyAttributesAdapter.java
com.esri.arcgis.android.samples.PopupUICustomization.MyTitleView.java
com.esri.arcgis.android.samples.PopupUICustomization.PopupFragment.java
com.esri.arcgis.android.samples.PopupUICustomization.PopupUICustomizationActivity.java
com.esri.arcgis.android.samples.addcsv2graphic.AddCSVActivity.java
com.esri.arcgis.android.samples.attributeeditor.AttributeEditorActivity.java
com.esri.arcgis.android.samples.attributeeditor.AttributeItem.java
com.esri.arcgis.android.samples.attributeeditor.AttributeListAdapter.java
com.esri.arcgis.android.samples.attributeeditor.FeatureLayerUtils.java
com.esri.arcgis.android.samples.basiclicense.MainActivity.java
com.esri.arcgis.android.samples.basiclicense.MessageDialogFragment.java
com.esri.arcgis.android.samples.classbreaksrenderer.ClassBreaksRendererActivity.java
com.esri.arcgis.android.samples.featuredusergroup.FeaturedGroupsActivity.java
com.esri.arcgis.android.samples.featuredusergroup.GroupsFragment.java
com.esri.arcgis.android.samples.featuredusergroup.ItemsFragment.java
com.esri.arcgis.android.samples.featuredusergroup.MapActivity.java
com.esri.arcgis.android.samples.geometryeditor.ConfirmDiscardDialogFragment.java
com.esri.arcgis.android.samples.geometryeditor.EditFailedDialogFragment.java
com.esri.arcgis.android.samples.geometryeditor.FeatureTypeData.java
com.esri.arcgis.android.samples.geometryeditor.FeatureTypeDialogFragment.java
com.esri.arcgis.android.samples.geometryeditor.GeometryEditorActivity.java
com.esri.arcgis.android.samples.identifytask.Identify.java
com.esri.arcgis.android.samples.measure.MainActivity.java
com.esri.arcgis.android.samples.nearby.Nearby.java
com.esri.arcgis.android.samples.offlineeditor.GDBUtil.java
com.esri.arcgis.android.samples.offlineeditor.OfflineActions.java
com.esri.arcgis.android.samples.offlineeditor.OfflineEditorActivity.java
com.esri.arcgis.android.samples.offlineeditor.TemplatePicker.java
com.esri.arcgis.android.samples.offlineroutingandgeocoding.RoutingAndGeocoding.java
com.esri.arcgis.android.samples.popupinwebmapforediting.PopupInWebmapForEditing.java
com.esri.arcgis.android.samples.popupinwebmapforviewing.PopupInWebmapForViewing.java
com.esri.arcgis.android.samples.querytask.MainActivity.java
com.esri.arcgis.android.samples.routing.MyAdapter.java
com.esri.arcgis.android.samples.routing.RoutingDialogFragment.java
com.esri.arcgis.android.samples.routing.RoutingListFragment.java
com.esri.arcgis.android.samples.routing.RoutingSample.java
com.esri.arcgis.android.samples.servicearea.EditFragment.java
com.esri.arcgis.android.samples.servicearea.ServiceAreaSample.java
com.esri.arcgis.android.samples.standardlicense.MainActivity.java
com.esri.arcgis.android.samples.standardlicense.MessageDialogFragment.java
com.esri.arcgis.android.samples.standardlicenseoffline.MainActivity.java
com.esri.arcgis.android.samples.standardlicenseoffline.MessageDialogFragment.java
com.esri.arcgis.android.samples.uniquevaluerenderer.UniqueValueRendererSampleActivity.java
com.esri.arcgis.android.samples.viewshed.Viewshed.java
com.esri.arcgis.android.samples.wmslayer.MainActivity.java
com.esri.arcgis.samples.offlineanalysis.MainActivity.java