携手创作,共同成长!这是我参与「掘金日新计划 · 8 月更文挑战」的第 1 天,点击查看活动详情
ArcGIS 学习笔记系列
1、Arcgis for Android 100 系列(一)、环境配置和地图加载
地图缩放
100 系列之后,ArcGIS 没有直接的缩放 API,但是我们可以通过设置地图比例尺来达到这个效果。
// 数字小,代表放大
mapView.setViewpointScaleAsync(1000)
// 数字大,代表缩小
mapView.setViewpointScaleAsync(20000)
添加图层
在 100 系列中,ArcGIS 按图层的用途分为两类,一类底图,一类操作图层。
- 添加底图
ArcGISMap map = new ArcGISMap(); map.setBasemap(new Basemap(layer));
- 添加操作图层
// 获取操作图层集合 LayerList layers = mapView.getMap().getOperationalLayers(); // 把图层添加到图层集合中 layers.add(layer)
切换底图
重新设置一个 Basemap 对象即可
mapView.setBasemap(new Basemap(layer));
定位当前位置
LocationDisplay display = mapView.getLocationDisplay();
// 设置定位模式,
display.setAutoPanMode(LocationDisplay.AutoPanMode.OFF);
display.setShowPingAnimation(false);
display.setShowAccuracy(true);
display.setOpacity(0.75f);
display.setShowLocation(true);
display.startAsync();
把图斑显示在屏幕中间
mapView.setViewpointGeometryAsync(geometry);
显示点、线、面等图斑
ArcGIS 中如果想要显示加载点、线、面等图斑,或者标注。都是在 GraphicsOverlay 临时层中展示,
- 添加一个临时层到 mapView
GraphicsOverlay graphicsOverlay = new GraphicsOverlay(); mapView.graphicsOverlays.add(graphicsOverlay);
- 设置图斑的样式
// 点样式 SimpleMarkerSymbol pointSymbol = new SimpleMarkerSymbol(SimpleMarkerSymbol.Style.CIRCLE, Color.RED, 6); // 线样式, // SimpleLineSymbol.Style.SOLID 实线 // SimpleLineSymbol.Style.DASH 虚线 // ... SimpleLineSymbol lineSymbol = new SimpleLineSymbol(SimpleLineSymbol.Style.SOLID, Color.RED, 3); // 面样式 // SimpleFillSymbol.Style.SOLID 纯色填充 // SimpleFillSymbol.Style.DIAGONAL_CROSS 对角填充 // ... SimpleFillSymbol fillSymbol = new SimpleFillSymbol(SimpleFillSymbol.Style.SOLID, Color.parseColor("#88F44336"), lineSymbol);
- 把点、线、面等图斑或标注添加到临时层
Graphic pointGraphic = new Graphic(point, pointSymbol); graphicsOverlay.getGraphics().add(pointGraphic)
添加文字标注
int textSize = 16;
TextSymbol textSymbol = new TextSymbol(textSize, text, Color.RED,
TextSymbol.HorizontalAlignment.LEFT, TextSymbol.VerticalAlignment.TOP);
Graphic txtgraphic = new Graphic(geometry , textSymbol);
GraphicsOverlay drawOverlay = new GraphicsOverlay();
drawOverlay.getGraphics().add(graphic);
添加图片标注
BitmapDrawable startDrawable = (BitmapDrawable) ContextCompat.getDrawable(mContext, R.drawable.ic_point_empty);
try {
PictureMarkerSymbol symbol = null;
symbol = PictureMarkerSymbol.createAsync(startDrawable).get();
symbol.loadAsync();
// 设置图片标注的宽高
symbol.setWidth(35);
symbol.setHeight(40);
// 设置偏移
symbol.setOffsetY(17);
PictureMarkerSymbol finalSymbol = symbol;
symbol.addDoneLoadingListener(() -> {
LoadStatus loadStatus = finalSymbol.getLoadStatus();
if (loadStatus == LoadStatus.LOADED) {
//添加新图形
Graphic pinSourceGraphic = new Graphic(point, finalSymbol);
heightOverlay.getGraphics().add(graphic);
} else {
finalSymbol.getLoadError().printStackTrace();
}
});
} catch (ExecutionException | InterruptedException e) {
e.printStackTrace();
}
添加气泡
Callout callout = mapView.getCallout();
// 设置气泡显示位置
callout.setLocation(geometry.getExtent().getCenter());
// 设置气泡显示内容
callout.setContent(view);
callout.show();