Android Open Source - arcgis-runtime-samples-android Service Area Sample






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
 */*ww w .  jav 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.android.samples.servicearea;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.graphics.Color;
import android.os.AsyncTask;
import android.os.Bundle;
import android.widget.EditText;
import android.widget.TextView;
import android.app.ActionBar;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
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.ags.ArcGISTiledMapServiceLayer;
import com.esri.android.map.event.OnSingleTapListener;
import com.esri.core.geometry.GeometryEngine;
import com.esri.core.geometry.Point;
import com.esri.core.geometry.SpatialReference;
import com.esri.core.map.Graphic;
import com.esri.core.symbol.SimpleFillSymbol;
import com.esri.core.tasks.na.NAFeaturesAsFeature;
import com.esri.core.tasks.na.ServiceAreaParameters;
import com.esri.core.tasks.na.ServiceAreaResult;
import com.esri.core.tasks.na.ServiceAreaTask;

/**
 * A service area is a region that encompasses all accessible streets. 
 * For instance, the 5-minute service area for a point on a network includes all 
 * the streets that can be reached within five minutes from that point.
 * The user has a leverage to select breakpoints from the action bar to perform
 * the search as desired by the user. It uses the Async Task to perform the
 * activity and return the results via a graphics service layer on the
 * map.Implemented Fragments to add the actions to set the break points.
 * 
 */
public class ServiceAreaSample extends Activity implements
    EditFragment.OnDialogClickedListener {

  MapView mMapView = null;
  ArcGISTiledMapServiceLayer baseMap = null;

  // Graphics layer for displaying the service area polygons
  GraphicsLayer serviceAreaLayer = null;

  // Spatial references used for projecting points
  final SpatialReference wm = SpatialReference.create(102100);
  final SpatialReference egs = SpatialReference.create(4326);

  // Three text boxes for specifying the break values
  EditText break1, break2, break3;

  // The action bar
  ActionBar action = null;

  // Dialog to check progress for the service task
  static ProgressDialog dialog = null;
  static AlertDialog.Builder alertDialogBuilder = null;
  static AlertDialog alertDialog = null;

  // Fragment Manager to add interfaces for fragments
  FragmentManager fm = getFragmentManager();

  // Adds Fragments concerned to the Fragment Manager
  FragmentTransaction trans = fm.beginTransaction();
  
  // Edit Fragment
    EditFragment editFragment = new EditFragment();
  
  // default value
  double breakValue1 = 1.0, breakValue2 = 2.0, breakValue3 = 3.0;

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

    // Retrieve the map and initial extent from XML layout
    mMapView = (MapView) findViewById(R.id.map);

    // Add the service area graphic layer (shows the SA polygons)
    serviceAreaLayer = new GraphicsLayer();
    mMapView.addLayer(serviceAreaLayer);

    // Adding the action bar
    action = getActionBar();

    /**
     * On single tapping the map, calculate the three service area polygons
     * defined by our three break value EditTexts, using the location
     * clicked as the source facility
     */
    mMapView.setOnSingleTapListener(new OnSingleTapListener() {
      private static final long serialVersionUID = 1L;

      public void onSingleTap(float x, float y) {
        // retrieve the user clicked location
        final Point location = mMapView.toMapPoint(x, y);

        SolveServiceArea solveArea = new SolveServiceArea();
        solveArea.execute(location);

      }
    });
  }

  /**
   * Creates the menu items on the action bar
   */
  public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.action, menu);
    return super.onCreateOptionsMenu(menu);
  }

  /**
   * Action for the item Selected from the menu.
   */
  public boolean onOptionsItemSelected(MenuItem item) {

    switch (item.getItemId()) {

    case R.id.action_refresh:
      Toast.makeText(this, getString(R.string.action_refresh),
          Toast.LENGTH_LONG).show();
      serviceAreaLayer.removeAll();
      return true;

    case R.id.set_breaks:
      fm = getFragmentManager();
      trans = fm.beginTransaction();
      editFragment = new EditFragment();

      trans.add(editFragment, "Set Breaks");
      trans.commit();

      Toast.makeText(this, getString(R.string.set_breaks),
          Toast.LENGTH_LONG).show();
      return true;
    }
    return false;
  }

  /**
   * Create a TextView containing the message
   * 
   * @param text
   *            The text view's content
   * @return The TextView
   */
  TextView message(String text) {

    final TextView msg = new TextView(this);
    msg.setText(text);
    msg.setTextSize(12);
    msg.setTextColor(Color.BLACK);
    return msg;

  }

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

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

  /**
   * The Task perform on the service layer has 3 methods to perform each time
   * i.e. Pre-Execute, Do-In-Background and the Post-Execute.
   * 
   * @author Esri Android Team
   */
  class SolveServiceArea extends AsyncTask<Point, Void, ServiceAreaResult> {

    protected void onPreExecute() {
      dialog = ProgressDialog.show(ServiceAreaSample.this, "",
          "Calculating the extent of Service Area");
    }

    @Override
    protected ServiceAreaResult doInBackground(Point... params) {
      Point startLocation = params[0];

      try {
        // Start building up service area parameters
        ServiceAreaParameters sap = new ServiceAreaParameters();
        NAFeaturesAsFeature nfaf = new NAFeaturesAsFeature();
        // Convert point to EGS (decimal degrees)
        Point p = (Point) GeometryEngine
            .project(startLocation, wm, egs);
        nfaf.addFeature(new Graphic(p, null));
        sap.setFacilities(nfaf);
        // Set the service area output SR to our map service's SR
        sap.setOutSpatialReference(wm);

        // Set the default break values with our entered values

        sap.setDefaultBreaks(new Double[] { breakValue1, breakValue2,
            breakValue3 });

        // Create a new service area task pointing to an NAService
        // (null credentials -> free service)
        ServiceAreaTask sat = new ServiceAreaTask(
            ServiceAreaSample.this.getResources().getString(
                R.string.naservice_url), null);

        // Solve the service area and retrieve the result.
        ServiceAreaResult saResult = sat.solve(sap);
        return saResult;

      } catch (Exception e) {

        e.printStackTrace();
        mMapView.getCallout().show(startLocation,
            message("Exception occurred"));
        return null;
      }

    }

    protected void onPostExecute(ServiceAreaResult result) {
      dialog.dismiss();

      ServiceAreaResult saResult = result;

      if (saResult != null) {
        // Symbol for the smallest service area polygon
        SimpleFillSymbol smallSymbol = new SimpleFillSymbol(Color.GREEN);
        smallSymbol.setAlpha(128);
        // Symbol for the medium service area polygon
        SimpleFillSymbol mediumSymbol = new SimpleFillSymbol(
            Color.YELLOW);
        mediumSymbol.setAlpha(128);
        // Symbol for the largest service area polygon
        SimpleFillSymbol largeSymbol = new SimpleFillSymbol(Color.RED);
        largeSymbol.setAlpha(128);

        // Create and add the service area graphics to the service
        // area Layer
        Graphic smallGraphic = new Graphic(
            saResult.getServiceAreaPolygons().getGraphics()[2]
                .getGeometry(),
            smallSymbol);
        Graphic mediumGraphic = new Graphic(
            saResult.getServiceAreaPolygons().getGraphics()[1]
                .getGeometry(),
            mediumSymbol);
        Graphic largeGraphic = new Graphic(
            saResult.getServiceAreaPolygons().getGraphics()[0]
                .getGeometry(),
            largeSymbol);
        serviceAreaLayer.addGraphics(new Graphic[] { smallGraphic,
            mediumGraphic, largeGraphic });
        // Zoom to the extent of the service area polygon with a
        // padding
        mMapView.setExtent(largeGraphic.getGeometry(), 50);
      } else {
        // send response to user
        alertDialogBuilder = new AlertDialog.Builder(
            ServiceAreaSample.this);
        alertDialogBuilder.setTitle("Query Response");
        alertDialogBuilder.setMessage(ServiceAreaSample.this
            .getResources().getString(R.string.sa_null_response));
        alertDialogBuilder.setCancelable(true);
        // create alert dialog
        alertDialog = alertDialogBuilder.create();
        alertDialog.show();
      }

    }

  }

  /**
   * Invoked after you tap OK on the dialog.
   */
  @Override
  public void onDialogClicked(double a1, double a2, double a3) {
    // TODO Auto-generated method stub
    FragmentTransaction trans1 = fm.beginTransaction();
    trans1.remove(fm.findFragmentByTag("Set Breaks"));
    trans1.commit();

    breakValue1 = a1;
    breakValue2 = a2;
    breakValue3 = a3;

  }

}




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