Android Open Source - arcgis-runtime-samples-android Viewshed






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
 *// w w  w. ja v  a 2 s  . c om
 * 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.android.samples.viewshed;

import java.util.ArrayList;
import java.util.Timer;
import java.util.TimerTask;

import android.app.Activity;
import android.app.ProgressDialog;
import android.graphics.Color;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.os.Handler.Callback;
import android.os.Message;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.Toast;

import com.esri.android.map.GraphicsLayer;
import com.esri.android.map.MapView;
import com.esri.android.map.event.OnSingleTapListener;
import com.esri.core.geometry.Geometry;
import com.esri.core.geometry.Point;
import com.esri.core.map.Graphic;
import com.esri.core.symbol.SimpleFillSymbol;
import com.esri.core.symbol.SimpleMarkerSymbol;
import com.esri.core.symbol.SimpleMarkerSymbol.STYLE;
import com.esri.core.tasks.ags.geoprocessing.GPFeatureRecordSetLayer;
import com.esri.core.tasks.ags.geoprocessing.GPLinearUnit;
import com.esri.core.tasks.ags.geoprocessing.GPParameter;
import com.esri.core.tasks.ags.geoprocessing.GPResultResource;
import com.esri.core.tasks.ags.geoprocessing.Geoprocessor;

/**
 * This sample application illustrates the usage of Geoprocessing. It allows the
 * user to select a point on the map and see all the areas that are visible
 * within the chosen distance.
 * 
 */

public class Viewshed extends Activity {

  protected static final int CLOSE_LOADING_WINDOW = 0;
  protected static final int CANCEL_LOADING_WINDOW = 1;
  MapView map = null;
  private ArrayList<GPParameter> params;
  Geoprocessor gp;
  GraphicsLayer gLayer;
  ProgressDialog dialog = null;
  Timer cancelViewShed = new Timer();
  Point mappoint;
  int gId=0;
  
  Handler uiHandler = new Handler(new Callback() {
    @Override
    public boolean handleMessage(final Message msg) {
      switch (msg.what) {
      case CLOSE_LOADING_WINDOW:
        if (dialog != null) {
          dialog.dismiss();
        }
        cancelViewShed.cancel();
        break;
      case CANCEL_LOADING_WINDOW:
        if (dialog != null) {
          dialog.dismiss();
        }
        Toast toast = Toast.makeText(Viewshed.this,
            "ViewShed too long, canceled", Toast.LENGTH_SHORT);
        toast.show();
        break;
      }
      return false;
    }

  });

  /**
   * Called when the activity is first created.
   * 
   * @param savedInstanceState
   *            Bundle
   */
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    map = (MapView) findViewById(R.id.map);
    
    // Create a graphics layer for Viewshed drawing
    gLayer = new GraphicsLayer();
    map.addLayer(gLayer);
    // Map Buttons

    Toast.makeText(this, "Single tap on the map", Toast.LENGTH_SHORT)
        .show();


    map.setOnSingleTapListener(new OnSingleTapListener() {
      private static final long serialVersionUID = 1L;

      @Override
      public void onSingleTap(final float x, final float y) {
        if(mappoint!=null){
          gLayer.removeGraphic(gId);
        }
        mappoint = map.toMapPoint(x, y);
        Graphic g = new Graphic(mappoint, new SimpleMarkerSymbol(
            Color.RED, 10, STYLE.CIRCLE));
        gId=gLayer.addGraphic(g);
      }
    });


  }

  @Override
  public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.menu_layout, menu);
    return super.onCreateOptionsMenu(menu);

  }
  /**
   * Clear all graphics from the graphics layer. The method is called when
   * the trash can button is clicked by the user.
   */

  @Override
  public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
    case R.id.go:
      if(gId==0){
        Toast.makeText(this, "Single Tap on the Map", Toast.LENGTH_SHORT).show();
        return false;
      }
      start(mappoint);
      return true;
    case R.id.delete:
      gLayer.removeAll();
      gId=0;
      return true;
    default:
      return super.onOptionsItemSelected(item);

    }
  }

  class ViewShedQuery extends
      AsyncTask<ArrayList<GPParameter>, Void, GPParameter[]> {

    GPParameter[] outParams = null;

    /**
     * Method onPostExecute.
     * 
     * @param result
     *            GPParameter[]
     */
    @Override
    protected void onPostExecute(GPParameter[] result) {
      if (result == null)
        return;
      for (int i = 0; i < result.length; i++) {
        if (result[i] instanceof GPFeatureRecordSetLayer) {

          GPFeatureRecordSetLayer fsl = (GPFeatureRecordSetLayer) result[i];
          for (Graphic feature : fsl.getGraphics()) {
            Graphic g = new Graphic(feature.getGeometry(),
                new SimpleFillSymbol(Color.CYAN));
            gLayer.addGraphic(g);
          }
        }
      }
      uiHandler.sendEmptyMessage(CLOSE_LOADING_WINDOW);
    }

    /**
     * Method doInBackground.
     * 
     * @param params1
     *            ArrayList<GPParameter>[]
     * @return GPParameter[]
     */
    @Override
    protected GPParameter[] doInBackground(
        ArrayList<GPParameter>... params1) {

      gp = new Geoprocessor(
          "http://sampleserver1.arcgisonline.com/ArcGIS/rest/services/Elevation/ESRI_Elevation_World/GPServer/Viewshed");
      /*
       * API v1.1 requires SpatialReference parameter
       */
      gp.setOutSR(map.getSpatialReference());
      try {
        GPResultResource rr = gp.execute(params1[0]);
        outParams = rr.getOutputParameters();
      } catch (Exception e) {
        e.printStackTrace();
      }
      return outParams;
    }
  }

  /**
   * 1) Create the GP object by passing it the url. 2) Create the input
   * parameters and add to the GPParameter object. 3) Pass these params to the
   * execute method.
   * 
   * @param mPoint
   *            - point selected on the map by the user
   */
  @SuppressWarnings("unchecked")
  public void start(Point mPoint) {
    // First input parameter
    GPFeatureRecordSetLayer gpf = new GPFeatureRecordSetLayer(
        "Input_Observation_Point");
    gpf.setSpatialReference(map.getSpatialReference());
    gpf.setGeometryType(Geometry.Type.POINT);
    // Add the point selected by the user
    Graphic f = new Graphic(mPoint, new SimpleMarkerSymbol(Color.RED, 25,
        STYLE.DIAMOND));
    gpf.addGraphic(f);

    // Second input parameter
    GPLinearUnit gpl = new GPLinearUnit("Viewshed_Distance");
    gpl.setUnits("esriMeters");
    gpl.setDistance(8046.72);

    // Add params
    params = new ArrayList<GPParameter>();
    params.add(gpf);
    params.add(gpl);

    try {
      dialog = ProgressDialog.show(Viewshed.this, "",
          "Calculating Viewshed ...", true, true);
      new ViewShedQuery().execute(params);
      cancelViewShed = new Timer();
      cancelViewShed.schedule(new TimerTask() {

        @Override
        public void run() {
          uiHandler.sendEmptyMessage(CANCEL_LOADING_WINDOW);
        }
      }, 60000);
    } catch (Exception e) {
      e.printStackTrace();
    }
  }

  @Override
  protected void onPause() {
    super.onPause();
    map.pause();
  }

  @Override
  protected void onResume() {
    super.onResume();
    map.unpause();
  }

}




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