ArcGIS Api For Android学习之(三)画点、线、面

本节目标:实现学习DrawGraphicElements例子利用GraphicsLayer在地图上画点、线和面。

关键要点:
1.
屏幕点击事件监听。
2.
符号的设置
具体代码如下:
package com.esri.arcgis.android.samples.helloworld;

 

import java.util.Map;

 

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.Color;
import android.os.Bundle;
import android.widget.Button;
import android.widget.Toast;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;

 

import com.esri.android.map.GraphicsLayer;
import com.esri.android.map.MapOnTouchListener;
import com.esri.android.map.MapView;
import com.esri.android.map.ags.ArcGISDynamicMapServiceLayer;
import com.esri.android.map.ags.ArcGISTiledMapServiceLayer;

 

importcom.esri.android.map.MapOnTouchListener;

 


import com.esri.core.geometry.Envelope;
import com.esri.core.geometry.MultiPath;
import com.esri.core.geometry.Point;
import com.esri.core.geometry.Polygon;
import com.esri.core.geometry.Polyline;
import com.esri.core.map.Graphic;
import com.esri.core.renderer.SimpleRenderer;
import com.esri.core.symbol.SimpleFillSymbol;
import com.esri.core.symbol.SimpleLineSymbol;
import com.esri.core.symbol.SimpleMarkerSymbol;
import com.esri.core.symbol.SimpleMarkerSymbol.STYLE;

 


public class HelloWorld extends Activity {
MapView map = null;
private Button btnAdd;
private Button btnGo;
public Button btnPoint;
private GraphicsLayer graphicsLayer = null;
// private MyTouchListener myListener = null;
final String[] geometryTypes = new String[] { “
”, “线”,
” };
int selectedGeometryIndex = -1;

MyTouchListener myListener = null;

/** Called when the activity is first created. */
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
map = (MapView)findViewById(R.id.map);
myListener = new MyTouchListener(HelloWorld.this, map);
map.setOnTouchListener(myListener);
this.btnAdd = (Button)findViewById(R.id.btn01);
this.btnAdd.setOnClickListener(new Button.OnClickListener()
{
public void onClick( View v )
{
// TODO Auto-generated method stub
map = (MapView)findViewById(R.id.map);
// Add dynamic layer to MapView
map.addLayer(new com.esri.android.map.ags.ArcGISTiledMapServiceLayer(“” +
http://192.168.1.201:8399/arcgis/rest/services/idw/MapServer
“));

Envelope initextext = new Envelope(12922237.7300255, 4831759.39777517,12991948.2998215, 4873341.14116225);

 

map.setExtent(initextext);

}
});
this.btnGo=(Button)findViewById(R.id.btn02);
this.btnGo.setOnClickListener(new Button.OnClickListener(){
public void onClick( View v )
{
// TODO Auto-generated method stub
map = (MapView)findViewById(R.id.map);
// Add dynamic layer to MapView
map.addLayer(new com.esri.android.map.ags.ArcGISTiledMapServiceLayer(“” +
http://cache1.arcgisonline.cn/ArcGIS/rest/services/ChinaCities_Community_BaseMap_ENG/ShenYang_Community_BaseMap_ENG/MapServer
“));

Envelope initextext = new Envelope(13700260.2294766, 5108777.85728174,13769970.7992726, 5150359.60066882);

map.setExtent(initextext);
}

});



// Retrieve the map and initial extent from XML layout
map = (MapView)findViewById(R.id.map);
// Add dynamic layer to MapView
map.addLayer(new ArcGISTiledMapServiceLayer(“” +
http://services.arcgisonline.com/ArcGIS/rest/services/World_Street_Map/MapServer
“));
//map.addLayer(new com.esri.android.map.ags.ArcGISTiledMapServiceLayer(“” +
// ”
http://192.168.1.201:8399/arcgis/rest/services/idw/MapServer
“));
//glayer = (MapView)findViewById(R.id.map);
graphicsLayer = new GraphicsLayer();
map.addLayer(graphicsLayer);
//myListener = new MapOnTouchListener(HelloWorld.this,map);
//map.setOnTouchListener(myListener);

btnPoint = (Button) findViewById(R.id.btn03);
btnPoint.setOnClickListener(new View.OnClickListener() {

public void onClick(View v) {
showDialog(0);
//myListener.setType(“POINT”);
}
});

