import java.util.HashMap;
import java.util.Map;
import android.app.Activity;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
import com.esri.android.map.GraphicsLayer;
import com.esri.android.map.InfoTemplate;
import com.esri.android.map.Layer;
import com.esri.android.map.MapView;
import com.esri.android.map.event.OnSingleTapListener;
import com.esri.core.geometry.Envelope;
import com.esri.core.geometry.Geometry;
import com.esri.core.geometry.Point;
import com.esri.core.map.Graphic;
import com.esri.core.renderer.SimpleRenderer;
import com.esri.core.symbol.PictureMarkerSymbol;
import com.esri.core.symbol.Symbol;
public
class GraphicsLayerDemoActivity
extends Activity {
Button btn1;
MapView mMapView;
final String URL_STREET_COLD = "http://cache1.arcgisonline.cn/ArcGIS/rest/services/ChinaOnlineStreetCold/MapServer";
GraphicsLayer mGraphicsLayer;
final
int STATE_ADD_GRAPHIC = 1;
//
進入 “添加graphics狀态,這時候單擊地圖時操作就添加graphics
final
int STATE_SHOW = 2;
//
“選中graphics狀态“,這時候單擊地圖時操作就
//
選擇一個graphics,并顯示該graphics的附加資訊”
int m_State;
//
狀态
@Override
public
void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
m_State = STATE_SHOW;
btn1 = (Button) findViewById(R.id.btn1);
btn1.setText("準備添加要素");
btn1.setOnClickListener(
new OnClickListener() {
@Override
public
void onClick(View arg0) {
//
切換按鈕狀态,第一次點選本按鈕後進入 “添加graphics狀态,這時候單擊地圖時操作就添加graphics”
//
第一次點選本按鈕後進入 “選中graphics狀态“,這時候單擊地圖時操作就
//
選擇一個graphics,并顯示該graphics的附加資訊”
m_State = m_State == STATE_ADD_GRAPHIC ? STATE_SHOW
: STATE_ADD_GRAPHIC;
if (m_State == STATE_ADD_GRAPHIC) {
btn1.setText("單擊地圖将添加要素,單擊本按鈕結束");
}
else {
btn1.setText("準備添加要素");
}
}
});
mMapView = (MapView) findViewById(R.id.map);
//
Add layer to MapView
mMapView.addLayer(
new com.esri.android.map.ags.ArcGISTiledMapServiceLayer(
URL_STREET_COLD));
Envelope initextext =
new Envelope(12899459.4956466, 4815363.65520802,
13004178.2243971, 4882704.67712717);
mMapView.setExtent(initextext);
//
設定單擊事件
mMapView.setOnSingleTapListener(m_OnSingleTapListener);
}
OnSingleTapListener m_OnSingleTapListener =
new OnSingleTapListener() {
int m_Char = 65;
public
void onSingleTap(
float x,
float y) {
if (!mMapView.isLoaded()) {
return;
}
if (m_State == STATE_ADD_GRAPHIC) {
//
獲得圖層
AddNewGraphic(x, y);
}
else {
//
選中 Graphics
SelectOneGraphic(x, y);
}
//
end if else
}
//
end method
private
void SelectOneGraphic(
float x,
float y) {
//
獲得圖層
GraphicsLayer layer = GetGraphicLayer();
if (layer !=
null && layer.isInitialized() && layer.isVisible()) {
Graphic result =
null;
//
檢索目前 光标點(手指按壓位置)的附近的 graphic對象
result = GetGraphicsFromLayer(x, y, layer);
if (result !=
null) {
//
獲得附加特别的屬性
String msgTag = (String) result
.getAttributeValue("tag");
//
顯示提示
AlertMsg(msgTag);
}
//
end if
}
//
end if
}
private
void AddNewGraphic(
float x,
float y) {
GraphicsLayer layer = GetGraphicLayer();
if (layer !=
null && layer.isInitialized() && layer.isVisible()) {
//
轉換坐标
Point pt = mMapView.toMapPoint(
new Point(x, y));
//
附加特别的屬性
Map map =
new HashMap();
map.put("tag", "" + (
char) (m_Char++));
//
建立 graphic對象
Graphic gp1 = CreateGraphic(pt, map);
//
添加 Graphics 到圖層
layer.addGraphic(gp1);
}
}
};
private Graphic GetGraphicsFromLayer(
double xScreen,
double yScreen,
GraphicsLayer layer) {
Graphic result =
null;
try {
int[] idsArr = layer.getGraphicIDs();
double x = xScreen;
double y = yScreen;
for (
int i = 0; i
Graphic gpVar = layer.getGraphic(idsArr[i]);
if (gpVar !=
null) {
Point pointVar = (Point) gpVar.getGeometry();
pointVar = mMapView.toScreenPoint(pointVar);
double x1 = pointVar.getX();
double y1 = pointVar.getY();
if (Math.sqrt((x - x1) * (x - x1) + (y - y1) * (y - y1))
result = gpVar;
break;
}
}
}
}
catch (Exception e) {
return
null;
}
return result;
}
private Graphic CreateGraphic(Point geometry, Map map) {
GraphicsLayer layer = GetGraphicLayer();
//
獲得圖層
Drawable image = GraphicsLayerDemoActivity.
this.getBaseContext()
.getResources().getDrawable(R.drawable.pop);
PictureMarkerSymbol symbol =
new PictureMarkerSymbol(image);
//
建構graphic
//
Graphic g = new Graphic(geometry, symbol);
Graphic g =
new Graphic(geometry, symbol, map,
null);
return g;
}
private GraphicsLayer GetGraphicLayer() {
if (mGraphicsLayer ==
null) {
mGraphicsLayer =
new GraphicsLayer();
mMapView.addLayer(mGraphicsLayer);
}
return mGraphicsLayer;
}
void AlertMsg(String str, Object... arg) {
String msg = String.format(str, arg);
Toast.makeText(
this, msg, 2).show();
Log.i("AlertMsg", msg);
}
@Override
protected
void onDestroy() {
super.onDestroy();
}
@Override
protected
void onPause() {
super.onPause();
mMapView.pause();
}
@Override
protected
void onResume() {
super.onResume();
mMapView.unpause();
}
}