Android - How to know if mapView is properly loaded?

纵然是瞬间 提交于 2020-01-04 04:29:21

问题


I am having trouble in detecting if mapview is loaded in my android map appication. In OnCreate method, when I assign the map URL, I want to know if any problem occured. Problems may occur due to my internet connection or any other problems like loading the map from REST services. Here is my code block;

map.addLayer(newArcGISDynamicMapServiceLayer("any map URL"));

after this point, I try to use a control like if(map.isLoaded==false) but it does not work, although map is loaded properly it falls into this block.

Can anyone help me?

Thanks in advance


回答1:


Sounds like what might be happening is the code is checking map.isLoaded() before it has been initialized. Check MapView Reference for the official explanation on how to properly check for initialization.

I've expanded on the reference code

    map = (MapView) findViewById(R.id.map);
    tileLayer = new ArcGISTiledMapServiceLayer("http://services.arcgisonline.com/ArcGIS/rest/services/World_Shaded_Relief/MapServer");

    tileLayer.setOnStatusChangedListener(new OnStatusChangedListener() {
        public void onStatusChanged(Object source, STATUS status) {
            if (OnStatusChangedListener.STATUS.INITIALIZED == status){
                map.addLayer(tileLayer);  //when layer is initialized add to map
            }
        }
    });


    map.setOnStatusChangedListener(new OnStatusChangedListener() {
       private static final long serialVersionUID = 1L;

       public void onStatusChanged(Object source, STATUS status) {
           //conditional checks if mapView's status has changed to initialized 
            if (OnStatusChangedListener.STATUS.INITIALIZED == status && source == map) 
            { 
                Toast mapViewToast = Toast.makeText(ActivityName.this, "MapView loaded", Toast.LENGTH_LONG);
                mapViewToast.show();
            }
        }
     });


来源:https://stackoverflow.com/questions/8969498/android-how-to-know-if-mapview-is-properly-loaded

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!