[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();
				}

也是一樣的結果,還得再研究研究




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