1. 程式人生 > >ArcGIS for Android 10.2.9(5):GraphicsLayer移除Graphic,GraphicsLayer常用API

ArcGIS for Android 10.2.9(5):GraphicsLayer移除Graphic,GraphicsLayer常用API

**API提供三個移除Graphic的方法:
1.public void removeAll ()刪除所有Graphic。
2.public void removeGraphic (int id)使用唯一ID刪除Graphic。
3.public void removeGraphics (int [] ids)使用唯一ID陣列刪除Graphic。**

如果想移除指定Graphic,就必須獲取Graphic的id,但是 graphic.getUid()獲取的值總是返回-1,所以就不能實現移除指定Graphic

解決辦法:

1.為每個Graphic設定屬性:

  Map<String
, Object> attr = new HashMap<>(); attr.put("name", name); Graphic graphic = new Graphic(mGeometry, fillSymbol, attr);

2.使用GraphicsLayer類的getGraphicIDs()方法獲取所有的 uid,然後再根據這個 uid獲取對應的Graphic,最後遍歷判斷這個Graphic是否是你想要的。

/**
     * 獲取 Graphic的uid
     *
     * @param name
     * @return
     */
private int getUid(String name) { int uid = -1; int[] graphicIDs = mGraphicsLayer.getGraphicIDs(); if (graphicIDs != null && graphicIDs.length > 0) { for (int graphicID : graphicIDs) { Graphic graphic = mGraphicsLayer.getGraphic(graphicID); if
(graphic == null) { uid = -1; //根據name判斷是否是你需要的graphic } else if (graphic.getAttributes().get("name").equals(name)) { uid = graphicID; } } } return uid; }

3.最後就可以移除Graphic:

 int uid = getUid(id);
 if (uid != -1) {
  //根據id移除Graphic
  mGraphicsLayer.removeGraphic(uid);
 }

也可以用下面方法來隱藏和顯示Grapjic:

public void setGraphicVisible (int uid,boolean visible):使用唯一的ID設定圖形的可見性。
public void setRenderer (Renderer renderer):設定GraphicsLayer的渲染器。

  //高亮顯示
  Graphic graphic = new Graphic(geometry, symbol, attributes);
  SimpleRenderer sr = new SimpleRenderer(new SimpleFillSymbol(Color.RED));
  mGraphicsLayer.setRenderer(sr);
  mGraphicsLayer.addGraphic(graphic);
public int addGraphic (Graphic graphic):將圖形新增到GraphicsLayer中。
如果新增成功,則表示新增的圖形的唯一ID。-1,如果新增失敗。

public int [] addGraphics (Graphic [] graphics)
新增一個Graphic陣列。

public void bringToFront (int id)
將圖形放在前面。

public Graphic getGraphic (int uid)
使用唯一的ID來返回圖形例項。

public int [] getGraphicIDs ()
檢索表示此圖層中所有圖形的唯一ID陣列。

public int getNumberOfGraphics ()
返回圖形的數量。

public void updateGraphic (int id,Map <String,Object> attributes)
使用屬性對映更新由id指定的圖形的屬性。

public void updateGraphic (int id,Symbol symbol)
用符號更新由id指定的圖形的符號屬性。

public void updateGraphic (int id,Graphic graphic)
使用另一個圖形例項的屬性更新由id指定的圖形。其圖形順序也更新。