Arcgis for Androd API開發系列教程(一)——地圖顯示與GPS定位

序:最近呢,工作鴨梨不是怎麼大,對於自己愛折騰的想法又冒出了水面,開始自己的android開發的學習之旅。但是呢,本人是做GIS的,所以呢,就打算從這方面入手看看,是不是有什麼比較好玩的玩意呢,這才導致了“Arcgis for Androd API開發系列教程”的成功問世……

本篇呢,是用“Arcgis for Androd API”實現基本的地圖顯示並在圖上顯示當前GPS所在位置。爲了比較直觀的讓大家看看本人的成果呢,先給大家上賬圖吧:

看見了吧,人所在的位置呢就是本人所處的位置……知道要做什麼了之後,下面給大家說一下具體的實現方法吧。

在做Arcgis for Android API開發之前,你得做一件大事,那就是搭建Android的開發環境,至於怎麼搭建,我在此就不再說了,本來沒打算說這玩意的。安卓開發環境搭建完成之後了,你需要需要安裝 ArcGIS 發相關的庫和 Eclipse插件了,這個的安裝呢,你可以選擇在線的安裝方式,也可以選擇離線的安裝方式,在線的比較簡單,Eclipse菜單/help/Install New Softwear...,在彈出的框框裏面輸入http://downloads.esri.com/software/arcgis/android即可,離線的更省事,不過你本機得有ArcGISAndroidSDK_v????.zip,沒有的那彆着急,你可以去網站上找,不想找的呢,我呢也給大家共享了,下載地址爲:http://download.csdn.net/detail/gisshixisheng/6703689,大家按需下載,不受積分的。

靠,廢話一大堆,終於到主題了!上面的工作完成之後呢,首先你得新建一個Arcgis for Android的工程,暫且就叫做MapGps吧,建成之後文件組織形式如下:

我想,做過安卓開發或者瞭解安卓開發的人呢對着玩意肯定不陌生吧,具體的我也不做解釋,有疑問的我們可以私聊,最好是美女……不過呢,有些東西呢,還是交代一下吧:

1、src

這個東東我不怎麼清楚,個人認爲類似於web開發的後臺

2、libs

這個是開發相關的類庫

3、res

英語差不多的人應該明白,res是resources的簡寫,是“資源”的意思。其中,darwable命名的文件夾是一些圖片文件,layout是一些佈局文件,values是一些值文件,裏面包括string,color等等……這個layout類似於web的前臺吧……

首先,來看看main.xml文件的內容:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >
    
    <!-- MapView layout and initial extent -->
    <com.esri.android.map.MapView
  		android:id="@+id/map"
  		android:layout_width="fill_parent"
  		android:layout_height="fill_parent">
  	</com.esri.android.map.MapView>
  	
  	<com.esri.arcgis.android.samples.helloworld.ZoomControlView
        android:id="@+id/ZoomControlView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
        android:layout_marginBottom="20.0dip"
        android:layout_marginRight="5.0dip"/>

  	<ZoomControls
  	    android:id="@+id/zoomCtrl"
  	    android:layout_width="wrap_content"
  	    android:layout_height="wrap_content"
  	    android:layout_alignParentTop="true"
        android:layout_alignParentRight="true"
        android:layout_marginTop="20.0dip"
        android:layout_marginRight="5.0dip" />

  	<android.widget.SearchView
  	    android:id="@+id/searchView"
  	    android:layout_width="wrap_content"
  	    android:layout_height="wrap_content"
  	    android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_marginTop="20.0dip"
        android:layout_marginLeft="5.0dip" />

  	<Button
  	    android:id="@+id/btnGps"
  	    android:layout_width="wrap_content"
  	    android:layout_height="wrap_content"
  	    android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:layout_marginBottom="20.0dip"
        android:layout_marginLeft="5.0dip"
  	    android:text="GPS" />
  	
</RelativeLayout>

接着,看看HelloWorld.java的內容:

/* Copyright 2012 ESRI
 *
 * All rights reserved under the copyright laws of the United States
 * and applicable international laws, treaties, and conventions.
 *
 * You may freely redistribute and use this sample code, with or
 * without modification, provided you include the original copyright
 * notice and use restrictions.
 *
 * See the �Sample code usage restrictions� document for further information.
 *
 */

package com.esri.arcgis.android.samples.helloworld;

