序:最近呢,工作鸭梨不是怎么大,对于自己爱折腾的想法又冒出了水面,开始自己的android开发的学习之旅。但是呢,本人是做GIS的,所以呢,就打算从这方面入手看看,是不是有什么比较好玩的玩意呢,这才导致了“Arcgis for Androd API开发系列教程”的成功问世……

本篇呢,是用“Arcgis for Androd API”实现基本的地图显示并在图上显示当前GPS所在位置。为了比较直观的让大家看看本人的成果呢,先给大家上账图吧:

看见了吧,人所在的位置呢就是本人所处的位置……知道要做什么了之后,下面给大家说一下具体的实现方法吧。

在做Arcgis for Android API开发之前,你得做一件大事,那就是搭建Android的开发环境,至于怎么搭建,我在此就不再说了,本来没打算说这玩意的。安卓开发环境搭建完成之后了,你需要需要安装 ArcGIS 发相关的库和 Eclipse插件了,这个的安装呢,你可以选择在线的安装方式,也可以选择离线的安装方式,在线的比较简单,Eclipse菜单/help/Install New Softwear...,在弹出的框框里面输入http://downloads.esri.com/software/arcgis/android即可,离线的更省事,不过你本机得有ArcGISAndroidSDK_v????.zip,没有的那别着急,你可以去网站上找,不想找的呢,我呢也给大家共享了,下载地址为:http://download.csdn.net/detail/gisshixisheng/6703689,大家按需下载,不受积分的。

靠,废话一大堆,终于到主题了!上面的工作完成之后呢,首先你得新建一个Arcgis for Android的工程,暂且就叫做MapGps吧,建成之后文件组织形式如下:

我想,做过安卓开发或者了解安卓开发的人呢对着玩意肯定不陌生吧,具体的我也不做解释,有疑问的我们可以私聊,最好是美女……不过呢,有些东西呢,还是交代一下吧:

1、src

这个东东我不怎么清楚,个人认为类似于web开发的后台

2、libs

这个是开发相关的类库

3、res

英语差不多的人应该明白,res是resources的简写,是“资源”的意思。其中,darwable命名的文件夹是一些图片文件,layout是一些布局文件,values是一些值文件,里面包括string,color等等……这个layout类似于web的前台吧……

首先,来看看main.xml文件的内容:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="fill_parent"
  4. android:layout_height="fill_parent" >
  5.  
  6. <!-- MapView layout and initial extent -->
  7. <com.esri.android.map.MapView
  8. android:id="@+id/map"
  9. android:layout_width="fill_parent"
  10. android:layout_height="fill_parent">
  11. </com.esri.android.map.MapView>
  12.  
  13. <com.esri.arcgis.android.samples.helloworld.ZoomControlView
  14. android:id="@+id/ZoomControlView"
  15. android:layout_width="wrap_content"
  16. android:layout_height="wrap_content"
  17. android:layout_alignParentBottom="true"
  18. android:layout_alignParentRight="true"
  19. android:layout_marginBottom="20.0dip"
  20. android:layout_marginRight="5.0dip"/>
  21.  
  22. <ZoomControls
  23. android:id="@+id/zoomCtrl"
  24. android:layout_width="wrap_content"
  25. android:layout_height="wrap_content"
  26. android:layout_alignParentTop="true"
  27. android:layout_alignParentRight="true"
  28. android:layout_marginTop="20.0dip"
  29. android:layout_marginRight="5.0dip" />
  30.  
  31. <android.widget.SearchView
  32. android:id="@+id/searchView"
  33. android:layout_width="wrap_content"
  34. android:layout_height="wrap_content"
  35. android:layout_alignParentTop="true"
  36. android:layout_alignParentLeft="true"
  37. android:layout_marginTop="20.0dip"
  38. android:layout_marginLeft="5.0dip" />
  39.  
  40. <Button
  41. android:id="@+id/btnGps"
  42. android:layout_width="wrap_content"
  43. android:layout_height="wrap_content"
  44. android:layout_alignParentBottom="true"
  45. android:layout_alignParentLeft="true"
  46. android:layout_marginBottom="20.0dip"
  47. android:layout_marginLeft="5.0dip"
  48. android:text="GPS" />
  49.  
  50. </RelativeLayout>

