Android Open Source - arcgis-runtime-samples-android Simple Map Fragment






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

package com.esri.arcgis.android.sample.simplemap;
//ww w .j a  v a 2s  .  c  o  m
import java.util.Map;

import android.app.Fragment;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.esri.android.map.MapView;
import com.esri.android.map.event.OnSingleTapListener;
import com.esri.android.map.event.OnStatusChangedListener;
import com.esri.android.toolkit.geocode.GeocodeHelper;
import com.esri.android.toolkit.map.MapViewHelper;
import com.esri.core.geometry.Point;
import com.esri.core.map.CallbackListener;
import com.esri.core.tasks.geocode.Locator;
import com.esri.core.tasks.geocode.LocatorReverseGeocodeResult;

public class SimpleMapFragment extends Fragment {

  // Keys and member variables to save address results.
  private static final String KEY_MAPSTATE = "mapState";
  String mMapState = null;

  private static final String KEY_RESULT_TITLE = "title";
  String mResultTitle = null;

  private static final String KEY_RESULT_SNIPPET = "snippet";
  String mResultSnippet = null;

  private static final String KEY_RESULT_X = "locationX";
  double mResultX = Double.NaN;

  private static final String KEY_RESULT_Y = "locationY";
  double mResultY = Double.NaN;

  // The MapView in the fragment layout, and a MapViewHelper that will be used 
  // with this map.
  MapView mMapView;

  MapViewHelper mMapViewHelper;

  // Locator to be used to perform a reverse geocode, finding an address for a
  // set of coordinates.
  Locator mLocator;

  // Fields from the reverse geocode result shown in the result graphic callout.
  final String[] mResultCalloutFields = { "Address", "City", "Region", "Postal" };

  // A drawable icon to draw reverse geocoded locations.
  Drawable mIcon;

  @Override
  public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);

    // Initialize online locator.
    mLocator = Locator.createOnlineLocator();

    // Create a drawable icon to use when
    mIcon = getResources().getDrawable(R.drawable.route_destination);

  }

  @Override
  public View onCreateView(LayoutInflater inflater, ViewGroup container,
      Bundle savedInstanceState) {
    View v = inflater.inflate(R.layout.fragment_simple_map, container, false);

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

    // Check for app state saved if the activity and fragment were destroyed.
    if (savedInstanceState != null) {
      mMapState = savedInstanceState.getString(KEY_MAPSTATE, null);
      mResultTitle = savedInstanceState.getString(KEY_RESULT_TITLE, null);
      mResultSnippet = savedInstanceState.getString(KEY_RESULT_SNIPPET, null);
      mResultX = savedInstanceState.getDouble(KEY_RESULT_X, Double.NaN);
      mResultY = savedInstanceState.getDouble(KEY_RESULT_Y, Double.NaN);

      // Too early to set map state here, as the map is not initialized;
      // at this point restoreState would be ignored.
    }

    // Set the MapView to wrap around.
    mMapView.enableWrapAround(true);

    // Set the Esri logo to be visible on the map.
    mMapView.setEsriLogoVisible(true);

    // Create a MapView Helper for use in MapView listeners set below.
    mMapViewHelper = new MapViewHelper(mMapView);
    
    mMapView.setOnStatusChangedListener(new OnStatusChangedListener() {
      private static final long serialVersionUID = 1L;

      @Override
      public void onStatusChanged(Object source, STATUS status) {

        if ((status == STATUS.INITIALIZED) && (source instanceof MapView )) {

          // When map is initialized, restore the map state (center and resolution)
          // if one was saved.
          if ((mMapState != null) && (!mMapState.isEmpty())) {
            mMapView.restoreState(mMapState);
          }
          else {
            // If there was no saved state then set default resolution and center
           mMapView.zoomToResolution(new Point(-1.3046152655046357E7, 
               4036221.883444177), 1.53);
          }

          if ((mResultTitle != null) && (!mResultTitle.isEmpty())) {
            // Additionally, if a tapped location was saved, then add this to 
            // the map as a graphic.
            mMapViewHelper.addMarkerGraphic(mResultY, mResultX, mResultTitle,
                mResultSnippet, null, null, false, 0);
            
          } else {
            // If there is no location saved, then add a graphic to represent 
            // ESRI Headquarters, using the MapViewHelper.addMarkerGraphic
            // simplification API by specifying a latitude and longitude, a
            // drawable icon, and the title and content of a callout that is
            // shown when the icon is tapped.
            mMapViewHelper.addMarkerGraphic(34.056695, -117.195693, "ESRI",
                "World Headquarters", null, mIcon, false, 0);
          }
        }
        
      }
    });
    
    // Set a listener for a single tap on the map.
    mMapView.setOnSingleTapListener(new OnSingleTapListener() {
      private static final long serialVersionUID = 1L;

      @Override
      public void onSingleTap(float x, float y) {
        // Remove all previous graphics added by the MapViewHelper.
        mMapViewHelper.removeAllGraphics();
        mResultTitle = mResultSnippet = null;
        mResultX = mResultY = Double.NaN;

        // Show address at the tapped point, by passing in screen coordinates,
        // the Locator created above, the MapViewHelper, and the Fields to be 
        // shown in the result. This uses a default symbol.
        GeocodeHelper.showAddress(x, y, mLocator, mMapViewHelper, null,
            mResultCalloutFields,
            new CallbackListener<LocatorReverseGeocodeResult>() {

              @Override
              public void onError(Throwable e) {
                // Indicate that there was a problem with this reverse geocode.
                Toast.makeText(getActivity(), "Error", Toast.LENGTH_LONG)
                    .show();
              }

              @Override
              public void onCallback(LocatorReverseGeocodeResult objs) {
                
                // In this callback, the reverse geocode has found a location;
                // GeocodeHelper adds the result location to the Map, but the 
                // result is preserved here for use if the fragment is destroyed.
                mResultX = objs.getLocation().getX();
                mResultY = objs.getLocation().getY();

                // To save the callout contents, the specified address fields 
                // are concatenated; GeocodeHelper uses these attributes as 
                // the Title in the callout.
                StringBuilder address = new StringBuilder();
                for (String field : mResultCalloutFields) {
                  Map<String, String> resultFields = objs.getAddressFields();
                  if (resultFields.containsKey(field)) {
                    address.append(resultFields.get(field) + " ");
                  }
                }
                mResultTitle = address.toString().trim();
              }
            });
      }
    });

    // Return the inflated view.
    return v;

  }

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

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

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

  
  @Override
  public void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);
    
    // Save the current state of the map before the activity is destroyed. 
    outState.putString(KEY_MAPSTATE, mMapState);
    outState.putString(KEY_RESULT_TITLE, mResultTitle);
    outState.putString(KEY_RESULT_SNIPPET, mResultSnippet);
    outState.putDouble(KEY_RESULT_X, mResultX);
    outState.putDouble(KEY_RESULT_Y, mResultY);
  }

}




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