package com.example.supermapimobile2d;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.content.ContextCompat;
import android.Manifest;
import android.content.Context;
import android.content.pm.PackageManager;
import android.hardware.SensorManager;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.WindowManager;
import android.widget.Toast;
import com.supermap.data.Dataset;
import com.supermap.data.Datasets;
import com.supermap.data.Datasource;
import com.supermap.data.DatasourceConnectionInfo;
import com.supermap.data.EngineType;
import com.supermap.data.Environment;
//import android.os.Environment;
import com.supermap.data.Workspace;
import com.supermap.data.WorkspaceConnectionInfo;
import com.supermap.data.WorkspaceType;
import com.supermap.mapping.Map;
import com.supermap.mapping.MapControl;
import com.supermap.mapping.MapView;
import com.supermap.mapping.view.LayerListView;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestPermissions();
MapControl mapControl = null;
MapControl m_mapControl = null;
String rootPath = android.os.Environment.getExternalStorageDirectory().getAbsolutePath();
// Environment.setLicensePath(rootPath + "/SuperMap/license/");
Environment.setLicensePath(android.os.Environment.getExternalStorageDirectory().getAbsolutePath()+"/SuperMap/license/");
Environment.setWebCacheDirectory(android.os.Environment.getExternalStorageDirectory().getAbsolutePath()+"/SuperMap/WebCache/");
Environment.setTemporaryPath(android.os.Environment.getExternalStorageDirectory().getAbsolutePath()+"/SuperMap/temp/");
Environment.initialization(this);
setContentView(R.layout.activity_main);
openMap();
}
private void requestPermissions() { //Android6.0以上设备设置动态权限
if (Build.VERSION.SDK_INT >= 23) {
// 检查是否拥有权限
String[] permissions = {
Manifest.permission.WRITE_EXTERNAL_STORAGE,
Manifest.permission.ACCESS_FINE_LOCATION,
Manifest.permission.READ_PHONE_STATE,
Manifest.permission.ACCESS_WIFI_STATE,
Manifest.permission.ACCESS_NETWORK_STATE,
Manifest.permission.CHANGE_WIFI_STATE,
};
String permission = null;
int id = 0;
boolean isBreak = false;
int checkCallPhonePermission = 0;
for (int i = 0; i < permissions.length; i++) {
permission = permissions[i];
checkCallPhonePermission = checkSelfPermission(permission);
if (checkCallPhonePermission != PackageManager.PERMISSION_GRANTED) {
requestPermissions(permissions, id);
isBreak = true;
break;
}
}
}
}
private boolean openMap() {
Workspace m_woWorkspace = new Workspace(); // 将地图显示控件与工作空间关联
MapView m_mapView = (MapView) findViewById(R.id.Map_view);
MapControl m_mapControl = m_mapView.getMapControl();
m_mapControl.getMap().setWorkspace(m_woWorkspace);
DatasourceConnectionInfo dsInfo = new DatasourceConnectionInfo();
dsInfo.setServer("http://192.168.4.20:8090/iserver/services/map-ChinaAyl/rest/maps/China");
dsInfo.setEngineType(EngineType.Rest);
dsInfo.setAlias("China");
Datasource ds = m_woWorkspace.getDatasources().open(dsInfo);
if(ds != null){
m_mapControl.getMap().getLayers().add(ds.getDatasets().get(0), true);
m_mapControl.getMap().viewEntire();
m_mapControl.getMap().setVisibleScalesEnabled(true);
}
return true;
}
}