import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ZoomControls;

import com.esri.android.map.GraphicsLayer;
import com.esri.android.map.MapView;
import com.esri.android.map.ags.ArcGISTiledMapServiceLayer;
import com.esri.core.geometry.GeometryEngine;
import com.esri.core.geometry.Point;
import com.esri.core.geometry.SpatialReference;
import com.esri.core.map.Graphic;
import com.esri.core.symbol.PictureMarkerSymbol;

public class HelloWorld extends Activity {
	Button zoomin;
	Button zoomout;	
	Button btnGps;
	ZoomControls zoomctrl;
	LocationManager locMag;
	Location loc ;
	
	MapView map = null;
	ArcGISTiledMapServiceLayer tileLayer;
	GraphicsLayer gLayerPos; 
	Point point;
	Point wgspoint;
	Point mapPoint;
	PictureMarkerSymbol locationSymbol;
	
	ZoomControlView mZoomControlView;
	
	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		
		map = (MapView)findViewById(R.id.map);
		
		tileLayer = new ArcGISTiledMapServiceLayer(
				"http://cache1.arcgisonline.cn/ArcGIS/rest/services/ChinaOnlineCommunity/MapServer");
		
		map.addLayer(tileLayer);
		
		//設置地圖中心點
		point = (Point) GeometryEngine.project(new Point(40.805, 111.661),SpatialReference.create(4326),map.getSpatialReference());
		map.centerAt(point, true);
		/*
		zoomin=(Button)findViewById(R.id.zoomin);
		zoomout=(Button)findViewById(R.id.zoomout);
		zoomin.setOnClickListener(new OnClickListener(){
			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				map.zoomin();
			}			
		});
		zoomout.setOnClickListener(new OnClickListener(){
			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				map.zoomout();
			}			
		});*/
		//放大與縮小——自定義
		mZoomControlView = (ZoomControlView) findViewById(R.id.ZoomControlView);
		mZoomControlView.setMapView(map);
		
		//放大與縮小ZoomControls
		zoomctrl=(ZoomControls)findViewById(R.id.zoomCtrl);
		zoomctrl.setOnZoomInClickListener(new OnClickListener(){
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				map.zoomin();
			}			
		});
		zoomctrl.setOnZoomOutClickListener(new OnClickListener(){
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				map.zoomout();
			}			
		});
		
		gLayerPos = new GraphicsLayer();
	    map.addLayer(gLayerPos);	    
	   
	    locationSymbol =  new PictureMarkerSymbol(this.getResources().getDrawable(
				R.drawable.location));
	    
	    //要定位在地圖中的位置,需要知道當前位置,而當前位置有Location對象決定,
	  	//但是,Location對象又需要LocationManager對象來創建。
	    //創建LocationManager的唯一方法
	  	locMag = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
	  	//獲得Provider列表
	  	final List<String> providers=locMag.getProviders(true);
	  	//循環Provider,根據Provider獲取位置信息
	  	for(String provider:providers)
	  	{
		    loc = locMag.getLastKnownLocation(provider);
		  	
		    LocationListener locationListener = new LocationListener(){
		    	/**
		    	 * 位置改變時調用
		    	 */
		    	public void onLocationChanged(Location location) {
					//刷新圖層
					markLocation(location);
				}
		    	//Provider失效時調用
				public void onProviderDisabled(String arg0) 
				{
				}
				//Provider生效時調用
				public void onProviderEnabled(String arg0) 
				{
				}
				//狀態改變時調用
				public void onStatusChanged(String arg0, int arg1, Bundle arg2) 
				{
				}
			};		
			locMag.requestLocationUpdates(provider, 100, 0, locationListener);
			if(loc!=null)
			{
				//開始畫圖
				markLocation(loc);
			}
		}
	}
	
	private void markLocation(Location location)
	{		
		gLayerPos.removeAll();
		double locx = location.getLongitude();
		double locy = location.getLatitude();
		wgspoint = new Point(locx, locy);  
		mapPoint = (Point) GeometryEngine.project(wgspoint,SpatialReference.create(4326),map.getSpatialReference());
	
		//圖層的創建
		Graphic graphic = new Graphic(mapPoint,locationSymbol);
		gLayerPos.addGraphic(graphic);	
		map.centerAt(mapPoint, true);
	}

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

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

附件:

點此下載源碼 點此現在類庫包

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