How to click in point of map and show attributes (like sample)?

1904
1
07-03-2017 01:16 PM
PedroFrancisco_de_Sousa_Neto
New Contributor II

I tried a query and on web-query for tests purpose and it's working, like: http://is.bramar.net/arcgis/rest/services/SocioEconomics/OutrogasAESA_FS/FeatureServer/0/query?where... 

In android it is not working.

My code:

[...]

compile 'com.esri.arcgis.android:arcgis-android:10.2.9'
[...]

featureLayer = new ArcGISFeatureLayer(

"http://is.bramar.net/arcgis/rest/services/SocioEconomics/OutrogasAESA_FS/FeatureServer"

, MODE.ONDEMAND);
[...]

and

mapView.setOnSingleTapListener(new OnSingleTapListener() {

    public void onSingleTap(float x, float y) {

        Geometry geom = (GeometryEngine.buffer(pointClicked, mapView.getSpatialReference(), 100, null));

        // build a query to select the clicked feature
        Query query = new Query();
        query.setOutFields(new String[]{"*"});
        query.setSpatialRelationship(SpatialRelationship.ENVELOPE_INTERSECTS);
        query.setGeometry(geom);
        query.setInSpatialReference(mapView.getSpatialReference());

        featureLayer.selectFeatures(query, ArcGISFeatureLayer.SELECTION_METHOD.NEW, new CallbackListener<FeatureSet>() {

            // handle any errors
            public void onError(Throwable e) {
                Log.d(TAG, "Select Features Error" + e.getLocalizedMessage());
            }

            public void onCallback(FeatureSet queryResults) {

                int size = 0;

                if (!Utils.isNullOrEmpty(queryResults.getGraphics())) {
                    size = queryResults.getGraphics().length;
                }

                Utils.log("Size: " + size);

                if (size > 0) {

                    Log.d(TAG, "Feature found id=" + queryResults.getGraphics()[0].getAttributeValue(featureLayer.getObjectIdField()));

                    // set new data and notify adapter that data has changed
                    listAdapter.setFeatureSet(queryResults);
                    listAdapter.notifyDataSetChanged();

                    // This callback is not run in the main UI thread. All GUI
                    // related events must run in the UI thread,
                    // therefore use the Activity.runOnUiThread() method. See
                    // http://developer.android.com/reference/android/app/Activity.html#runOnUiThread(java.lang.Runnable)
                    // for more information.
                    AttributeEditorActivity.this.runOnUiThread(new Runnable() {

                        public void run() {

                            // show the editor dialog.
                            showDialog(ATTRIBUTE_EDITOR_DIALOG_ID);

                        }
                    });
                }
            }
        });
    }
});
Tags (1)
0 Kudos
1 Reply
PedroFrancisco_de_Sousa_Neto
New Contributor II

I fixed this. Here is the code for who wants do the same.

mapView.setOnSingleTapListener(new OnSingleTapListener() {
    @Override
    public void onSingleTap(float v, float v1) {
        Point point = mapView.toMapPoint(v, v1);
        new AsyncTaskDetalhePontoNoMapa(getURLTipoMapa(), point, mapView, arcGISDynamicMapServiceLayer.getSpatialReference(), isOutorga(), ActivityMapa.this).execute();
    }
});

Finally, my asynctask:

import android.os.AsyncTask;

import com.esri.android.map.MapView;
import com.esri.core.geometry.Envelope;
import com.esri.core.geometry.Point;
import com.esri.core.geometry.SpatialReference;
import com.esri.core.io.UserCredentials;
import com.esri.core.map.FeatureSet;
import com.esri.core.map.Graphic;
import com.esri.core.tasks.ags.query.Query;
import com.esri.core.tasks.ags.query.QueryTask;

import org.codehaus.jackson.map.ObjectMapper;

import app.i3systems.bramar.ffdc.interfaces.Listable;
import app.i3systems.bramar.ffdc.interfaces.PontoClicavel;
import app.i3systems.bramar.ffdc.model.Outorga;
import app.i3systems.bramar.ffdc.model.Poco;
import app.i3systems.bramar.ffdc.utils.Utils;
import app.i3systems.bramar.ffdc.utils.UtilsSharedPreferences;

/**
 * Created by pedrofsn on 07-07-17.
 */

public class AsyncTaskDetalhePontoNoMapa extends AsyncTask<Point, Void, String> {

    private Point clickPoint;
    private double resolution;
    private String url;
    private SpatialReference spatialReference;
    private boolean isOutorga;
    private PontoClicavel callback;

    public AsyncTaskDetalhePontoNoMapa(String url, Point clickPoint, MapView mapView, SpatialReference spatialReference, boolean isOutorga, PontoClicavel callback) {
        this.url = url;
        this.callback = callback;
        this.isOutorga = isOutorga;
        this.clickPoint = clickPoint;
        this.spatialReference = spatialReference;
        this.resolution = mapView.getResolution();
    }

    @Override
    protected String doInBackground(Point... params) {
        double tolerance = 100;
        double resolutionWithTolerance = tolerance * resolution;
        Envelope envelope = new Envelope(clickPoint, resolutionWithTolerance, resolutionWithTolerance);

        try {
            UserCredentials userCredentials = new UserCredentials();
            userCredentials.setUserAccount(UtilsSharedPreferences.getLogin(), UtilsSharedPreferences.getSenha());
            QueryTask queryTask = new QueryTask(url + "/0", userCredentials);

            Query query = new Query();
            query.setInSpatialReference(spatialReference);
            query.setOutSpatialReference(spatialReference);
            query.setGeometry(envelope);
            query.setMaxFeatures(30);
            query.setOutFields(new String[]{"*"});

            FeatureSet featureSet = queryTask.execute(query);

            Graphic[] resultGraphic = featureSet.getGraphics();

            if (!Utils.isNullOrEmpty(resultGraphic) && resultGraphic.length > 0) {
                ObjectMapper mapper = new ObjectMapper();
                Listable listable = mapper.convertValue(resultGraphic[0].getAttributes(), isOutorga ? Outorga.class : Poco.class);
                return listable.toString();
            }

        } catch (Exception e) {
            e.printStackTrace();
        }

        Utils.log("Coordenadas : " + clickPoint.getX() + ", " + clickPoint.getY());
        return null;
    }

    @Override
    protected void onPostExecute(String string) {
        super.onPostExecute(string);
        callback.onDetalhesObtidosDoPontoNoMapa(clickPoint, string);
    }
}