首页 / 浏览问题 / 移动GIS / 问题详情
Android 如何重叠加载两个地图
Lsk
11EXP 2022年10月18日

问题是:现在有两个地图服务地址,地址1和地址2,分别对应两个地图,地图1和地图2,地图1是一张世界地图作为底,地图2是一块区域地图背景是透明的,两个地图的坐标系一样,现在想在一个地图容器里面同时展示这两幅地图,地图2在地图1对应的位置重叠展示;

我都代码如下:

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.Layers;
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 {
    String RootPath = android.os.Environment.getExternalStorageDirectory().getAbsolutePath().toString();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        requestPermissions();
        super.onCreate(savedInstanceState);

        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);


        //2022-10-18 添加下面一行代码:用内存引擎,openGL引擎在模拟器上不存在,iserver地图可以在模拟器上展示了
        Environment.setOpenGLMode(false);
        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();
        DatasourceConnectionInfo dsInfo1 = new DatasourceConnectionInfo();

        dsInfo.setServer("http://192.168.4.21:8090/iserver/services/map-ChinaAyl/rest/maps/ChinaDark?prjCoordSys={'epsgCode':4326}");
        dsInfo1.setServer("http://192.168.4.21:8090/iserver/services/map-ZKGQ/rest/maps/ZKYZT?prjCoordSys={'epsgCode':4326}");

        dsInfo.setEngineType(EngineType.Rest);
        dsInfo1.setEngineType(EngineType.Rest);
        dsInfo.setAlias("ChinaDark");
        dsInfo1.setAlias("ZKYZT");

        Datasource ds = m_woWorkspace.getDatasources().open(dsInfo);

        Datasource ds1 = m_woWorkspace.getDatasources().open(dsInfo1);

        if(ds != null){

            m_mapControl.getMap().getLayers().add(ds.getDatasets().get(0), true);
            m_mapControl.getMap().getLayers().add(ds1.getDatasets().get(0), true);

            m_mapControl.getMap().refresh();

        }

        return true;
    }

}

控制台有异常信息:

E/SurfaceHolder: Exception locking surface
    java.lang.IllegalArgumentException: Surface was already locked
        at android.view.Surface.lockCanvas(Surface.java:308)
        at android.view.SurfaceView$4.internalLockCanvas(SurfaceView.java:990)
        at android.view.SurfaceView$4.lockCanvas(SurfaceView.java:958)
        at com.supermap.mapping.MapControl1.c(MapControl1.java:365)
        at com.supermap.mapping.MapControl1.refreshCallback(MapControl1.java:1072)
        at com.supermap.mapping.MapControlNative.jni_SetSize(Native Method)
        at com.supermap.mapping.MapControl1.a(MapControl1.java:1218)
        at com.supermap.mapping.MapControl1.c(MapControl1.java:375)
        at com.supermap.mapping.MapControl1$3.surfaceCreated(MapControl1.java:338)
        at android.view.SurfaceView.updateWindow(SurfaceView.java:618)
        at android.view.SurfaceView$3.onPreDraw(SurfaceView.java:161)
        at android.view.ViewTreeObserver.dispatchOnPreDraw(ViewTreeObserver.java:944)
        at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2205)
        at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1254)
        at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6338)
        at android.view.Choreographer$CallbackRecord.run(Choreographer.java:874)
        at android.view.Choreographer.doCallbacks(Choreographer.java:686)
        at android.view.Choreographer.doFrame(Choreographer.java:621)
        at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:860)
        at android.os.Handler.handleCallback(Handler.java:751)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:154)
        at android.app.ActivityThread.main(ActivityThread.java:6176)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:893)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:783)
 

1个回答

您好,建议您先用桌面叠加两张在线地图,看看效果是否正确,还要注意两张地图的坐标系要一致
3,745EXP 2022年10月18日
您好,这个桌面叠加后效果是没问题的 坐标系也在请求的时候修改为一致了
在线地图不支持动态投影,建议把地图坐标系修改一致后再发布或者使用在线加本地的方法即先添加天地图、再开启动态投影、接着添加本地图层即可
有代码案例吗
...