Arcgis API for Android之GPS定位

欢迎大家增加Arcgis API for Android的QQ交流群:337469080

先说说写这篇文章的原因吧,在群内讨论的过程中,有人提到了定位的问题,刚好,自己曾经在做相关工作的时候做过相关的东西,所以就总结一下,给大家共享出来,因为本人水平有限,bug是在所难免,还望有更高的高人批评指正。废话不多说,直接进入主题。

要想在地图上定位并将定位结果实时显示出来,启发逻辑上非常easy:首先,接收并解析GPS或者网络的位置信息,一般来说,接受的位置信息是WGS84的经纬度的,可是我们的地图的投影一般都不会是WGS84的,所以,位置信息接收来了得做一次坐标转换,眼下,坐标转换的方式有七參数或者四參数的方式,可是这两种參数转换的算法和方式都不怎么方便,还好,Arcgis支持从WGS84到地图投影的转换,转换完毕之后再在地图上将该点展示出来并间隔刷新就可以实现。以下我来说说我的实现方式:

1、定义一个GraphicsLayer并将之加入到map

GraphicsLayer gLayerGps;
……
gLayerGps = new GraphicsLayer();
mapview.addLayer(gLayerGps);

2、定义一个PictureMarkerSymbol用来设置位置显示的样式

PictureMarkerSymbol locationSymbol;
locationSymbol =  new PictureMarkerSymbol(this.getResources().getDrawable(
				R.drawable.location));

3、定义LocationManager

LocationManager locMag;
//要定位在地图中的位置,须要知道当前位置,而当前位置有Location对象决定,
//可是,Location对象又须要LocationManager对象来创建。
//创建LocationManager的唯一方法
locMag = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
//获得Provider列表
final List<String> providers=locMag.getProviders(true);

4、循环Provider,依据Provider获取位置信息

		//循环Provider,依据Provider获取位置信息
	  	for(String provider:providers)
	  	{
		    loc = locMag.getLastKnownLocation(provider);

		    LocationListener locationListener = new LocationListener(){
		    	/**
		    	 * 位置改变时调用
		    	 */
		    	public void onLocationChanged(Location location) {
					lblPosition.setText("Lat:"+String.format("%.4f", location.getLatitude()) + ",Lon:" + String.format("%.4f", location.getLongitude()));
					//刷新图层
					markLocation(location);
				}
		    	//Provider失效时调用
				public void onProviderDisabled(String arg0)
				{
				}
				//Provider生效时调用
				public void onProviderEnabled(String arg0)
				{
				}
				//状态改变时调用
				public void onStatusChanged(String arg0, int arg1, Bundle arg2)
				{
				}
			};
			locMag.requestLocationUpdates(provider, 100, 0, locationListener);
			if(loc!=null)
			{
				double latitude = loc.getLatitude();
				double longitude = loc.getLongitude();
				lblPosition.setText("Lat:"+String.format("%.4f", latitude) + ",Lon:" + String.format("%.4f", longitude));
				//開始绘图
				markLocation(loc);
			}
		}

5、当位置不为空时,就在地图上画点

	/**
	 * 在地图上显示当前位置
	 * @param location
	 */
	private void markLocation(Location location)
	{
		gLayerPos.removeAll();
		double locx = location.getLongitude();
		double locy = location.getLatitude();
		wgspoint = new Point(locx, locy);
		mapPoint = (Point) GeometryEngine.project(wgspoint,SpatialReference.create(4326),mapview.getSpatialReference());

		//图层的创建
		Graphic graphicPoint = new Graphic(mapPoint,locationSymbol);
		gLayerPos.addGraphic(graphicPoint);
		/*划线
		if (startPoint == null) {
			poly=new Polyline();
			startPoint = mapPoint;
			poly.startPath((float) startPoint.getX(),
					(float) startPoint.getY());
			Graphic graphicLine = new Graphic(startPoint,new SimpleLineSymbol(Color.RED,2));
			gLayerGps.addGraphic(graphicLine);
		}*/
		poly.lineTo((float) mapPoint.getX(), (float) mapPoint.getY());
		gLayerGps.addGraphic(new Graphic(poly,new SimpleLineSymbol(Color.BLACK,2)));
	}

