Select to view content in your preferred language

help with displaying popups on map features in app?

5151
10
Jump to solution
10-23-2017 11:00 AM
CoraColeman
Regular Contributor

Below is my current implementation of this method for displaying popups from the latest ArcGIS Runtime for Android SDK, however I have come to a stopping point where you see below:

 showPopup(identifiedPopup, screenPoint); 

While I have been able to print out popups to the logcat console, I am still unsure how to go about actually configuring their display in my app past this info? I am getting a bit confused looking at the API code for popups here, and am wondering if I need to use the methods mentioned there in order to do this after I grab the popup from my layer? 

import android.content.Context;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;

import com.esri.arcgisruntime.concurrent.ListenableFuture;
import com.esri.arcgisruntime.data.Feature;
import com.esri.arcgisruntime.layers.FeatureLayer;
import com.esri.arcgisruntime.mapping.ArcGISMap;
import com.esri.arcgisruntime.mapping.GeoElement;
import com.esri.arcgisruntime.mapping.popup.Popup;
import com.esri.arcgisruntime.mapping.view.DefaultMapViewOnTouchListener;
import com.esri.arcgisruntime.mapping.view.IdentifyLayerResult;
import com.esri.arcgisruntime.mapping.view.MapView;
import com.esri.arcgisruntime.portal.Portal;
import com.esri.arcgisruntime.portal.PortalItem;

import java.util.List;
import java.util.concurrent.ExecutionException;

import static com.example.corac.generalglacier.R.id.mapView;

public class MainActivity extends AppCompatActivity {

    private Portal aPortal;
    private PortalItem aPortalItem;
    private ArcGISMap aMap;
    private MapView aMapView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // inflate MapView from layout
        aMapView = (MapView) findViewById(mapView);

        // get the portal url for ArcGIS Online
        aPortal = new Portal(getResources().getString(R.string.portal_url));
        // get the pre-defined portal id and portal url
        aPortalItem = new PortalItem(aPortal, getResources().getString(R.string.webmap_glacier_national_park_id));
        // create a map from a PortalItem
        aMap = new ArcGISMap(aPortalItem);
        // set the map to be displayed in this view
        aMapView.setMap(aMap);
        Log.d("MAP SET", "we have set our map!");

        aMapView.setOnTouchListener(new IdentifyFeatureLayerTouchListener(this, aMapView));
        Log.d("ID SET", "we have set our map to be identified!");

    }
}

class IdentifyFeatureLayerTouchListener extends DefaultMapViewOnTouchListener {

    // provide a default constructor
    public IdentifyFeatureLayerTouchListener(Context context, MapView mapView) {
        super(context, mapView);
    }

    // override the onSingleTapConfirmed gesture to handle a single tap on the MapView
    @Override
    public boolean onSingleTapConfirmed(MotionEvent e) {

        // reference to the layer to identify features in
        FeatureLayer layer = null;

        // get the screen point where user tapped
        final android.graphics.Point screenPoint = new android.graphics.Point((int) e.getX(), (int) e.getY());
        // call identifyLayersAsync, passing in the screen point, tolerance, return types, and maximum results, but no layer
        final ListenableFuture<List<IdentifyLayerResult>> identifyFuture = mMapView.identifyLayersAsync(
                screenPoint, 10, false);

        // add a listener to the future
        identifyFuture.addDoneListener(new Runnable() {

            @Override
            public void run() {
                try {

                    // get the identify results from the future - returns when the operation is complete
                    List<IdentifyLayerResult> identifyLayersResults = identifyFuture.get();

                    // Here we just show the pop-up for the first (top) layer; if more than one layer may have pop-ups, or more
                    // than one element is identified in a single layer, add controls to the pop-up content to iterate through
                    // multiple pop-ups.
                    if (identifyLayersResults.size() >= 1) {
                        IdentifyLayerResult identifyLayerResult = identifyLayersResults.get(0);

                        // Only identifying the topmost item in this example, so only expecting one pop-up
                        if (identifyLayerResult.getPopups().size() >= 1) {
                            Popup identifiedPopup = identifyLayerResult.getPopups().get(0);

                            showPopup(identifiedPopup, screenPoint); 
                           
                            Log.d("popup", identifiedPopup.toString());

                        }
                    }

                } catch (InterruptedException | ExecutionException ex) {
                    // deal with exceptions thrown from the async identify operation
                }
            }
        });

        return true;
    }
}
Tags (4)
0 Kudos
10 Replies
RTomas
by
Emerging Contributor

Many thks Cora!

Its working!