Select to view content in your preferred language

XML with ArcGIS.com webmap

540
3
11-27-2012 01:25 PM
JohnAllen
Deactivated User
I'm using the sample code [PopupInWebMapForViewing] provided by ArcGIS SDK.

I placed my arcgis.com webmap link into the sample code and was able to view my map I created on arcgis.com as well as the popups I configured.

My question:

How do I add a button to my map view using the webmap mapview?
Do I use XML or do I code the button in java?

The sample code for viewing the map:

 /** Called when the activity is first created. */    
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    
    // Load a webmap.
    map = new MapView(this, "http://www.arcgis.com/home/item.html?id=ec84873154cf43069c407", "", "");
    setContentView(map);


I've used buttons before, but I called on my ArcGISDynamic map services in a slightly different way.

This is how I coded my button previously:

/** Called when the activity is first created. */
 public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
   setContentView(R.layout.mapscreen);

    // Retrieve the map and initial extent from XML layout
    map = (MapView) findViewById (R.id.map);
      
    // This adds ESRI's titled map service layer "World Street Map" to the MapView, as well as DHEC's 
    // dynamics map service "MPOS"
    map.addLayer(new ArcGISTiledMapServiceLayer("" +
    "http://services.arcgisonline.com/ArcGIS/rest/services/World_Street_Map/MapServer"));
    map.addLayer(new ArcGISDynamicMapServiceLayer("" +
    "http://TEST/ArcGIS/rest/services/Mobile/TRSS/MapServer"));
  
    //Setting up the button reference.
          ZoomToMyLocation = (Button) findViewById (R.id.button1);


Thanks,
0 Kudos
3 Replies
JohnAllen
Deactivated User
I need to add a button ontop of the the webmap view.

Can I change my [ setContentView(map); ] so that it points to an XML layout and still use the web map view?
0 Kudos
JohnAllen
Deactivated User
Follow up....

I have a solution to my problem.

To make a button show up on top of my webmap view I used a popup window.

  /** Called when the activity is first created. */    
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    
    // Load a webmap.
    map = new MapView(this, "http://www.arcgis.com/home/item.html?id=ec84873069c408", "", "");
    setContentView(map);
    
 // Post create PopupWindow since it created during onCreate().
   final Context context = this;
   map.post(
    new Runnable()
    {
     public void run()
     {
      popupWindow = new PopupWindow(context);
      LinearLayout linearLayout = new LinearLayout(context);
      linearLayout.setOrientation(LinearLayout.HORIZONTAL);
      Button button = new Button(context);
      button.setText("Zoom To My Location");
      linearLayout.addView(button);
      popupWindow.setContentView(linearLayout);
      popupWindow.showAtLocation(linearLayout, Gravity.LEFT | Gravity.BOTTOM, 10, 10);
      popupWindow.update(250,64);
     }
    });


The button isn't set to do anything in the above code, just wanted to show how it looks.

I found my answer on this thread:

http://forums.arcgis.com/threads/60852-Cannot-use-MapView-in-a-LinearLayout

[ATTACH=CONFIG]19583[/ATTACH]
0 Kudos
JohnAllen
Deactivated User
Follow up...

The reason I added the button (or Popup window) in the java code (*see previous post) was because I didn't know how to call the webmap from XML.

Here's how to call the webmap from XML and use XML to add buttons (Really its and image view, not a button).

Calling a button (image view) from XML is easier in my opinion:

Java Code:

public class MapscreenTwo extends Activity {
 
 
  public MapView map;
  public ImageView ZoomToMyLocation;
  ....

    /** Called when the activity is first created. */
 public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
   setContentView(R.layout.mapscreentwo);

    // Retrieve the map and initial extent from XML layout
    map = (MapView) findViewById (R.id.map);
  
    //Setting up the button (Image View) reference.
                  ZoomToMyLocation = (ImageView) findViewById (R.id.gpsicon);
              
                 //This listens for when the ZoomToMyLocation button (Image View) is pressed. 
                 ZoomToMyLocation.setOnClickListener(new View.OnClickListener() {



This is what my XML looks like.

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout 
        xmlns:android="http://schemas.android.com/apk/res/android"
 android:orientation="vertical"
 android:layout_width="fill_parent"
        android:layout_height="fill_parent">
    
        <!-- MapView layout and initial extent -->
        <com.esri.android.map.MapView
        android:id="@+id/map"
        android:layout_width="fill_parent"
 android:layout_height="match_parent"
 url="http://www.arcgis.com/home/item.html?id=ec84873154cf4d7f" >
 </com.esri.android.map.MapView>
  
           <LinearLayout 
                  android:orientation="horizontal"   
           android:gravity="left"   
           android:layout_width="wrap_content"
           android:layout_height="wrap_content" >

           <ImageView
                 android:id="@+id/gpsicon"
                 android:layout_width="55dp"
                 android:layout_height="54dp"
                 android:src="@drawable/gpsicon" />

          </LinearLayout>
          
</RelativeLayout>


I'm a complete newbie to programming. I just try stuff until it works. Hope this helps.
0 Kudos