翻开以前做的东西,看了看,很多从逻辑上比较乱,对之做了修改,完成后实现的效果为:


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();}}


更多相关文章

  1. Android(安卓)判断网络是否可用、网络类型WIFI/2G/3G/4G及获取IP
  2. Android(安卓)Permission 中英对照
  3. 第十一篇 ANDROID 系统网络连接和管理机制
  4. Android全屏(包含3种隐藏顶部状态栏及标题栏和一种隐藏Android(安
  5. 【Android】关于TextView
  6. cocos2d-x 3.X 在android 中添加多盟插屏广告
  7. Android(安卓)App 隐藏显示标题栏、状态栏、导航栏
  8. android listview、GridView中item点击后改变其他item中的状态 s
  9. Android(安卓)获取IMEI号码

随机推荐

  1. javascript常量,变量,数据类型实例
  2. 模板字面量标签函数和解构赋值与对象字面
  3. 京东商城flex结构实例演示
  4. Promise的用法-初
  5. 常用标签,属性,表格
  6. 安装编辑器与常用插件
  7. html预习,请老师审批,
  8. 实例演示flex容器与项目中常用的属性
  9. 三行三列的定位布局中演示QQ客服的固定定
  10. 对于模板字面量与标签函数、解构赋值于对