Select to view content in your preferred language

Pb to center the map

3222
1
12-18-2013 05:53 AM
SergeUngar
New Contributor
Hello

I'm a newbie in ArcGis under Android.

I would like to center the map on a specific point (for instance 33.666354, -117.903557).


My activity is :


public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        mMapView = (MapView)findViewById(R.id.map);

        //MapOptions topo = new MapOptions(MapType.TOPO, 23, -110, 9); 
        //mMapView = new MapView(this, topo);
        
        Point esri4326 = new Point(33.666354, -117.903557);
  Point esri102100 = (Point) GeometryEngine.project(
   esri4326,
   SpatialReference.create(4326),
   SpatialReference.create(102100)
  );
  
  mMapView.centerAt(esri102100, false);
        
        
        topo = new MapOptions(MapType.TOPO);
 streets = new MapOptions(MapType.STREETS);
 natGeo = new MapOptions(MapType.NATIONAL_GEOGRAPHIC);
 ocean = new MapOptions(MapType.OCEANS);
  
 mMapView.setEsriLogoVisible(true);

    }




My xml file is :


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    tools:context=".ArcGisHelloWorldActivity"
    >
 <!-- MapView layout and initial extent -->
 <com.esri.android.map.MapView
  android:id="@+id/map"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  mapoptions.MapType="topo"
        >
 </com.esri.android.map.MapView>
</LinearLayout>


So the map is open but with the world :

[ATTACH=CONFIG]29973[/ATTACH]

Thank you for your help
0 Kudos
1 Reply
ShellyGill1
Esri Contributor
Hi,

You can set an initial extent within the layout XML - this is ideal if you want to hard-code this to something specific known in advance, e.g.
 <com.esri.android.map.MapView
  android:id="@+id/map"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  mapoptions.MapType="topo"
                mapoptions.ZoomLevel="13"
                mapoptions.center="33.666354 -117.903557"     >
 </com.esri.android.map.MapView>   

You can see this done in the Hello World sample code in the SDK - note that you likely want to zoom in as well to center on some part of the world, and this is done here by setting a zoom level.

If you want to do this programmatically instead, first you must make sure that your coorinstae are the correct way around - look at the Point contructor and you will see that the Point constructor takes X, Y coordinates in that order, and X = latitude, Y = longitude. So your point creation would need to be:
    Point esri4326 = new Point(-117.903557, 33.666354);

In the onCreate of an activity, the MapView may still be initializing itself, so this may be why the map did not zoom/center at that point possibly. What worked for me adding a listener for when the basemap layer is loaded and then I used zoomToResolution, which allows you to set the center point and to zoom to a specific resolution in the same method call, for example as shown below (you can probably work out a more appropriate way to check the basemap layer is loaded, e.g. cast source parameter to an ArcGISTiledMapServiceLayer and check it's name, or position in the map perhaps, below is just an example of something that worked in this case):
   mMapView.setOnStatusChangedListener(new OnStatusChangedListener() {
      private static final long serialVersionUID = 1L;

      public void onStatusChanged(Object source, STATUS status) {
        if (OnStatusChangedListener.STATUS.LAYER_LOADED == status && (source instanceof ArcGISTiledMapServiceLayer)) {
          Point esri4326 = new Point(-117.903557, 33.666354);
          Point esri102100 = (Point) GeometryEngine.project(esri4326, SpatialReference.create(4326), SpatialReference.create(102100);
          mMapView.zoomToResolution(esri102100, 2000);
        }
      }
    });

Note that in theOnStatusChangedListener onStatusChanged callback I originally tried this below; however I found this wasn't working for me, although I am not sure why:
    if (OnStatusChangedListener.STATUS.INITIALIZED == status && source == mMapView)
      // Failed to set map zoom / resolution here


Hope this helps,
0 Kudos