接着,看看HelloWorld.java的内容:

  1. /* Copyright 2012 ESRI
  2. *
  3. * All rights reserved under the copyright laws of the United States
  4. * and applicable international laws, treaties, and conventions.
  5. *
  6. * You may freely redistribute and use this sample code, with or
  7. * without modification, provided you include the original copyright
  8. * notice and use restrictions.
  9. *
  10. * See the �Sample code usage restrictions� document for further information.
  11. *
  12. */
  13.  
  14. package com.esri.arcgis.android.samples.helloworld;
  15.  
  16. import java.util.List;
  17.  
  18. import android.app.Activity;
  19. import android.content.Context;
  20. import android.location.Location;
  21. import android.location.LocationListener;
  22. import android.location.LocationManager;
  23. import android.os.Bundle;
  24. import android.util.Log;
  25. import android.view.View;
  26. import android.view.View.OnClickListener;
  27. import android.widget.Button;
  28. import android.widget.ZoomControls;
  29.  
  30. import com.esri.android.map.GraphicsLayer;
  31. import com.esri.android.map.MapView;
  32. import com.esri.android.map.ags.ArcGISTiledMapServiceLayer;
  33. import com.esri.core.geometry.GeometryEngine;
  34. import com.esri.core.geometry.Point;
  35. import com.esri.core.geometry.SpatialReference;
  36. import com.esri.core.map.Graphic;
  37. import com.esri.core.symbol.PictureMarkerSymbol;
  38.  
  39. public class HelloWorld extends Activity {
  40. Button zoomin;
  41. Button zoomout;
  42. Button btnGps;
  43. ZoomControls zoomctrl;
  44. LocationManager locMag;
  45. Location loc ;
  46.  
  47. MapView map = null;
  48. ArcGISTiledMapServiceLayer tileLayer;
  49. GraphicsLayer gLayerPos;
  50. Point point;
  51. Point wgspoint;
  52. Point mapPoint;
  53. PictureMarkerSymbol locationSymbol;
  54.  
  55. ZoomControlView mZoomControlView;
  56.  
  57. /** Called when the activity is first created. */
  58. @Override
  59. public void onCreate(Bundle savedInstanceState) {
  60. super.onCreate(savedInstanceState);
  61. setContentView(R.layout.main);
  62.  
  63. map = (MapView)findViewById(R.id.map);
  64.  
  65. tileLayer = new ArcGISTiledMapServiceLayer(
  66. "http://cache1.arcgisonline.cn/ArcGIS/rest/services/ChinaOnlineCommunity/MapServer");
  67.  
  68. map.addLayer(tileLayer);
  69.  
  70. //设置地图中心点
  71. point = (Point) GeometryEngine.project(new Point(40.805, 111.661),SpatialReference.create(4326),map.getSpatialReference());
  72. map.centerAt(point, true);
  73. /*
  74. zoomin=(Button)findViewById(R.id.zoomin);
  75. zoomout=(Button)findViewById(R.id.zoomout);
  76. zoomin.setOnClickListener(new OnClickListener(){
  77. @Override
  78. public void onClick(View arg0) {
  79. // TODO Auto-generated method stub
  80. map.zoomin();
  81. }
  82. });
  83. zoomout.setOnClickListener(new OnClickListener(){
  84. @Override
  85. public void onClick(View arg0) {
  86. // TODO Auto-generated method stub
  87. map.zoomout();
  88. }
  89. });*/
  90. //放大与缩小——自定义
  91. mZoomControlView = (ZoomControlView) findViewById(R.id.ZoomControlView);
  92. mZoomControlView.setMapView(map);
  93.  
  94. //放大与缩小ZoomControls
  95. zoomctrl=(ZoomControls)findViewById(R.id.zoomCtrl);
  96. zoomctrl.setOnZoomInClickListener(new OnClickListener(){
  97. @Override
  98. public void onClick(View v) {
  99. // TODO Auto-generated method stub
  100. map.zoomin();
  101. }
  102. });
  103. zoomctrl.setOnZoomOutClickListener(new OnClickListener(){
  104. @Override
  105. public void onClick(View v) {
  106. // TODO Auto-generated method stub
  107. map.zoomout();
  108. }
  109. });
  110.  
  111. gLayerPos = new GraphicsLayer();
  112. map.addLayer(gLayerPos);
  113.  
  114. locationSymbol = new PictureMarkerSymbol(this.getResources().getDrawable(
  115. R.drawable.location));
  116.  
  117. //要定位在地图中的位置,需要知道当前位置,而当前位置有Location对象决定,
  118. //但是,Location对象又需要LocationManager对象来创建。
  119. //创建LocationManager的唯一方法
  120. locMag = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
  121. //获得Provider列表
  122. final List<String> providers=locMag.getProviders(true);
  123. //循环Provider,根据Provider获取位置信息
  124. for(String provider:providers)
  125. {
  126. loc = locMag.getLastKnownLocation(provider);
  127.  
  128. LocationListener locationListener = new LocationListener(){
  129. /**
  130. * 位置改变时调用
  131. */
  132. public void onLocationChanged(Location location) {
  133. //刷新图层
  134. markLocation(location);
  135. }
  136. //Provider失效时调用
  137. public void onProviderDisabled(String arg0)
  138. {
  139. }
  140. //Provider生效时调用
  141. public void onProviderEnabled(String arg0)
  142. {
  143. }
  144. //状态改变时调用
  145. public void onStatusChanged(String arg0, int arg1, Bundle arg2)
  146. {
  147. }
  148. };
  149. locMag.requestLocationUpdates(provider, 100, 0, locationListener);
  150. if(loc!=null)
  151. {
  152. //开始画图
  153. markLocation(loc);
  154. }
  155. }
  156. }
  157.  
  158. private void markLocation(Location location)
  159. {
  160. gLayerPos.removeAll();
  161. double locx = location.getLongitude();
  162. double locy = location.getLatitude();
  163. wgspoint = new Point(locx, locy);
  164. mapPoint = (Point) GeometryEngine.project(wgspoint,SpatialReference.create(4326),map.getSpatialReference());
  165.  
  166. //图层的创建
  167. Graphic graphic = new Graphic(mapPoint,locationSymbol);
  168. gLayerPos.addGraphic(graphic);
  169. map.centerAt(mapPoint, true);
  170. }
  171.  
  172. @Override
  173. protected void onPause() {
  174. super.onPause();
  175. map.pause();
  176. }
  177.  
  178. @Override
  179. protected void onResume() {
  180. super.onResume();
  181. map.unpause();
  182. }
  183. }

