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!
Solved! Go to Solution.
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.
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.