arcigs for android 提供LocationDisplayManager類專門用於獲取GPS信息,它能夠在地圖上展現當前位置變化狀況。
public class MainActivity extends Activity { MapView mMapView; LocationDisplayManager lDisplayManager = null; GraphicsLayer gpsGraphicsLayer; Polyline mPolyline; int pointCount = 0; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // after the content of this activity is set // the map can be accessed from the layout mMapView = (MapView) findViewById(R.id.map); ArcGISTiledMapServiceLayer tile = new ArcGISTiledMapServiceLayer("http://map.geoq.cn/ArcGIS/rest/services/ChinaOnlineCommunity_Mobile/MapServer"); mMapView.addLayer(tile); gpsGraphicsLayer = new GraphicsLayer(); mMapView.addLayer(gpsGraphicsLayer); mPolyline = new Polyline(); mMapView.setOnStatusChangedListener(new OnStatusChangedListener() { @Override public void onStatusChanged(Object source, STATUS status) { if (source == mMapView && status == STATUS.INITIALIZED) { lDisplayManager = mMapView.getLocationDisplayManager();//獲取LocationDisplayManager lDisplayManager.setAutoPanMode(LocationDisplayManager.AutoPanMode.OFF); lDisplayManager.setShowLocation(false);//不顯示當前位置,座標系不一致座標偏移嚴重 lDisplayManager.setShowPings(false); lDisplayManager.setAccuracyCircleOn(false); lDisplayManager.setAllowNetworkLocation(true); lDisplayManager.setLocationListener(new LocationListener() { @Override public void onLocationChanged(Location loc) { //火星座標轉換 double[] gcj = CoordinateConvert.wgs2GCJ(loc.getLatitude(), loc.getLongitude()); Point wgspoint = new Point(gcj[1], gcj[0]); Point p = (Point) GeometryEngine.project(wgspoint, SpatialReference.create(SpatialReference.WKID_WGS84), mMapView.getSpatialReference()); SimpleMarkerSymbol ptSym = new SimpleMarkerSymbol(Color.BLUE, 15, SimpleMarkerSymbol.STYLE.CIRCLE); Graphic graphic = new Graphic(p, ptSym, null); if (pointCount == 0) { mPolyline.startPath(p.getX(), p.getY()); mMapView.zoomTo(p, 17); } else { mPolyline.lineTo(p.getX(), p.getY());//點畫線 mMapView.centerAt(p, true); } gpsGraphicsLayer.removeAll(); SimpleLineSymbol lineSym = new SimpleLineSymbol(Color.RED, 10); Graphic g = new Graphic(mPolyline, lineSym); gpsGraphicsLayer.addGraphic(g); pointCount++; gpsGraphicsLayer.addGraphic(graphic); } @Override public void onProviderDisabled(String arg0) { } @Override public void onProviderEnabled(String arg0) { } @Override public void onStatusChanged(String arg0, int arg1, Bundle arg2) { } }); // Actionlistener lDisplayManager.start(); } } }); } }