翻開之前作的東西,看了看,不少從邏輯上比較亂,對之作了修改,完成後實現的效果爲:java
MapActivity源代碼以下:android
package com.lzugis.map; import java.io.File; import java.util.Iterator; import android.app.Activity; import android.content.Context; import android.content.Intent; import android.location.GpsSatellite; import android.location.GpsStatus; import android.location.Location; import android.location.LocationListener; import android.location.LocationManager; import android.location.LocationProvider; import android.os.Bundle; import android.os.Environment; import android.provider.Settings; import android.util.Log; import android.view.Gravity; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.Toast; import com.esri.android.map.GraphicsLayer; import com.esri.android.map.MapView; import com.esri.android.map.ags.ArcGISLocalTiledLayer; import com.esri.android.runtime.ArcGISRuntime; 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; import com.lzugis.tool.ZoomCtrl; public class MapActivity extends Activity { private static File dataFile; private static String dirName; private static String filename; private LocationListener locationListener = new LocationListener(){ /** * 位置信息變化時觸發 */ public void onLocationChanged(Location location) { markLocation(location); } /** * 狀態改變時調用 */ public void onStatusChanged(String provider, int status, Bundle extras) { switch (status) { //GPS狀態爲可見時 case LocationProvider.AVAILABLE: showToast("當前GPS狀態爲可見狀態"); Log.i("TAG", "當前GPS狀態爲可見狀態"); break; //GPS狀態爲服務區外時 case LocationProvider.OUT_OF_SERVICE: showToast("當前GPS狀態爲服務區外狀態"); Log.i("TAG", "當前GPS狀態爲服務區外狀態"); break; //GPS狀態爲暫停服務時 case LocationProvider.TEMPORARILY_UNAVAILABLE: showToast("當前GPS狀態爲暫停服務狀態"); Log.i("TAG", "當前GPS狀態爲暫停服務狀態"); break; } } /** * GPS開啓時觸發 */ public void onProviderEnabled(String provider) { showToast("GPS打開"); Location location=locMag.getLastKnownLocation(provider); markLocation(location); } /** * GPS禁用時觸發 */ public void onProviderDisabled(String provider) { showToast("GPS已關閉"); } }; MapView mapview; ArcGISLocalTiledLayer local; ZoomCtrl zoomCtrl; GraphicsLayer gLayerGps; Button btnPosition; Toast toast; LocationManager locMag; Location loc ; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main_map); //去除水印 ArcGISRuntime.setClientId("1eFHW78avlnRUPHm"); //要定位在地圖中的位置,須要知道當前位置,而當前位置有Location對象決定, //可是,Location對象又須要LocationManager對象來建立。 //建立LocationManager的惟一方法 locMag = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE); mapview = (MapView)findViewById(R.id.map); dataFile = Environment.getExternalStorageDirectory(); dirName = this.getResources().getString(R.string.offline_dir); filename = this.getResources().getString(R.string.local_tpk); String basemap = "file://"+dataFile + File.separator +dirName + File.separator + filename; local = new ArcGISLocalTiledLayer(basemap); mapview.addLayer(local); //放大與縮小 zoomCtrl = (ZoomCtrl) findViewById(R.id.ZoomControl); zoomCtrl.setMapView(mapview); gLayerGps = new GraphicsLayer(); mapview.addLayer(gLayerGps); btnPosition=(Button)findViewById(R.id.btnPosition); btnPosition.setOnClickListener(new OnClickListener(){ public void onClick(View v) { //判斷GPS是否正常啓動 if(!locMag.isProviderEnabled(LocationManager.GPS_PROVIDER)){ showToast("請開啓GPS導航..."); //返回開啓GPS導航設置界面 Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS); startActivityForResult(intent,0); return; } Location location= locMag.getLastKnownLocation(LocationManager.GPS_PROVIDER); markLocation(location); locMag.addGpsStatusListener(listener); locMag.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 0, locationListener); } }); } //狀態監聽 GpsStatus.Listener listener = new GpsStatus.Listener() { public void onGpsStatusChanged(int event) { switch (event) { //第一次定位 case GpsStatus.GPS_EVENT_FIRST_FIX: Log.i("TAG", "第一次定位"); break; //衛星狀態改變 case GpsStatus.GPS_EVENT_SATELLITE_STATUS: Log.i("TAG", "衛星狀態改變"); //獲取當前狀態 GpsStatus gpsStatus=locMag.getGpsStatus(null); //獲取衛星顆數的默認最大值 int maxSatellites = gpsStatus.getMaxSatellites(); //建立一個迭代器保存全部衛星 Iterator<GpsSatellite> iters = gpsStatus.getSatellites().iterator(); int count = 0; while (iters.hasNext() && count <= maxSatellites) { GpsSatellite s = iters.next(); count++; } System.out.println("搜索到:"+count+"顆衛星"); break; //定位啓動 case GpsStatus.GPS_EVENT_STARTED: Log.i("TAG", "定位啓動"); break; //定位結束 case GpsStatus.GPS_EVENT_STOPPED: Log.i("TAG", "定位結束"); break; } }; }; private void markLocation(Location location) { if(location!=null){ Log.i("TAG", "時間:"+location.getTime()); Log.i("TAG", "經度:"+location.getLongitude()); Log.i("TAG", "緯度:"+location.getLatitude()); Log.i("TAG", "海拔:"+location.getAltitude()); double locx = location.getLongitude(); double locy = location.getLatitude(); ShowPointOnMap(locx,locy); } } public void ShowPointOnMap(double lon,double lat){ //清空定位圖層 gLayerGps.removeAll(); //接收到的GPS的信號X(lat),Y(lon) double locx = lon; double locy = lat; Point wgspoint = new Point(locx, locy); Point mapPoint = (Point) GeometryEngine.project(wgspoint,SpatialReference.create(4326),mapview.getSpatialReference()); //圖層的建立 // Graphic graphic = new Graphic(mapPoint,new SimpleMarkerSymbol(Color.RED,18,STYLE.CIRCLE)); PictureMarkerSymbol pms = new PictureMarkerSymbol(this.getResources().getDrawable( R.drawable.location)); Graphic graphic = new Graphic(mapPoint,pms); gLayerGps.addGraphic(graphic); } private void showToast(String msg) { if(toast == null) { toast = Toast.makeText(this, msg, Toast.LENGTH_SHORT); } else { toast.setText(msg); toast.setDuration(Toast.LENGTH_SHORT); } toast.setGravity(Gravity.BOTTOM, 0, 0); toast.show(); } @Override protected void onDestroy() { super.onDestroy(); } @Override protected void onPause() { super.onPause(); mapview.pause(); } @Override protected void onResume() { super.onResume(); mapview.unpause(); } }