How to select graphic on click

3719
1
Jump to solution
01-10-2016 11:21 AM
sepans
by
New Contributor

I'm using the arcGIS Android API with Android Studio, and I would like to know how to select a graphic symbol when a user taps on it. I have a graphics layer and I've added a couple picture marker symbols to it. I simply want to show a symbol as being selected when a user taps on it.

Creating the graphics:

graphicAirport_IAD = new Graphic(projIAD, pmsAirport); 
graphicAirport_DCA = new Graphic(projDCA, pmsAirport); 
graphicAirport_BWI = new Graphic(projBWI, pmsAirport); 

Adding the graphics to map view:

mMapView.addLayer(gAirport); 
gAirport.addGraphic(graphicAirport_IAD); 
gAirport.addGraphic(graphicAirport_DCA); 
gAirport.addGraphic(graphicAirport_BWI); 

The graphics all show up fine. Again, I would just like to be able to select them when they're tapped on (respectively) by a user.

Thanks!

Tags (1)
0 Kudos
1 Solution

Accepted Solutions
AlexanderNohe1
Occasional Contributor III

I wrote something like this:

public class MainActivity extends AppCompatActivity {

  MapView mMapView;
   GraphicsLayer mGraphicsLayer;

   @Override
   protected void onCreate(Bundle savedInstanceState) {
   super.onCreate(savedInstanceState);
   setContentView(R.layout.activity_main);
   mMapView = (MapView) findViewById(R.id.map_view);
   mMapView.setOnSingleTapListener(new OnSingleTapListener() {
   @Override
   public void onSingleTap(float v, float v1) {
  Point point = mMapView.toMapPoint(v, v1);
  int[] graphicIDs = mGraphicsLayer.getGraphicIDs(v, v1, 1, 1);
  for (int i = 0; i < graphicIDs.length; i++) {
  Log.d("NOHE", "GRAPHIC: " + graphicIDs);
   }
   mGraphicsLayer.setSelectedGraphics(graphicIDs, true);
   }
  });

   mGraphicsLayer = new GraphicsLayer(mMapView.getSpatialReference(), mMapView.getMaxExtent(), GraphicsLayer.RenderingMode.DYNAMIC);
   mGraphicsLayer.addGraphic(new Graphic(new Point(-8416452.130, 4628356.957), new PictureMarkerSymbol(ContextCompat.getDrawable(getApplicationContext(), R.drawable.ic_marker))));
   mGraphicsLayer.setSelectionColorWidth(3);
   mGraphicsLayer.setSelectionColor(R.color.selectionColor);
   mMapView.addLayer(mGraphicsLayer);
   }
}

More logic is required for you to get it working exactly the way you want.  The important portion here is the onSingleTap listener.  You could modify this code to remove and add selections.

View solution in original post

1 Reply
AlexanderNohe1
Occasional Contributor III

I wrote something like this:

public class MainActivity extends AppCompatActivity {

  MapView mMapView;
   GraphicsLayer mGraphicsLayer;

   @Override
   protected void onCreate(Bundle savedInstanceState) {
   super.onCreate(savedInstanceState);
   setContentView(R.layout.activity_main);
   mMapView = (MapView) findViewById(R.id.map_view);
   mMapView.setOnSingleTapListener(new OnSingleTapListener() {
   @Override
   public void onSingleTap(float v, float v1) {
  Point point = mMapView.toMapPoint(v, v1);
  int[] graphicIDs = mGraphicsLayer.getGraphicIDs(v, v1, 1, 1);
  for (int i = 0; i < graphicIDs.length; i++) {
  Log.d("NOHE", "GRAPHIC: " + graphicIDs);
   }
   mGraphicsLayer.setSelectedGraphics(graphicIDs, true);
   }
  });

   mGraphicsLayer = new GraphicsLayer(mMapView.getSpatialReference(), mMapView.getMaxExtent(), GraphicsLayer.RenderingMode.DYNAMIC);
   mGraphicsLayer.addGraphic(new Graphic(new Point(-8416452.130, 4628356.957), new PictureMarkerSymbol(ContextCompat.getDrawable(getApplicationContext(), R.drawable.ic_marker))));
   mGraphicsLayer.setSelectionColorWidth(3);
   mGraphicsLayer.setSelectionColor(R.color.selectionColor);
   mMapView.addLayer(mGraphicsLayer);
   }
}

More logic is required for you to get it working exactly the way you want.  The important portion here is the onSingleTap listener.  You could modify this code to remove and add selections.