Add a clickable "Pin" to a map

3082
7
01-15-2014 09:06 PM
razmaimon
New Contributor
hello all,
i am a bit new to this, inherited a code from an Old programer.

i have a map on my android app, to it, i add "pins" (Graphic) to some Geometries(each has an other Lat-Long)
now the customer wants each pin to be clickable, meaning, that if the user scrolls around in the map all will be the same, but if he clicks a pin on the map, it will invoke an other activity at my choice.

is this thing possible?
any suggestions on how to approach the problem?
i'm attaching a code sample for the part where i insert the pins to the map:

 map = new MapView(this);
...
map.addLayer(graphicsLayer);
...
graphicsLayer.setRenderer(new SimpleRenderer(_bluePin));
for (int i = 0; i < workingVector.size(); i++) {
    businessData = workingVector.elementAt(i);
    Graphic graphicForBluePin = new Graphic();
    graphicForBluePin.setGeometry(new Point(businessData.Longitude, businessData.Latitude));
    graphicsLayer.addGraphic(graphicForBluePin);
}
graphicsLayer.postInvalidate();



thanks!!!
Raz
0 Kudos
7 Replies
ShellyGill1
Esri Contributor
Hi Raz,


is this thing possible?
any suggestions on how to approach the problem?


Indeed it is possible. I would try something like this:
1. Use the setOnSingleTapListener method on the MapView to find out when a user taps on the map. In that method you will be passed a location in pixels (x and y parameters).
2. Pass these to the getGraphicIds method on the GraphicsLayer that you have, and this will find the unique IDs of the nearest graphics. You may want to experiment with the different overloads of this method - you can choose different tolerance distances for the tap location, and limit the number of results returned.
3. From that graphic ID that gets returned, you can then use the GraphicLayer.getGraphic method, to return the Graphic that was tapped on/near. At this point you can get any information from that Graphic you want.
4. Then you can pass this information to your second Activity via an Intent.

For example, in the activity that has your map view (MapViewActivity in the the code below:
       map.setOnSingleTapListener(new OnSingleTapListener() {
          
          @Override
          public void onSingleTap(float x, float y) {
            int[] ids = graphicsLayer.getGraphicIDs(x,  y,  10, 1); // experiment with tolerance and num of results params here
            if (ids != null && ids.length > 0)
            {
              Graphic foundGraphic = graphicsLayer.getGraphic(ids[0]);
              if (foundGraphic != null) {
                Intent intent = new Intent(MapViewActivity.this, GraphicActivity.class);
                intent.putExtra("graphicInfo", String.valueOf(foundGraphic.getId()));
                startActivity(intent);
              }
            }
          }
        });


Then in your second activity (called GraphicActivity in this example), you can get back the info from the Intent and show it somehow - just showing the ID in this example here:
public class GraphicActivity extends Activity {

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    
    // Get the message from the intent
    Intent intent = getIntent();
    String message = intent.getStringExtra("graphicInfo");

    // Create the text view
    TextView textView = new TextView(this);
    textView.setTextSize(40);
    textView.setText("GraphicID = " + message);

    // Set the text view as the activity layout
    setContentView(textView);
  }


Hope this helps,
0 Kudos
razmaimon
New Contributor
first, thanks for the quick reply,

how can i set an id to a graphic?
i only see the method SetId(...) in the GraphicsLayer.

for Graphic, i only see these:

setAttributes(...);
setGeometry(...);
setInfoTemplate(...);
setSymbol(...);



Raz
0 Kudos
razmaimon
New Contributor
also,
in my graphics layer, i only have the method
Graphic[] getGraphics()

i don't have
getGraphicIDs
or
getGraphic
0 Kudos
ShellyGill1
Esri Contributor
how can i set an id to a graphic?

You dont need to set an ID, because this happens automatically when you add the graphic to the layer.

in my graphics layer, i only have the method Graphic[] getGraphics()

I wonder what version of the ArcGIS Runtime SDK for Android you are using? Perhaps things have changed since the version you are using, as you mentioned that the code is inherited from someone else? I dont know much about the older versions of the API, I was using the latest version, e.g. see the API documentation here:
https://developers.arcgis.com/en/android/api-reference/reference/com/esri/android/map/GraphicsLayer....
0 Kudos
razmaimon
New Contributor
if it helps,this is my ArcGIS_Android.jar Manifest:
it is about 3-4 years old....so my guess is that i need the new one....

Manifest-Version: 1.0
Archiver-Version: Plexus Archiver
Created-By: Apache Maven
Built-By: root
Build-Jdk: 1.6.0_20
Implementation-Title: ArcGIS_Android
Implementation-Version: 0.1
Implementation-Vendor-Id: esri
Hudson-Build-Number: 160
Hudson-Project: AndroidSDK_build
Hudson-Version: 1.369
Implementation-Build: 0.1.150_20101008
0 Kudos
ShellyGill1
Esri Contributor
Yep it certainly looks like a very early version - the current release would be Implementation-Build: 10.2 I think (there have not been 10 major versions, but at some point the version numbers changed so they matched other Esri software, so it became 10.x version and forwards). There could be a whole lot of changes between the version you have and the current version though so worth investigating before you know if it's best to move or not - could be a good idea to upgrade if you intend your app to have a lifespan of a few more years.
0 Kudos
razmaimon
New Contributor
thanks,
you helped me alot....

Raz
0 Kudos