问题
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