[android学习]给地图添加要素

2012.11.14  22:25

今天上班好忙乱-  -不知道该说好还是不好……


在地图上画点东西吧

先吐嘈一下arcgis api for android的api帮助文档,可以再不方便一点么?

http://help.arcgis.com/en/arcgismobile/10.0/apis/android/api/index.html或许是我太过愚笨,这个index实在太不好使了,而且我很想找一下v2.0的帮助文档在哪里-  -虽然目前有很多ESRI中国的朋友在发教学帖子,但是对于我辈菜到家来说,有个好用的API还是很快乐的啊。

吐嘈暂放一边,想来for android跟AE应该还是挺相像的,在AE里有MarkerSymbol,LineSymbol,FillSymbol对应 点线面,还有TextSymbol,3D Chart,想来for android应该也有。

虽然帮助文档用着不大顺手,不过东西还是全的

Class Summary
FillSymbol Base class for polygon symbols.
LineSymbol Base class for line symbols.
MarkerSymbol The base class for all the marker symbol.
MultiLayerSymbol MultiLayerSymbol class indicates that the symbol is a multilayer symbol.
PictureMarkerSymbol Used to draw points and multipoints on the graphics layer using an image.
SimpleFillSymbol Used to draw polygon features on the graphics layer using simple patterns.
SimpleLineSymbol Used to draw linear features on the graphics layer.
SimpleMarkerSymbol Used to draw points and multipoints (or nodes of polylines and polygons) on the graphics layer using simple markers.
SymbolHelper A convenient class which parses JSON representation of the symbol into a Symbol instance.
TextSymbol Used to display text at points on the graphics layer.
那么就来试一试吧,用最简单的SympleMarkerSymbol画个点,当我们用手指在地图上点击的时候,就画一个点。

修改oncreate方法,

public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_map);
        
     // Retrieve the map and initial extent from XML layout
     	map = (MapView)findViewById(R.id.map);
     	// Add tiled layer to MapView
     	tileLayer = new ArcGISTiledMapServiceLayer("http://services.arcgisonline.com/ArcGIS/rest/services/World_Street_Map/MapServer");
     	map.addLayer(tileLayer);
     	
     	graphicsLayer=new GraphicsLayer();
     	map.addLayer(graphicsLayer);
     	map.setOnSingleTapListener(new OnSingleTapListener(){
			public void onSingleTap(float x, float y) {
				// TODO Auto-generated method stub
				Graphic graphic=new Graphic(map.toMapPoint(x, y), new SimpleMarkerSymbol(Color.RED, 15, STYLE.CIRCLE));
				graphicsLayer.addGraphic(graphic);
				Log.v(TAG, "add simpleMarker");
			}
        });
        Log.v(TAG, "begin");
        Button btnSubmit=(Button) findViewById(R.id.exit);
        btnSubmit.setOnClickListener(new OnClickListener(){
			public void onClick(View v) {
				// TODO Auto-generated method stub
                finish();//关闭当前Activity  
			}
        	
        });
    }
目前还不需要在布局文件中添加神马。

PS 我能想到最尴尬的事,就是当准备调试的时候报Installation error: INSTALL_FAILED_INSUFFICIENT_STORAGE 存储空间不足-  -

这么一个破程序居然有16.66MB,我该先删掉一些包么?


2012.11.15 20:25

可以添加点就可以添加图片

GraphicsLayer是很有包容性的图层,可以add的graphic包括了Geometry、Symbol、attributes、InforTemplate,而Symbol如上所见,恰好包括了PictureMarkerSymbol ,

修改代码,

//Graphic graphic=new Graphic(map.toMapPoint(x, y), new SimpleMarkerSymbol(Color.RED, 15, STYLE.CIRCLE));
Resources res=getResources();
Drawable drawable=res.getDrawable(R.drawable.talkico);
Graphic graphic=new Graphic(map.toMapPoint(x,y),new PictureMarkerSymbol(drawable));
graphicsLayer.addGraphic(graphic);
PictureMarkerSymbol有四种构造函数

PictureMarkerSymbol(android.graphics.drawable.Drawable drawable)
          Instantiates a PictureMarkerSymbol with a Drawable image.
PictureMarkerSymbol(org.codehaus.jackson.JsonNode node)
          Instantiates an object of PictureMarkerSymbol from a JsonNode.
PictureMarkerSymbol(PictureMarkerSymbol symbol)
           
PictureMarkerSymbol(String pictureUrl)
          Instantiates a PictureMarkerSymbol with a URL of an image.
这里使用了第一种

那么怎么得到drawable呢?

android.graphics.drawable.Drawable

drawable : A Drawable is a general abstraction for "something that can be drawn."官方说法是这么定义的,drawable有很多,可以参见官方API 秉承android的一贯作风,这些drawable都可以在xml文件中声明(You can create most of these drawables using XML, as described in Drawable Resources.)。

如何获得android.graphics.drawable.Drawable,最全的参考资料请戳这里

一般的要取得图片有三种情况,第一种是上面那样,图片就在资源里,就可以直接从资源里拿

Resources res=getResources();
Drawable drawable=res.getDrawable(R.drawable.talkico);//通过id
第二种是图片放在assets下面,那就需要使用InputStream

static Drawable createFromStream(InputStream is, String srcName)
Create a drawable from an inputstream
使用到InputStream的还有

static Drawable createFromResourceStream(Resources res, TypedValue value, InputStream is, String srcName, BitmapFactory.Options opts)
Create a drawable from an inputstream, using the given resources and value to determine density information.
static Drawable createFromResourceStream(Resources res, TypedValue value, InputStream is, String srcName)
Create a drawable from an inputstream, using the given resources and value to determine density information.
还一种情况,图片在sdcard里,

static Drawable

createFromPath(String pathName)
Create a drawable from file path name.
String baseDir = Environment.getExternalStorageDirectory().getAbsolutePath();
                String filePath=baseDir+"/DCIM/a.jpg";
                Drawable drawable=Drawable.createFromPath(filePath);

在调试的过程中,这种方法应该是取到了图片了,但是

Graphic graphic=new Graphic(map.toMapPoint(x,y),new PictureMarkerSymbol(drawable));
				graphicsLayer.addGraphic(graphic);

这一步反应很慢,最后出来的结果是一个问号,图片没有得到。

还有一种方法是先得到bitmap

FileInputStream fis;
				
				try {
					fis = new FileInputStream(filePath);
					Bitmap bmp = BitmapFactory.decodeStream(fis);
					BitmapDrawable bd=new BitmapDrawable(bmp);//过期
					Drawable drawable=bd;
					Graphic graphic=new Graphic(map.toMapPoint(x,y),new PictureMarkerSymbol(drawable));
					graphicsLayer.addGraphic(graphic);
					Log.v(TAG, "add simpleMarker");
				} catch (FileNotFoundException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}

也是一样的结果,还得再研究研究




發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章