Arcgis For Android之GPS定位实现
翻开曾经做的东西,看了看,非常多从逻辑上比較乱,对之做了改动,完毕后实现的效果为:
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();
} @Override
protected void onDestroy() {
super.onDestroy();
}
@Override
protected void onPause() {
super.onPause();
mapview.pause();
}
@Override
protected void onResume() {
super.onResume();
mapview.unpause();
}
}
Arcgis For Android之GPS定位实现的更多相关文章
- [置顶]
xamarin android使用gps定位获取经纬度
看了文章你会得出以下几个结论 1.android定位主要有四种方式GPS,Network(wifi定位.基站定位),AGPS定位 2.绝大部分android国产手机使用network进行定位是没有作用 ...
- Android中GPS定位的简单应用
在Android中通过GPS获得当前位置,首先要获得一个LocationManager实例,通过该实例的getLastKnownLocation()方法获得第一个的位置,该方法的说明如下: void ...
- android 获取GPS定位
AndroidManifest.xml <?xml version="1.0" encoding="utf-8"?> <manifest xm ...
- 【Android】GPS定位基本原理浅析
位置服务已经成为越来越热的一门技术,也将成为以后所有移动设备(智能手机.掌上电脑等)的标配.而定位导航技术中,目前精度最高.应用最广泛的,自然非GPS莫属了.网络上介绍GPS原理的专业资料很多,而本文 ...
- Android开发——GPS定位
1.LocationManager LocationManager系统服务是位置服务的核心组件,它提供了一系列方法来处理与位置相关的问题. 与LocationManager相关的两个知识点: 1.1 ...
- Arcgis API for Android之GPS定位
欢迎大家增加Arcgis API for Android的QQ交流群:337469080 先说说写这篇文章的原因吧,在群内讨论的过程中,有人提到了定位的问题,刚好,自己曾经在做相关工作的时候做过相关的 ...
- Android之GPS定位详解
一.LocationManager LocationMangager,位置管理器.要想操作定位相关设备,必须先定义个LocationManager.我们可以通过如下代码创建LocationManger ...
- ArcGIS Runtime SDK for Android 定位权限(GPS定位\网络定位)
ACCESS_COARSE_LOCATION和ACCESS_FINE_LOCATION: android.permission.ACCESS_COARSE_LOCATION:是基站定位,即基于无线网络 ...
- 外业数据采集平台(GPS+Android Studio+Arcgis for android 100.2.1)
外业数据采集平台 1. 综述 在室外,通过平板或者手机接收GPS坐标,实时绘制点.线.面数据,以便为后续进行海域监测.土地确权.地图绘图提供有效数据和依据. 2. 技术路线 Android studi ...
随机推荐
- JavaFX它ListView使用
ListView它是通过同一控制非.在JavaFX在.ListView此外,它拥有非常丰富的功能.下列.让我们来看看如何使用ListView. ListView位于javafx.scene.contr ...
- 得到JAVA项目根文件夹
获得的相对路径 说明:相对路径(这并不说明什么时候相对谁)可以通过以下来获得(无论是一般java项目或web工程) String path = System.getProperty("use ...
- 无法使用Django新建项目:'django-admin.py'不是内部或外部命令
问题: watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvbXlhdGxhbnRpcw==/font/5a6L5L2T/fontsize/400/fill/I0 ...
- java 添加一个线程、创建响应的用户界面 。 演示示例代码
javajava 添加一个线程.创建响应的用户界面 . 演示示例代码 来自thinking in java 4 21章 部分的代码 夹21.2.11 thinking in java 4免费下载: ...
- .NET应用架构设计—再次了解分层架构(现代企业应用分层架构核心设计元素)
阅读文件夹: 1.背景介绍 2.简要回想下传统三层架构 3.企业级应用分层架构(现代分层架构的基本演变过程) 3.1.服务层中应用契约式设计来解决动态条件不匹配错误(通过契约式设计模式来将问题在线下暴 ...
- SQL Server审计功能入门:SQL Server审核 (SQL Server Audit)
原文:SQL Server审计功能入门:SQL Server审核 (SQL Server Audit) 介绍 Audit是SQL Server 2008之后才有的功能,它能告诉你"谁什么时候 ...
- cuda vector addition
http://webgpu.hwu.crhc.illinois.edu/ // MP 1 #include <wb.h> __global__ void vecAdd(float * in ...
- word插入图片显示不完整的解决的方法
有时在编写word文档,插入图片时,会出现图不完整的情况. 解决方法是:选中图片,段落格式设置为单位行距(不是22磅),图片格式为嵌入式.问题解决.
- 十天学Linux内核之第十天---总结篇(kconfig和Makefile & 讲不出再见)
原文:十天学Linux内核之第十天---总结篇(kconfig和Makefile & 讲不出再见) 非常开心能够和大家一起分享这些,让我受益匪浅,感激之情也溢于言表,,code monkey的 ...
- thinkphp学习笔记4—眼花缭乱的配置
原文:thinkphp学习笔记4-眼花缭乱的配置 1.配置类别 ThinkPHP提供了灵活的全局配置功能,ThinkPHP会依次加载管理配置>项目配置>调试配置>分组配置>扩展 ...