有时我们需要在arcgis地图上创建点线面(多边形)等图形。下面我们根据官方文档,梳理一下具体思路。包括Graphic创建,在地图上被选中,移动Graphic的位置的整个思路及代码。
一、创建GraphicsOverlay
- 创建GraphicsOverlay对象,并把此对象加入mapView中
// create a graphics overlay
graphicsOverlay = new GraphicsOverlay();
- 创建Graphic对象,Graphic对象可以包括点Point或线polyline或多边形(面)polygon。下面以点Point为例列出代码。最后把Graphic对象放入刚才创建的GraphicsOverlay对象中。其中可以对点线面的样式进行描述。
// create a map point for the Santa Monica pier
Point pierPoint = new Point(-118.4978, 34.0086, SpatialReferences.getWgs84());
// create a red (0xFFFF0000) circle simple marker symbol
SimpleMarkerSymbol redCircleSymbol = new SimpleMarkerSymbol(SimpleMarkerSymbol.Style.CIRCLE, 0xFFFF0000, 10);
// create a graphic from the point and symbol
Graphic pierGraphic = new Graphic(pierPoint, redCircleSymbol);
// add the graphic to the graphics overlay
graphicsOverlay.getGraphics().add(pierGraphic);
- 把 graphicsOverlay 对象 加入mapView中。这样地图上就会显示相应的点或线或面了。
// add graphics overlay to the map view's graphics overlay collection
mapView.getGraphicsOverlays().add(graphicsOverlay);
二、在graphicsOverlay选中,识别选中的图形selectedGraphic
使用mapView的identifyGraphicsOverlayAsync方法进和识别选中。具体代码如下:
// identify graphics on the graphics overlay
final ListenableFuture<IdentifyGraphicsOverlayResult> identifyFuture = mGeoView.identifyGraphicsOverlayAsync(graphicsOverlay,
screenPoint, 10, false, 10);
identifyFuture.addDoneListener(new Runnable() {
@Override
public void run() {
try {
// get the list of graphics returned by identify
List<Graphic> identifiedGraphics = identifyFuture.get().getGraphics();
if (!identifiedGraphics.isEmpty()) {
selectedGraphic=identifiedGraphics.get(0);
selectedGraphic.setSelected(true);
}
/* // iterate the graphics
for (Graphic graphic : identifiedGraphics) {
// Use identified graphics as required, for example access attributes or geometry, select, build a table, etc...
graphic.setSelected(true);
}*/
} catch (InterruptedException | ExecutionException ex) {
dealWithException(ex); // must deal with checked exceptions
}
}
});
三、移动点或线或面的位置
使用刚才识别或选中的点或线或面的selectedGraphic,修改位置。具体代码如下:
// create a point from location clicked
mapView.setOnMouseClicked(e -> {
if (e.getButton() == MouseButton.PRIMARY) {
Point2D geoViewPoint = new Point2D(e.getX(), e.getY());
// add new location to selected graphic
Point geoPoint = mapView.screenToLocation(geoViewPoint);
selectedGraphic.setGeometry(geoPoint);
}
});
愿意看官方英文文档的朋友,可以查看下面相关文档,也可以相互沟通学习。
https://developers.arcgis.com/android/maps-2d/add-graphics-to-a-map-view/