Arcgis For Android之GPS定位實現

來源:互聯網
上載者:User

標籤:android   地圖   arcgis   

翻開以前做的東西,看了看,很多從邏輯上比較亂,對之做了修改,完成後實現的效果為:


MapActivity原始碼如下:

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();      } @Overrideprotected void onDestroy() {super.onDestroy();}@Overrideprotected void onPause() {super.onPause();mapview.pause();}@Overrideprotected void onResume() {super.onResume();mapview.unpause();}}


Arcgis For Android之GPS定位實現

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.