erebr

Crashing on .getUnit with custom spatial reference

Discussion created by erebr on Jun 21, 2012
I'm using the ArcGIS for android SDK and at my job we are creating an app that, amongst other things, will get your location, plot it on a map, and display it. This functionality worked fine when pointed at ESRI's sample map service. But when pointed at my workplace's custom map service, it doesn't work.

The code:
// Show current location
mapView.setOnStatusChangedListener(new OnStatusChangedListener() {

  private static final long serialVersionUID = 1L;
  boolean firstTime = true;
  public void onStatusChanged(Object source, STATUS status) {
   if (source == mapView && status == STATUS.INITIALIZED) {
    LocationService ls = mapView.getLocationService();
    ls.setAutoPan(false);
    ls.setLocationListener(new LocationListener() {

   

     // Zooms to the current location when first GPS fix
     // arrives.
     public void onLocationChanged(Location loc) {
    
       graphicsLayer.removeAll(); //removes any existing current location icon.
       double locy = loc.getLatitude();
       double locx = loc.getLongitude();
       Point wgspoint = new Point(locx, locy);
       Point mapPoint = (Point) GeometryEngine
         .project(wgspoint,
           SpatialReference.create(4326),
           mapView.getSpatialReference());

       Unit mapUnit = mapView.getSpatialReference()
         .getUnit();
       double zoomWidth = Unit.convertUnits(
         SEARCH_RADIUS,
         Unit.create(LinearUnit.Code.MILE_US),
         mapUnit);
       Envelope zoomExtent = new Envelope(mapPoint,
         zoomWidth, zoomWidth);
       if (firstTime) { // Ensures the extent only shifts the first time location lock happens
         firstTime = false;
       mapView.setExtent(zoomExtent);
       }
      
       //*****Plot the current location on the map*****//

         Point point = (Point)GeometryEngine.project(wgspoint,SpatialReference.create(4326), mapView.getSpatialReference());
         Symbol symbol = null;
         symbol = targetIcon;
        
         HashMap<String, Object> attrMap = new HashMap<String, Object>();

         graphicsLayer.addGraphic(new Graphic(point, symbol, attrMap, null));
      }

     //}

     public void onProviderDisabled(String arg0) {

     }

     public void onProviderEnabled(String arg0) {
     }

     public void onStatusChanged(String arg0, int arg1,
       Bundle arg2) {

     }
    });
    ls.start();

   }

  }
});


The code that is failing:

Unit mapUnit = mapView.getSpatialReference()
.getUnit();// the debugger points to this line, not the getspatialreference line, as the source of the crash, and getspatialreference is used right above it in the code without apparently crashing.


We have a custom spatial reference. This is how it is listed when navigating to the map service in a browser.
Spatial Reference: PROJCS["NAD83[HARN] / Arizona Central [ft]",GEOGCS["NAD83[HARN]",DATUM["NAD83 [High Accuracy Regional Network] [EPSG ID 6152]",SPHEROID["GRS 1980 [EPSG ID 7019]",6378137.0,298.257222101]],PRIMEM["Greenwich",0.0],UNIT["Degree",0.01745329251994328]],PROJECTION["Transverse_Mercator"],PARAMETER["False_Easting",700000.0],PARAMETER["False_Northing",0.0],PARAMETER["Central_Meridian",-111.9166666666667],PARAMETER["Scale_Factor",0.9999],PARAMETER["Latitude_Of_Origin",31.0],UNIT["foot",0.3048]]


When I look at the spatial reference for the sample map, it is just this four digit EPSG ID number. Our spatial reference is the long string listed earlier. Is there something wrong with it? My boss tells me that our unit is feet, but that we should not have to hardcode that in (as, indeed, the original sample did not have it hardcoded in), because it should be able to be pulled from the map service's info. The android eclipse debugger has values stored for all the variables up until the line that says .getUnit(), and I do not know why it is crashing here. The sample code had a tiled map layer and a dynamic and feature layers, our app only needs the tiled map layer, so I have commented out the others. Is .getUnit incompatible with ArcGISTiledMapsServiceLayers? Is our spatial reference formated wrong? Something else? Can anyone help?

Outcomes