附件:

点此下载源码    
点此现在类库包

Arcgis for Androd API开发系列教程(一)——地图显示与GPS定位的更多相关文章

  1. 高德地图 JavaScript API 开发系列教程(一)

    高德地图 API 提供包括 Web API 即 JavaScript API,Android API,定位API,IOS API,WP API,Win8 API等,本系列教程主要针对高德 JavaSc ...

  2. 高德地图 JavaScript API 开发系列教程(二)

    上节简单介绍高德地图JavaScript API及网页中基本地图展示.有了地图后我们可以用来做什么?有人说离我最近的超市在哪里,我怎么去?别急,这些功能我们后面都会慢慢实现.今天为大家详细讲解如何在地 ...

  3. C#微信公众号开发系列教程六(被动回复与上传下载多媒体文件)

    微信公众号开发系列教程一(调试环境部署) 微信公众号开发系列教程一(调试环境部署续:vs远程调试) C#微信公众号开发系列教程二(新手接入指南) C#微信公众号开发系列教程三(消息体签名及加解密) C ...

  4. 循序渐进学.Net Core Web Api开发系列【8】:访问数据库(基本功能)

    系列目录 循序渐进学.Net Core Web Api开发系列目录 本系列涉及到的源码下载地址:https://github.com/seabluescn/Blog_WebApi 一.概述 本篇讨论如 ...

  5. 使用react全家桶制作博客后台管理系统 网站PWA升级 移动端常见问题处理 循序渐进学.Net Core Web Api开发系列【4】:前端访问WebApi [Abp 源码分析]四、模块配置 [Abp 源码分析]三、依赖注入

    使用react全家桶制作博客后台管理系统   前面的话 笔者在做一个完整的博客上线项目,包括前台.后台.后端接口和服务器配置.本文将详细介绍使用react全家桶制作的博客后台管理系统 概述 该项目是基 ...

  6. Android 开发系列教程之(一)Android基础知识

    什么是Android Android一词最早是出现在法国作家维里耶德利尔·亚当1986年发表的<未来夏娃>这部科幻小说中,作者利尔·亚当将外表像人类的机器起名为Android,这就是And ...

  7. C#微信公众号开发系列教程三(消息体签名及加解密)

    http://www.cnblogs.com/zskbll/p/4139039.html C#微信公众号开发系列教程一(调试环境部署) C#微信公众号开发系列教程一(调试环境部署续:vs远程调试) C ...

  8. C#微信公众号开发系列教程二(新手接入指南)

    http://www.cnblogs.com/zskbll/p/4093954.html 此系列前面已经更新了两篇博文了,都是微信开发的前期准备工作,现在切入正题,本篇讲解新手接入的步骤与方法,大神可 ...

  9. 微信公众号开发系列教程一(调试环境部署续:vs远程调试)

    http://www.cnblogs.com/zskbll/p/4080328.html 目录 C#微信公众号开发系列教程一(调试环境部署) C#微信公众号开发系列教程一(调试环境部署续:vs远程调试 ...

