使用service这个demo的代码,配置成功,显示出了中国地图。将代码挪到自己的工程中就加载不出来了。log中打印日志: E/com.example.supermap.DemoActivity: 打开数据源失败了。 java代码如下:
package com.example.supermap;
import android.Manifest;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.SharedPreferences;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.PopupWindow;
import android.widget.Toast;
import android.widget.ZoomControls;
import com.supermap.data.Datasource;
import com.supermap.data.DatasourceConnectionInfo;
import com.supermap.data.EngineType;
import com.supermap.data.Environment;
import com.supermap.data.Geometry;
import com.supermap.data.Point2D;
import com.supermap.data.Workspace;
import com.supermap.mapping.CallOut;
import com.supermap.mapping.MapControl;
import com.supermap.mapping.MapView;
import com.supermap.services.FeatureSet;
import com.supermap.services.QueryMode;
import com.supermap.services.QueryOption;
import com.supermap.services.QueryService;
import com.supermap.services.ResponseCallback;
import com.supermap.services.ServiceQueryParameter;
import pub.devrel.easypermissions.EasyPermissions;
/**
* <p>
* Title:服务模块示范代码
* </p>
*
* <p>
* Description:
* ============================================================================>
* ------------------------------版权声明----------------------------
* 此文件为SuperMap iMobile for Android 的示范代码
* 版权所有:北京超图软件股份有限公司
* ----------------------------------------------------------------
* ---------------------SuperMap iMobile for Android 示范程序说明------------------------
* <p>
* 1、范例简介:示范如何使用服务模块
* 2、示例数据:http://support.supermap.com.cn:8090/iserver/services/map-china400/rest/maps/China
* 3、关键类型/成员:
* QueryService.query(); 方法
* QueryService.setResposeCallback(); 方法
* ServiceQueryParameter.setQueryServiceName(); 方法
* ServiceQueryParameter.setQueryMapName(); 方法
* ServiceQueryParameter.setQueryLayerName(); 方法
* ServiceQueryParameter.setExpectRecordCount(); 方法
* ServiceQueryParameter.setQueryRecordStart(); 方法
* ServiceQueryParameter.setQueryOption(); 方法
* ServiceQueryParameter.setAttributeFilter(); 方法
* 4、使用步骤:
* (1)点击设置按钮,设置服务器地址与端口号。
* (2)输入要查询的服务名称、地图名称、图层名称(本范例程序中填入的服务名称和地图名与底图一致)。
* (3)输入查询语句,点击查询,查询结果会在地图上显示。
* ------------------------------------------------------------------------------
* ============================================================================>
* </p>
*
* <p>
* Company: 北京超图软件股份有限公司
* </p>
*/
public class DemoActivity extends Activity {
private MapView m_mapView;
private MapControl m_mapControl; // 地图显示控件
private Workspace m_woWorkspace; // 工作空间
private ZoomControls m_Zoom;
private Button m_btnSetting;
private Button m_btnQuery;
private PopupWindow m_popup;
private View mainLayout = null;
private EditText m_edtServerName;
private EditText m_edtMapName;
private EditText m_edtLayerName;
private EditText m_edtSql;
private String m_strServer = "";
private SharedPreferences m_preferences;
private SharedPreferences.Editor m_editor;
private final String sdcard = android.os.Environment.getExternalStorageDirectory().getAbsolutePath().toString();
/**
* 需要申请的权限数组
*/
protected String[] needPermissions = {
Manifest.permission.WRITE_EXTERNAL_STORAGE,
Manifest.permission.READ_EXTERNAL_STORAGE,
Manifest.permission.ACCESS_FINE_LOCATION,
Manifest.permission.ACCESS_COARSE_LOCATION,
Manifest.permission.READ_PHONE_STATE,
Manifest.permission.ACCESS_WIFI_STATE,
Manifest.permission.ACCESS_NETWORK_STATE,
Manifest.permission.CHANGE_WIFI_STATE,
};
/**
* Called when the activity is first created.
*/
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestPermissions();
// 设置许可路径
Environment.setLicensePath(sdcard + "/SuperMap/license/");
// 初始化环境
Environment.initialization(this);
setContentView(R.layout.activity_demo);
openMap();
// initView();
}
// 打开地图
private boolean openMap() {
m_woWorkspace = new Workspace();
// 将地图显示空间和 工作空间关联
m_mapView = (MapView) findViewById(R.id.mapview);
m_mapControl = m_mapView.getMapControl();
m_mapControl.getMap().setWorkspace(m_woWorkspace);
DatasourceConnectionInfo dsInfo = new DatasourceConnectionInfo();
dsInfo.setServer("http://support.supermap.com.cn:8090/iserver/services/map-china400/rest/maps/China");
dsInfo.setEngineType(EngineType.Rest);
dsInfo.setAlias("ChinaRest");
Datasource ds = m_woWorkspace.getDatasources().open(dsInfo);
if (ds != null) {
m_mapControl.getMap().getLayers().add(ds.getDatasets().get(0), true);
m_mapControl.getMap().refresh();
return true;
}
Log.e(this.getClass().getName(), "打开数据源失败了");
return true;
}
/**
* 检测权限
* return true:已经获取权限
* return false: 未获取权限,主动请求权限
*/
public boolean checkPermissions(String[] permissions) {
return EasyPermissions.hasPermissions(this, permissions);
}
/**
* 申请动态权限
*/
private void requestPermissions() {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
return;
}
if (!checkPermissions(needPermissions)) {
EasyPermissions.requestPermissions(
this,
"为了应用的正常使用,请允许以下权限。",
0,
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);
//没有授权,编写申请权限代码
} else {
//已经授权,执行操作代码
}
}
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
// Forward results to EasyPermissions
EasyPermissions.onRequestPermissionsResult(requestCode, permissions, grantResults, this);
}
}