欢迎关注我的微信公众号“人小路远”哦,在这里我将会记录自己日常学习的点滴收获与大家分享,以后也可能会定期记录一下自己在外读博的所见所闻,希望大家喜欢,感谢支持!
解决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);
}