随机推荐

  1. go——并发(二)

    通常程序会被编写为一个顺序执行并完成一个独立任务的代码. 如果没有特别的需求,最好总是这样写代码,因为这种类型的程序通常很容易写,也容易维护. 不过也有一些情况下,并行执行多个任务会有更大的好处. 一 ...

  2. LightOJ - 1236 (唯一分解定理)

    题意:求有多少对数对(i,j)满足lcm(i,j) = n,1<=i<=j, 1<=n<=1e14. 分析:根据整数的唯一分解定理,n可以分解为(p1^e1)*(p2^e2)* ...

  3. 增for语句内容

    #author:leon #"hello world!" for i in range(10): #循环十次,每一次循环赋一个0-9中的数字给i . print("--- ...

  4. spring boot集成redis缓存

    spring boot项目中使用redis作为缓存. 先创建spring boot的maven工程,在pom.xml中添加依赖 <dependency> <groupId>or ...

  5. AFNetworking 3.0 解决加密后请求参数是字符串问题

    把整个请求参数的json加密生成一个字符串传给服务器,错误提示:[NSJSONSerialization dataWithJSONObject:options:error:]: Invalid top ...

  6. Spring 之通过 XML 装配 bean

    1.关于 使用传统标签还是 c- p- 命名空间定义的标签, 我的观点是能用  c- p- 命名空间定义的标签 就不用 传统标签(这样会比较简洁... 2.强依赖使用构造器注入,可选性依赖使用属性注入 ...

  7. Nginx 静态缓存

    静态文件缓存 静态缓存在客户端下进行缓存,可以设置缓存文件类型与缓存时间,提升客户端访问站点速度. 主要对图片,css,js等元素更改机会比较少的情况下使用,特别是图片,占用带宽大,我们完全可以设置图 ...

  8. 高并发情况下分布式全局ID

    1.高并发情况下,生成分布式全局id策略2.利用全球唯一UUID生成订单号优缺点3.基于数据库自增或者序列生成订单号4.数据库集群如何考虑数据库自增唯一性5.基于Redis生成生成全局id策略6.Tw ...

  9. Autofac register and resolve

    Passing Parameters to Register When you register components you have the ability to provide a set of ...

  10. HTML常用标签——思维导图

    如图 思维导图图片链接 http://www.edrawsoft.cn/viewer/public/s/38d99149304484