AnsweredAssumed Answered

Problem with Geodatabase Query code

Question asked by ssiddiqui on Mar 16, 2015

  Hi ,

I am new in Android Development so i am not able to figure out the problem,

trying to get the result on singletab using offline geodatabase. Following are the code

Its not returning the result and control is not going inside the onCallback method.

Please help me in resolving this .

 

@Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

        // create the path to local tpk

        landDataFile = Environment.getExternalStorageDirectory();

        offlineDataSDCardDirName = this.getResources().getString(R.string.offline_dir);

        filename = this.getResources().getString(R.string.local_tpk);

        fileNameGeoDatabase = this.getResources().getString(R.string.local_geodatabase);

        // create the url

        String basemap = landDataFile + File.separator + offlineDataSDCardDirName + File.separator + filename;

        String geodatabasepath = landDataFile + File.separator + offlineDataSDCardDirName + File.separator + fileNameGeoDatabase;

        // for accessing the featurelayer using geodatabase

        try {

            geodatabase = new Geodatabase(geodatabasepath);

        } catch (FileNotFoundException e) {

            e.printStackTrace();

        }

        geodatabaseFeatureTable = geodatabase.getGeodatabaseFeatureTableByLayerId(1);

        // Create feature layer and add the map into it.

        featureLayer = new FeatureLayer(geodatabaseFeatureTable);

       // featureLayer.getSelectedFeatures();

        featureLayer.setSelectionColor(12828800);  // added for identity

        featureLayer.setSelectionColorWidth(30);    //added for identity

        // create the mapview

        mMapView = (MapView) findViewById(R.id.map);

        // create the local tpk

        localTiledLayer = new ArcGISLocalTiledLayer(basemap);

        // add the layer

        mMapView.addLayer(localTiledLayer);

        mMapView.addLayer(featureLayer);

        Envelope env = new Envelope(398646.635989773, 2941161.63084675, 399056.211808925, 2941234.25911701);

        mMapView.setExtent(env);

        // enable panning over date line

        mMapView.enableWrapAround(true);

        // set Esri logo

        mMapView.setEsriLogoVisible(false);

        mMapView.setOnStatusChangedListener(new OnStatusChangedListener() {

            public void onStatusChanged(Object source, STATUS status) {

                // Check to see if map has successfully loaded

                if ((source == mMapView) && (status == STATUS.INITIALIZED)) {

                    // Set the flag to true

                    mIsMapLoaded = true;

                }

            }

        });

        mMapView.setOnSingleTapListener(new OnSingleTapListener() {

            @Override

            public void onSingleTap(float x, float y) {

              //  geoDataBaseQuery();

                // convert event into screen click

                featureLayer.clearSelection();

                pointClicked = mMapView.toMapPoint(x, y);

                QueryParameters q = new QueryParameters();

                // optional

                q.setWhere("1=1");

                q.setOutSpatialReference(mMapView.getSpatialReference());

                q.setReturnGeometry(true);

                q.setSpatialRelationship(SpatialRelationship.INTERSECTS);

                q.setGeometry(pointClicked);

                q.setInSpatialReference(mMapView.getSpatialReference());

           //    q.setSpatialRelationship(SpatialRelationship.INTERSECTS);

           //     featureLayer.selectFeatures(q,SelectionMode.NEW,callback);

                featureLayer.selectFeatures(q,SelectionMode.NEW, new CallbackListener<FeatureResult>() {

                    @Override

                    public void onError(Throwable ex) {

                        // Highlight errors to the user.

                        showToast("Error querying FeatureServiceTable");

                    }

                    @Override

                    public void onCallback(FeatureResult objs) {

                        // If there are no query results, inform user.

                        if (objs.featureCount() < 1) {

                            showToast("No results");

                            return;

                        }

                        else {

                            // Report number of results to user.

                            showToast("Found " + objs.featureCount() + " features.");

 

 

                            // Iterate the results and select each feature.

                            for (Object objFeature : objs) {

                                Feature feature = (Feature) objFeature;

                                featureLayer.selectFeature(feature.getId());

                            }

                        }

                    }

                });

            }

        });

    }

Outcomes