ArcGIS for Android 100.3.0(7):繪製圖層(GraphicsOverlay) ,符號和渲染器(Symbols and Renderers)
繪製圖層GraphicsOverlay
100版本單獨把繪製圖層拿出來的一個好處就是,它將永遠置於地圖內容之上。在以前我們如果要在MapView里加載一個新的圖層,必須先remove帶有圖形的FeatureLayer,載入這個圖層之後再載入一次帶有圖形的FeatureLayer,否則我們這個FeatureLayer將會被新載入的圖層所覆蓋。現在這個問題就不復存在了。
GraphicsOverlay graphicsOverlay1 = new GraphicsOverlay();
GraphicsOverlay graphicsOverlay2 = new GraphicsOverlay(GraphicsOverlay.RenderingMode .DYNAMIC);
mMapView.getGraphicsOverlays().add(graphicsOverlay1);
GraphicsOverlay預設的構造模式一共有兩種:DYNAMIC和STATIC,預設的是動態渲染模式DYNAMIC。
當為DYNAMIC模式時候,每當圖形有變化時候GraphicsOverlay會立刻更新圖形;
而為STATIC模式時候,圖形變化了不會馬上更新,而是在進行地圖的縮放、旋轉和平移時候才更新圖層,適合於繪製圖層裡含有大量圖形時候使用,以防圖形更新太慢影響體驗。
舉個例子:繪製點
mMapView.setOnTouchListener(new DefaultMapViewOnTouchListener(this, mMapView) {
@Override
public boolean onSingleTapConfirmed(MotionEvent v) {
Point clickPoint = mMapView.screenToLocation (new android.graphics.Point(Math.round(e.getX()), Math.round(e.getY())));
GraphicsOverlay graphicsOverla=new GraphicsOverlay();
SimpleMarkerSymbol simpleMarkerSymbol = new SimpleMarkerSymbol(SimpleMarkerSymbol.Style.CIRCLE, Color.RED, 10);
Graphic pointGraphic = new Graphic(clickPoint,simpleMarkerSymbol);
graphicsOverlay.getGraphics().add(pointGraphic);
mMapView.getGraphicsOverlays().add(graphicsOverlay);
return true;
}
});
符號與渲染器(Symbols and Renderers),通過色彩值、大小、形狀、邊線、透明度來表達空間要素的具體含義。如果僅僅有位置,而沒有位置上的屬性資訊,位置也會失去意義,符號與渲染器是呈現位置上屬性資訊的最直接表達方式。對於Graphic(GraphicsOverlay),可以直接賦值符號或者設定渲染器,而對於Feature(FeatureLayer)只能通過渲染器進行設定。
符號Symbol使用
Symbol類是一個用來控制顯示圖形符號樣式的類。
1.點符號(MarkerSymbol)
點符號,顧名思義就是用來修飾Point的符號。包含三種類型,分別是
PictureMarkerSymbol(圖片點符號),
SimpleMarkerSymbol(簡單點符號),
TextSymbol(文字符號)。
2.線符號(LineSymbol)
線符號是用來修飾線PolyLine的符號。只有一種型別,SimpleLineSymbol(簡單線符號)。
3.面符號(FillSymbol)
面符號是用來修飾面Polygon的符號,一共有兩種PictureFillSymbol(圖片面符號), SimpleFillSymbol(簡單面符號)。
渲染Renderer使用
渲染也是設定圖形樣式,相比起符號來,渲染就相當於是批量的圖形樣式設定。但是當渲染和符號同時存在時候,會優先使用符號的樣式。
渲染一共包含了ClassBreaksRenderer, HeatmapRenderer, SimpleRenderer, UniqueValueRenderer, UnsupportedRenderer五大類,這裡簡要以SimpleRenderer介紹下吧。
單一符號渲染(SimpleRenderer)
官方案例:
public class AddGraphicsRendererActivity extends AppCompatActivity {
private MapView mMapView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add_graphics_renderer);
mMapView = (MapView) findViewById(R.id.mapView);
ArcGISMap mMap = new ArcGISMap(Basemap.Type.TOPOGRAPHIC, 15.169193, 16.333479, 2);
addGraphicsOverlay();
mMapView.setMap(mMap);
}
private void addGraphicsOverlay() {
// point graphic
Point pointGeometry = new Point(40e5, 40e5, SpatialReferences.getWebMercator());
// red diamond point symbol
SimpleMarkerSymbol pointSymbol = new SimpleMarkerSymbol(SimpleMarkerSymbol.Style.DIAMOND, Color.RED, 10);
// create graphic for point
Graphic pointGraphic = new Graphic(pointGeometry);
// create a graphic overlay for the point
GraphicsOverlay pointGraphicOverlay = new GraphicsOverlay();
// create simple renderer
SimpleRenderer pointRenderer = new SimpleRenderer(pointSymbol);
pointGraphicOverlay.setRenderer(pointRenderer);
// add graphic to overlay
pointGraphicOverlay.getGraphics().add(pointGraphic);
// add graphics overlay to the MapView
mMapView.getGraphicsOverlays().add(pointGraphicOverlay);
// line graphic
PolylineBuilder lineGeometry = new PolylineBuilder(SpatialReferences.getWebMercator());
lineGeometry.addPoint(-10e5, 40e5);
lineGeometry.addPoint(20e5, 50e5);
// solid blue line symbol
SimpleLineSymbol lineSymbol = new SimpleLineSymbol(SimpleLineSymbol.Style.SOLID, Color.BLUE, 5);
// create graphic for polyline
Graphic lineGraphic = new Graphic(lineGeometry.toGeometry());
// create graphic overlay for polyline
GraphicsOverlay lineGraphicOverlay = new GraphicsOverlay();
// create simple renderer
SimpleRenderer lineRenderer = new SimpleRenderer(lineSymbol);
// add graphic to overlay
lineGraphicOverlay.setRenderer(lineRenderer);
// add graphic to overlay
lineGraphicOverlay.getGraphics().add(lineGraphic);
// add graphics overlay to the MapView
mMapView.getGraphicsOverlays().add(lineGraphicOverlay);
//polygon graphic
PolygonBuilder polygonGeometry = new PolygonBuilder(SpatialReferences.getWebMercator());
polygonGeometry.addPoint(-20e5, 20e5);
polygonGeometry.addPoint(20e5, 20e5);
polygonGeometry.addPoint(20e5, -20e5);
polygonGeometry.addPoint(-20e5, -20e5);
// solid yellow polygon symbol
SimpleFillSymbol polygonSymbol = new SimpleFillSymbol(SimpleFillSymbol.Style.SOLID, Color.YELLOW, null);
// create graphic for polygon
Graphic polygonGraphic = new Graphic(polygonGeometry.toGeometry());
// create graphic overlay for polygon
GraphicsOverlay polygonGraphicOverlay = new GraphicsOverlay();
// create simple renderer
SimpleRenderer polygonRenderer = new SimpleRenderer(polygonSymbol);
// add graphic to overlay
polygonGraphicOverlay.setRenderer(polygonRenderer);
// add graphic to overlay
polygonGraphicOverlay.getGraphics().add(polygonGraphic);
// add graphics overlay to MapView
mMapView.getGraphicsOverlays().add(polygonGraphicOverlay);
}
@Override
protected void onPause() {
super.onPause();
mMapView.pause();
}
@Override
protected void onResume() {
super.onResume();
mMapView.resume();
}
@Override
protected void onDestroy() {
super.onDestroy();
mMapView.dispose();
}
}
效果圖:
唯一值渲染(UniqueValueRenderer)
UniqueValueRenderer表示具有匹配屬性的功能/圖形組。這在名義或字串資料中最常見。
UniqueValueRenderer用於繪製具有不同符號的多個要素/圖形,並將UniqueValueRenderer中的欄位名稱與UniqueValues中的值進行匹配。
示例:使用UniqueValueRenderer對分割槽指定進行符號化:黃色表示“住宅”,紫色表示“工業”,紅色表示“商業”,等等。
public class UniqueValueRendererActivity extends AppCompatActivity {
private MapView mMapView;
private String sample_service_url = "https://sampleserver6.arcgisonline.com/arcgis/rest/services/Census/MapServer/3";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_unique_value_renderer);
mMapView = (MapView) findViewById(R.id.mapView);
ArcGISMap map = new ArcGISMap(Basemap.createTopographic());
//[DocRef: Name=Unique Value Renderer, Topic=Symbols and Renderers, Category=Fundamentals]
// Create service feature table
ServiceFeatureTable serviceFeatureTable = new ServiceFeatureTable(sample_service_url);
// Create the feature layer using the service feature table
FeatureLayer featureLayer = new FeatureLayer(serviceFeatureTable);
// Override the renderer of the feature layer with a new unique value renderer
UniqueValueRenderer uniqueValueRenderer = new UniqueValueRenderer();
// Set the field to use for the unique values
//You can add multiple fields to be used for the renderer in the form of a list, in this case
uniqueValueRenderer.getFieldNames().add("STATE_ABBR");
// we are only adding a single field
// Create the symbols to be used in the renderer
SimpleFillSymbol defaultFillSymbol = new SimpleFillSymbol(SimpleFillSymbol.Style.NULL, Color.BLACK,
new SimpleLineSymbol(SimpleLineSymbol.Style.SOLID, Color.GRAY, 2));
SimpleFillSymbol californiaFillSymbol = new SimpleFillSymbol(SimpleFillSymbol.Style.SOLID, Color.RED,
new SimpleLineSymbol(SimpleLineSymbol.Style.SOLID, Color.RED, 2));
SimpleFillSymbol arizonaFillSymbol = new SimpleFillSymbol(SimpleFillSymbol.Style.SOLID, Color.GREEN,
new SimpleLineSymbol(SimpleLineSymbol.Style.SOLID, Color.GREEN, 2));
SimpleFillSymbol nevadaFillSymbol = new SimpleFillSymbol(SimpleFillSymbol.Style.SOLID, Color.BLUE,
new SimpleLineSymbol(SimpleLineSymbol.Style.SOLID, Color.BLUE, 2));
// Set default symbol
uniqueValueRenderer.setDefaultSymbol(defaultFillSymbol);
uniqueValueRenderer.setDefaultLabel("Other");
// Set value for california
List<Object> californiaValue = new ArrayList<>();
// You add values associated with fields set on the unique value renderer.
// If there are multiple values, they should be set in the same order as the fields are set
californiaValue.add("CA");
uniqueValueRenderer.getUniqueValues().add(
new UniqueValueRenderer.UniqueValue("California", "State of California", californiaFillSymbol,
californiaValue));
// Set value for arizona
List<Object> arizonaValue = new ArrayList<>();
// You add values associated with fields set on the unique value renderer.
// If there are multiple values, they should be set in the same order as the fields are set
arizonaValue.add("AZ");
uniqueValueRenderer.getUniqueValues()
.add(new UniqueValueRenderer.UniqueValue("Arizona", "State of Arizona", arizonaFillSymbol, arizonaValue));
// Set value for nevada
List<Object> nevadaValue = new ArrayList<>();
// You add values associated with fields set on the unique value renderer.
// If there are multiple values, they should be set in the same order as the fields are set
nevadaValue.add("NV");
uniqueValueRenderer.getUniqueValues()
.add(new UniqueValueRenderer.UniqueValue("Nevada", "State of Nevada", nevadaFillSymbol, nevadaValue));
// Set the renderer on the feature layer
featureLayer.setRenderer(uniqueValueRenderer);
//[DocRef: END]
// add the layer to the map
map.getOperationalLayers().add(featureLayer);
map.setInitialViewpoint(new Viewpoint(
new Envelope(-13893029.0, 3573174.0, -12038972.0, 5309823.0, SpatialReferences.getWebMercator())));
// set the map to be displayed in the mapview
mMapView.setMap(map);
}
@Override
protected void onPause() {
super.onPause();
// pause MapView
mMapView.pause();
}
@Override
protected void onResume() {
super.onResume();
// resume MapView
mMapView.resume();
}
@Override
protected void onDestroy() {
super.onDestroy();
// dispose MapView
mMapView.dispose();
}
}