移动GIS开发之Esri地图2d和3d切换

解决MapView和SceneView遮挡问题有两种思路,一种是利用setContentView切换layout,一种是把当前地图最小化为一个像素。本文以前者为例,第一个layout为activity_esri_map.xml,第二为activity_esri_scene.xml。

目录

一、新建地图控件

二、定义变量并加载地图

三、函数封装

四、点击运行查看效果


一、新建地图控件

在地图设计页面上新建三个Button,用于切换地图,两个2d一个3d。button1和button2为2d,button3为3d。

activity_esri_map.xml中如下:

<Button
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:layout_marginEnd="30dp"
        android:onClick="button1onclick"
        android:id="@+id/button1"
        android:text="@string/button1"
        />
    
    <Button
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:layout_marginTop="40dp"
        android:layout_marginEnd="30dp"
        android:onClick="button2onclick"

        android:id="@+id/button2"
        android:text="@string/button2"
        />

    <Button
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:layout_marginTop="80dp"
        android:layout_marginEnd="30dp"
        android:onClick="button3onclick"

        android:id="@+id/button3"
        android:text="@string/button3"
        />

    <com.esri.arcgisruntime.mapping.view.MapView
        android:id="@+id/mapView"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >
    </com.esri.arcgisruntime.mapping.view.MapView>

 activity_esri_scene.xml中把上面的MapView替换为SceneView

<com.esri.arcgisruntime.mapping.view.SceneView
    android:id="@+id/sceneView"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >
</com.esri.arcgisruntime.mapping.view.SceneView>

二、定义变量并加载地图

    private MapView mMapView;
    private ArcGISMap mMap;
    private Portal mPortal;
    private PortalItem mPortalItem_map1;
    private PortalItem mPortalItem_map2;
    private PortalItem mPortalItem_scene;
    private SceneView mSceneView;
@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_esri_map);
        //查找并绑定名为mapView的容器
        mMapView = findViewById(R.id.mapView);
        //添加经纬格网
        LatitudeLongitudeGrid grid = new LatitudeLongitudeGrid();
        //在MapView上显示格网
        mMapView.setGrid(grid);
        // get the portal url for ArcGIS Online
        mPortal = new Portal(getResources().getString(R.string.portal_url));


        // get the pre-defined portal id and portal url
        mPortalItem_map1 = new PortalItem(mPortal, getResources().getString(R.string.webmap_houses_with_mortgages_id));
        // create a map from a PortalItem
        mMap = new ArcGISMap(mPortalItem_map1);
        // set the map to be displayed in this view
        mMapView.setMap(mMap);


    }

地图链接portal id

    <!-- arcgis online portal url -->
    <string name="portal_url">http://www.arcgis.com/</string>

    <!-- scenemap portal item id's -->
    <string name="buildings_brest">8eaff4f349d6420c99968107629246c4</string>
    <!-- webmap portal item id's -->
    <string name="webmap_houses_with_mortgages_id">23fe7e8317ba4331b6ca72bf2a8eddb6</string>
    <string name="webmap_usa_tapestry_segmentation_id">bf024b8d0b4b48f5a486070214e87c5f</string>

三、函数封装

//本处解决MaoView和SceneView遮挡问题使用的是setContentView切换layout的方法
    //还有一种思路是把地图最小化为一个像素
    public void button1onclick(View v){

        setContentView(R.layout.activity_esri_map);
        //查找并绑定名为mapView的容器
        mMapView = findViewById(R.id.mapView);
        //添加经纬格网
        LatitudeLongitudeGrid grid = new LatitudeLongitudeGrid();
        //在MapView上显示格网
        mMapView.setGrid(grid);
        // get the portal url for ArcGIS Online
        mPortal = new Portal(getResources().getString(R.string.portal_url));
        // get the pre-defined portal id and portal url
        mPortalItem_map1 = new PortalItem(mPortal, getResources().getString(R.string.webmap_houses_with_mortgages_id));
        // create a map from a PortalItem
        mMap = new ArcGISMap(mPortalItem_map1);
        // set the map to be displayed in this view
        mMapView.setMap(mMap);
    }

    public void button2onclick(View v){

        setContentView(R.layout.activity_esri_map);
        //查找并绑定名为mapView的容器
        mMapView = findViewById(R.id.mapView);
        //添加经纬格网
        LatitudeLongitudeGrid grid = new LatitudeLongitudeGrid();
        //在MapView上显示格网
        mMapView.setGrid(grid);
        // get the portal url for ArcGIS Online
        mPortal = new Portal(getResources().getString(R.string.portal_url));
        // get the pre-defined portal id and portal url
        mPortalItem_map2 = new PortalItem(mPortal, getResources().getString(R.string.webmap_usa_tapestry_segmentation_id));
        // create a map from a PortalItem
        mMap = new ArcGISMap(mPortalItem_map2);
        // set the map to be displayed in this view
        mMapView.setMap(mMap);
    }
    
    
    
    public void button3onclick(View v){
        setContentView(R.layout.acttivity_esri_scene);
        //查找并绑定名为sceneView的容器
        mSceneView = findViewById(R.id.sceneView);
        // get the portal url for ArcGIS Online
        mPortal = new Portal(getResources().getString(R.string.portal_url));
        // get the pre-defined portal id and portal url
        mPortalItem_scene = new PortalItem(mPortal, getResources().getString(R.string.buildings_brest));
        // create a map from a PortalItem
        ArcGISScene scene = new ArcGISScene(mPortalItem_scene);
        // set the map to be displayed in this view
        mSceneView.setScene(scene);

    }

四、点击运行查看效果

猜你喜欢

转载自blog.csdn.net/qq_36017609/article/details/85700587