//Retrieve the non-configuration instance data that was previously returned.
Object init = getLastNonConfigurationInstance();
if (init != null) {
map.restoreState((String) init);
}
}

 

//public boolean onTouchEvent(MotionEventevent)

 

// {

 

 

//this.btnPoint.setText(“你在触摸屏上进行了滑动”);
//Graphic graphic = new Graphic(map.toMapPoint(new Point(event.getX(), event
// .getY())),new SimpleMarkerSymbol(Color.RED,25,STYLE.CIRCLE));
//graphic.setGeometry();
//graphicsLayer.addGraphic(graphic);

 

 

// return super.onTouchEvent(event);

 

// }

 

class MyTouchListener extendsMapOnTouchListener
{

 

MultiPath poly;
String type = “”;
Point startPoint = null;

 

public MyTouchListener(Context context,MapView view) {
super(context, view);
}

 

public void setType(String geometryType) {
this.type = geometryType;
}

 

public String getType() {
return this.type;
}
//
画点

public boolean onSingleTap(MotionEvent e) {
if (type.length() > 1 && type.equalsIgnoreCase(“POINT”))
{
graphicsLayer.removeAll();
Graphic graphic = new Graphic(map.toMapPoint(new Point(e.getX(), e
.getY())),new SimpleMarkerSymbol(Color.RED,25,STYLE.CIRCLE));
//graphic.setGeometry();
graphicsLayer.addGraphic(graphic);
return true;
}
return false;

 

}
//
画线

public boolean onDragPointerMove(MotionEvent from, MotionEvent to) {
if (type.length() > 1
&& (type.equalsIgnoreCase(“POLYLINE”) || type
.equalsIgnoreCase(“POLYGON”))) {

 

Point mapPt = map.toMapPoint(to.getX(),to.getY());

 

/*
* if StartPoint is null, create a polyline and start a path.
*/
if (startPoint == null) {
graphicsLayer.removeAll();
poly = type.equalsIgnoreCase(“POLYLINE”) ? new Polyline()
: new Polygon();
startPoint = map.toMapPoint(from.getX(), from.getY());
poly.startPath((float) startPoint.getX(),
(float) startPoint.getY());

 

/*
* Create a Graphic and add polyline geometry
*/
Graphic graphic = new Graphic(startPoint,new SimpleLineSymbol(Color.RED,5));

 

/*
* add the updated graphic to graphics layer
*/
graphicsLayer.addGraphic(graphic);
}

 

poly.lineTo((float) mapPt.getX(), (float)mapPt.getY());

return true;
}
return super.onDragPointerMove(from, to);

 

}
//
画面

public boolean onDragPointerUp(MotionEvent from, MotionEvent to) {
if (type.length() > 1
&& (type.equalsIgnoreCase(“POLYLINE”) || type
.equalsIgnoreCase(“POLYGON”))) {

 

/*
* When user releases finger, add the last point to polyline.
*/
if (type.equalsIgnoreCase(“POLYGON”)) {
poly.lineTo((float) startPoint.getX(),
(float) startPoint.getY());
graphicsLayer.removeAll();
graphicsLayer.addGraphic(new Graphic(poly,new SimpleFillSymbol(Color.RED)));

}
graphicsLayer.addGraphic(new Graphic(poly,new SimpleLineSymbol(Color.BLUE,5)));
startPoint = null;

return true;
}
return super.onDragPointerUp(from, to);
}
}

 

protected Dialog onCreateDialog(int id) {

 

return newAlertDialog.Builder(HelloWorld.this)
.setTitle(“Select Geometry”)
.setItems(geometryTypes, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
graphicsLayer.removeAll();

 

// ignore first element
Toast toast = Toast.makeText(getApplicationContext(),
”", Toast.LENGTH_LONG);
toast.setGravity(Gravity.BOTTOM, 0, 0);

 

// Get item selected by user.
String geomType = geometryTypes[which];
//label.setText(geomType + ” selected.”);
selectedGeometryIndex = which;

 

// process user selection
if (geomType.equalsIgnoreCase(“
”)) {
myListener.setType(“POLYGON”);

} else if (geomType.equalsIgnoreCase(“
线
”)) {
myListener.setType(“POLYLINE”);

} else if (geomType.equalsIgnoreCase(“
”)) {
myListener.setType(“POINT”);

}

 


}
}).create();

 

}

protected void onPause() {
super.onPause();
map.pause();
}

 

protected void onResume() {
super.onResume();
map.unpause();
}
}

 

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值