Arcgis API for Android之GPS定位

歡迎大家加入Arcgis API for Android的QQ交流羣:337469080

先說說寫這篇文章的原因吧,在羣內討論的過程中,有人提到了定位的問題,剛好,自己以前在做相關工作的時候做過相關的東西,所以就總結一下,給大家共享出來,由於本人水平有限,bug是在所難免,還望有更高的高人批評指正。廢話不多說,直接進入主題。

要想在地圖上定位並將定位結果實時顯示出來,啓示邏輯上很簡單:首先,接收並解析GPS或者網絡的位置信息,一般來說,接受的位置信息是WGS84的經緯度的,但是我們的地圖的投影一般都不會是WGS84的,所以,位置信息接收來了得做一次座標轉換,目前,座標轉換的方式有七參數或者四參數的方式,但是這兩種參數轉換的算法和方式都不怎麼方便,還好,Arcgis支持從WGS84到地圖投影的轉換,轉換完成之後再在地圖上將該點展示出來並間隔刷新即可實現。下面我來說說我的實現方式:

1、定義一個GraphicsLayer並將之添加到map

GraphicsLayer gLayerGps;
……
gLayerGps = new GraphicsLayer();
mapview.addLayer(gLayerGps);

2、定義一個PictureMarkerSymbol用來設置位置顯示的樣式

PictureMarkerSymbol locationSymbol;
locationSymbol =  new PictureMarkerSymbol(this.getResources().getDrawable(
				R.drawable.location));

3、定義LocationManager

LocationManager locMag;
//要定位在地圖中的位置,需要知道當前位置,而當前位置有Location對象決定,
//但是,Location對象又需要LocationManager對象來創建。
//創建LocationManager的唯一方法
locMag = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
//獲得Provider列表
final List<String> providers=locMag.getProviders(true);

4、循環Provider,根據Provider獲取位置信息

		//循環Provider,根據Provider獲取位置信息
	  	for(String provider:providers)
	  	{
		    loc = locMag.getLastKnownLocation(provider);
		  	
		    LocationListener locationListener = new LocationListener(){
		    	/**
		    	 * 位置改變時調用
		    	 */
		    	public void onLocationChanged(Location location) {
					lblPosition.setText("Lat:"+String.format("%.4f", location.getLatitude()) + ",Lon:" + String.format("%.4f", location.getLongitude()));
					//刷新圖層
					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)
			{
				double latitude = loc.getLatitude();
				double longitude = loc.getLongitude();
				lblPosition.setText("Lat:"+String.format("%.4f", latitude) + ",Lon:" + String.format("%.4f", longitude));
				//開始畫圖
				markLocation(loc);
			}
		}

5、當位置不爲空時,就在地圖上畫點

	/**
	 * 在地圖上顯示當前位置
	 * @param location
	 */
	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),mapview.getSpatialReference());
	
		//圖層的創建
		Graphic graphicPoint = new Graphic(mapPoint,locationSymbol);
		gLayerPos.addGraphic(graphicPoint);
		/*劃線
		if (startPoint == null) {
			poly=new Polyline();
			startPoint = mapPoint;
			poly.startPath((float) startPoint.getX(),
					(float) startPoint.getY());
			Graphic graphicLine = new Graphic(startPoint,new SimpleLineSymbol(Color.RED,2));
			gLayerGps.addGraphic(graphicLine);
		}*/
		poly.lineTo((float) mapPoint.getX(), (float) mapPoint.getY());
		gLayerGps.addGraphic(new Graphic(poly,new SimpleLineSymbol(Color.BLACK,2)));
	}

6、將gLayerPos顯示

gLayerPos.setVisible(false);

這樣,GPS的點就能在地圖上顯示了,並且能夠顯示所走的路徑……

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