按钮点击,切换工作空间,代码如下:我觉得代码应该没问题,就是有时这个会出现ANR;我记录了其中一次出现这种情况的log,我将其复制下来:
private void open(String path){
if(!isCreated)
init();
isCreated = true;
mworkspace = new Workspace();
WorkspaceConnectionInfo connectionInfo = new WorkspaceConnectionInfo();
connectionInfo.setServer(path);
connectionInfo.setType(WorkspaceType.SMWU );
mworkspace.open(connectionInfo);
Datasource da = mworkspace.getDatasources().get(0);
mmapControl = mapView.getMapControl();
mmapControl.getMap().setWorkspace(mworkspace);
map = mmapControl.getMap();
maps = mworkspace.getMaps();
layers = map.getLayers();
if(maps.getCount()!=0){
String mapname = mworkspace.getMaps().get(0);//打开第一幅地图
map.open(mapname);
}
layer_btn.requestFocus();
//显示特定比例尺,必须在地图加载完之后设置,否则不起作用
// double [] doubles = new double[]{1.0/100.0,1.0/200.0,1.0/500.0,1.0/1000.0,1.0/2000.0,1.0/5000.0,1.0/10000.0,1.0/25000.0,1.0/50000.0,1.0/100000.0};
// map.setVisibleScales(doubles);
// map.setVisibleScalesEnabled(true);
Display display = getWindowManager().getDefaultDisplay();
DisplayMetrics dm = new DisplayMetrics();
display.getMetrics(dm);
map.setMapDPI(dm.densityDpi);
/*
如果项目中包含测绘和勘探数据源,我们就将地图设置为其
如果不包含,则将其设置为第一个数据源的投影
*/
Myapplication myapplication = (Myapplication) getApplication();
List<String> datasourName = new ArrayList<>();
for(int i = 0; i< mworkspace.getDatasources().getCount();i++){
datasourName.add(mworkspace.getDatasources().get(i).getAlias());
}
if(datasourName.contains("测绘") ){
map.setPrjCoordSys(mworkspace.getDatasources().get("测绘").getDatasets().get("地质点").getPrjCoordSys());
myapplication.settarget_PriCoordsys(mworkspace.getDatasources().get("测绘").getDatasets().get("地质点").getPrjCoordSys());
}else if ( datasourName.contains("勘探")){
map.setPrjCoordSys(mworkspace.getDatasources().get("勘探").getDatasets().get(0).getPrjCoordSys());
myapplication.settarget_PriCoordsys(mworkspace.getDatasources().get("勘探").getDatasets().get(0).getPrjCoordSys());
}else {
map.setPrjCoordSys(mworkspace.getDatasources().get(0).getPrjCoordSys());
Log.e("mapcontrol",mworkspace.getDatasources().get(0).getPrjCoordSys().getName());
myapplication.settarget_PriCoordsys(mworkspace.getDatasources().get(0).getPrjCoordSys());
}
myapplication.setMapControl(mmapControl);
//保存工程文名称,在后面具有很大的作用
String [] split1 = path.split("/");
String project_name= split1[split1.length-2];
myapplication.setProject_Name(project_name);
/*
timer = new Timer();
TimerTask timerTask = new TimerTask() {
@Override
public void run() {
if(map!=null){
map.refresh();
}
}
};
timer.schedule(timerTask,1000,3000);*/
}
对应的log: