Problems with using MapView.setMapOptions() or MapView.centerAndZoom()

1726
3
Jump to solution
07-27-2016 06:27 AM
DanielGailer
New Contributor

Hallo everybody,

i just made some Tutorials with the Esri Android API including the "Use ArcGIS basemaps" and everything worked fine. Now I want to implement a MapView into my own App and there are some problems with it:

What I have done:

I declared the MapView in my XML-Layout File and refer to it per "R.id". In my java-File Irefer to it and set some additional parameters like "mMapView.setEsriLogoVisible(true);" etc. and everything works fine until I reach the point where I want to change the initial View of the Map dynamically. So I tried ".setMapOptions" as well as ".centerAndZoom" nothing works here.

What I tried:

I remembered, that it was working fine in the Tutorial so I tried a few things out and saw, that the View changed if I wait a few seconds after initializing the MapView in java code. I created a timer and it worked. Also with a Button it worked fine. But if I try it directly after the initialisation it wont.

After that i tried to use the "OnStatusChangedListener" because I thougt it might work if the Map will be completely initialized, but here happens nothing as well. Tha map just appears at the initial point declared in the xml file. If I delete the initial coordinates in my Layout-File the hole MapView won't beinitialized (referring to the Callback).

Main question short:

How do I know, when I can use MapView.setMapOptions to change the initial center and appearence of the Map?

My Code:

.java:

public class MissionControlActivity extends AppCompatActivity{

     private MapView mMapView = null;

     private MapOptions mMapOptionsSat = new MapOptions(MapType.SATELLITE, 43.971873, 6.161288, 9);

    

     @Override

     protected void onCreate(Bundle savedInstanceState) {

     super.onCreate(savedInstanceState);

     setContentView(R.layout.activity_mission_control);

     initUI();

     }

    

     private void initUI(){

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

          mMapView.enableWrapAround(true);

          mMapView.setAllowRotationByPinch(true);

          mMapView.setMapOptions(mMapOptionsSat);            //This here doesn't work

}

.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
   xmlns:android="http://schemas.android.com/apk/res/android"
   xmlns:custom="http://schemas.android.com/apk/res-auto"
   android:layout_width="fill_parent"
   android:layout_height="fill_parent">

       <com.esri.android.map.MapView
             android:id="@+id/esriMapView"
             android:layout_width="fill_parent"
             android:layout_height="fill_parent"
             mapoptions.MapType="Topo"
             mapoptions.ZoomLevel="13"
             mapoptions.center="33.666354, -117.903557"/>

</RelativeLayout>

0 Kudos
1 Solution

Accepted Solutions
AlexanderNohe1
Occasional Contributor III

Daniel Gailer

I got this working by listening for the status changed to have completed doing the following in my MainActivity.java:

package com.arcgis.androidsupportcases.geonetcenterandzoom;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import com.esri.android.map.MapOptions;
import com.esri.android.map.MapView;
import com.esri.android.map.event.OnStatusChangedListener;

public class MainActivity extends AppCompatActivity {

  MapView mMapView = null;
  private MapOptions mMapOptionsSat = new MapOptions(MapOptions.MapType.SATELLITE, 43.971873, 6.161288, 9);

  @Override protected void onCreate(Bundle savedInstanceState) {
   super.onCreate(savedInstanceState);
   setContentView(R.layout.activity_main);
   initUI();
  }

  private void initUI(){
   mMapView = (MapView) findViewById(R.id.esriMapView);

   mMapView.setOnStatusChangedListener(new OnStatusChangedListener() {
   @Override public void onStatusChanged(Object o, STATUS status) {
   if(status == STATUS.INITIALIZED) {
   mMapView.enableWrapAround(true);
   mMapView.setAllowRotationByPinch(true);
   mMapView.setMapOptions(mMapOptionsSat)//This here doesn't work
   }
  }
  });


  }
}

What did your onStatusChanged listener look like?

View solution in original post

3 Replies
AlexanderNohe1
Occasional Contributor III

Daniel Gailer

I got this working by listening for the status changed to have completed doing the following in my MainActivity.java:

package com.arcgis.androidsupportcases.geonetcenterandzoom;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import com.esri.android.map.MapOptions;
import com.esri.android.map.MapView;
import com.esri.android.map.event.OnStatusChangedListener;

public class MainActivity extends AppCompatActivity {

  MapView mMapView = null;
  private MapOptions mMapOptionsSat = new MapOptions(MapOptions.MapType.SATELLITE, 43.971873, 6.161288, 9);

  @Override protected void onCreate(Bundle savedInstanceState) {
   super.onCreate(savedInstanceState);
   setContentView(R.layout.activity_main);
   initUI();
  }

  private void initUI(){
   mMapView = (MapView) findViewById(R.id.esriMapView);

   mMapView.setOnStatusChangedListener(new OnStatusChangedListener() {
   @Override public void onStatusChanged(Object o, STATUS status) {
   if(status == STATUS.INITIALIZED) {
   mMapView.enableWrapAround(true);
   mMapView.setAllowRotationByPinch(true);
   mMapView.setMapOptions(mMapOptionsSat)//This here doesn't work
   }
  }
  });


  }
}

What did your onStatusChanged listener look like?

DanielGailer
New Contributor

First: Thanks for the fast answer!

I actually tried a bit more and thaught about it, why it works fine with your code and not with mine. I had the exact same callback as you with one exeption: I had implemented the Callback out of the sample code of esri and there is the if-condition like that:

if (STATUS.INITIALIZED == status) {...}

the only thing I changed now, was the order to this:

if (status ==STATUS.INITIALIZED) {...}

I have absolutely no idea why that should make a difference, but now it works. Really strange! But thanks anyways.

0 Kudos
AlexanderNohe1
Occasional Contributor III

Awesome!  If this resolved your question, would you kindly mark my answer as the correct one?

Thank you!

0 Kudos