6、将gLayerPos显示


gLayerPos.setVisible(false);

这样,GPS的点就能在地图上显示了,而且可以显示所走的路径……

Arcgis API for Android之GPS定位,布布扣,bubuko.com

时间: 2024-08-05 14:59:55

Arcgis API for Android之GPS定位的相关文章

android 获取GPS定位

AndroidManifest.xml <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.yanlei.yl5" > <uses-permission android:name="a

新建arcgis api for android 项目失败

新建ArcGIS Project for Android项目失败解决方案 - skybot的专栏 - 博客频道 - CSDN.NET http://blog.csdn.net/daonidedie/article/details/8556184     问题描述: The selected wizard could not be started. Plug-in "com.esri.arcgis.android.core" was unable to instantiate class

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.Gps

Android之GPS定位详解

一.LocationManagerLocationMangager,位置管理器.要想操作定位相关设备,必须先定义个LocationManager.我们可以通过如下代码创建LocationManger对象.LocationManger locationManager=(LocationManager)this.getSystemService(Context.LOCATION_SERVICE); 二.LocationListenerLocationListener,位置监听,监听位置变化,监听设备

ArcGIS API For Android Errors汇总

API客户端异常错误的发生通常是由于不正确的方法参数,对象状态依赖,或网络条件. //*******推荐使用方法,按下Ctrl+F搜索错误代码,再查看与代码对应的解释.********// ArcGIS弹出错误 11000 Invalid attributes or geometry on the associated GeoElement. 11001 Null value is not allowed. 11002 Value is out of range. 11003 Value exc

Android之百度定位API使用

API版本: 百度定位API:V5.0(http://developer.baidu.com/map/wiki/index.php?title=android-locsdk/guide/v5-0) 百度Geocoding API:V2.0(http://developer.baidu.com/map/index.php?title=webapi/guide/webservice-geocoding#7..E9.80.86.E5.9C.B0.E7.90.86.E7.BC.96.E7.A0.81.E

Android GPS定位,取得城市名称

首先说明,定位城市是基于地图服务API的. GPS定位城市需要做两个步骤: 1.取得用户当前位置的经度,纬度. 2.根据经纬度转换成城市名称. 经纬度转换成城市名称,只能使用地图服务了.自己做不来. 地图服务API有两个,一个是百度地图,一个是谷歌地图.百度地图API调用需要注册百度帐号,并申请APP_KEY,谷歌地图API直接调用即可. 百度地图API调用地址:http://api.map.baidu.com/geocoder?output=json&location=纬度,经度&key

Android GPS定位

GPS定位貌似在室内用不了,今天自己弄了一个GPS定位小Demo,包括用户所在的经度.纬度.高度.方向.移动速度.精确度等信息.Android为GPS功能支持专门提供了一个LocationManager类,程序并不能直接创建LocationManager实例,而是通过Context的getSystemService()方法来获取. 例如: //创建LocationManager对象 LocationManager lm = (LocationManager)getSystemService(Co

Android GPS定位实现,教你如何一分钟实现GPS定位

转载请注明出处:http://blog.csdn.net/smartbetter/article/details/50425041 今天给大家带来的是一篇关于GPS定位的文章,我们都知道,Android定位大致分为wifi定位,基站定位和GPS定位,今天我就带大家来看看GPS定位是什么玩意,通过本篇的学习,相信大家能很快上手GPS定位操作了.GPS定位是通过和GPS定位卫星通讯来进行定位的,可以使用最少数量的卫星实现全球定位,3颗,通过光波进行通讯,不需要联网,但是连接时间比较长,大致1分钟左右