ArcgisforAndrod API地图显示与GPS定位

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

ArcgisforAndrod API地图显示与GPS定位_第1张图片

 

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

在做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吧,建成之后文件组织形式如下:

ArcgisforAndrod API地图显示与GPS定位_第2张图片

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

1、src

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

2、libs

这个是开发相关的类库

3、res

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

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

view source print ?
01. <!--?xml version="1.0" encoding="utf-8"?-->
02. <relativelayoutandroid:layout_height="fill_parent"android:layout_width="fill_parent"xmlns:android="http://schemas.android.com/apk/res/android">
03.      
04.     <!-- MapView layout and initial extent -->
05.     <com.esri.android.map.mapviewandroid:id="@+id/map"android:layout_height="fill_parent"android:layout_width="fill_parent">
06.     </com.esri.android.map.mapview>
07.      
08.     <com.esri.arcgis.android.samples.helloworld.zoomcontrolviewandroid:id="@+id/ZoomControlView"android:layout_alignparentbottom="true"android:layout_alignparentright="true"android:layout_height="wrap_content"android:layout_marginbottom="20.0dip"android:layout_marginright="5.0dip"android:layout_width="wrap_content">
09.  
10.     <zoomcontrolsandroid:id="@+id/zoomCtrl"android:layout_alignparentright="true"android:layout_alignparenttop="true"android:layout_height="wrap_content"android:layout_marginright="5.0dip"android:layout_margintop="20.0dip"android:layout_width="wrap_content">
11.  
12.     <android.widget.searchviewandroid:id="@+id/searchView"android:layout_alignparentleft="true"android:layout_alignparenttop="true"android:layout_height="wrap_content"android:layout_marginleft="5.0dip"android:layout_margintop="20.0dip"android:layout_width="wrap_content"><buttonandroid:id="@+id/btnGps"android:layout_alignparentbottom="true"android:layout_alignparentleft="true"android:layout_height="wrap_content"android:layout_marginbottom="20.0dip"android:layout_marginleft="5.0dip"android:layout_width="wrap_content"android:text="GPS"></button></android.widget.searchview></zoomcontrols></com.esri.arcgis.android.samples.helloworld.zoomcontrolview></relativelayout>



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

 

view source print ?
001. /* Copyright 2012 ESRI
002.  *
003.  * All rights reserved under the copyright laws of the United States
004.  * and applicable international laws, treaties, and conventions.
005.  *
006.  * You may freely redistribute and use this sample code, with or
007.  * without modification, provided you include the original copyright
008.  * notice and use restrictions.
009.  *
010.  * See the ?Sample code usage restrictions? document for further information.
011.  *
012.  */
013.  
014. packagecom.esri.arcgis.android.samples.helloworld;
015.  
016. importjava.util.List;
017.  
018. importandroid.app.Activity;
019. importandroid.content.Context;
020. importandroid.location.Location;
021. importandroid.location.LocationListener;
022. importandroid.location.LocationManager;
023. importandroid.os.Bundle;
024. importandroid.util.Log;
025. importandroid.view.View;
026. importandroid.view.View.OnClickListener;
027. importandroid.widget.Button;
028. importandroid.widget.ZoomControls;
029.  
030. importcom.esri.android.map.GraphicsLayer;
031. importcom.esri.android.map.MapView;
032. importcom.esri.android.map.ags.ArcGISTiledMapServiceLayer;
033. importcom.esri.core.geometry.GeometryEngine;
034. importcom.esri.core.geometry.Point;
035. importcom.esri.core.geometry.SpatialReference;
036. importcom.esri.core.map.Graphic;
037. importcom.esri.core.symbol.PictureMarkerSymbol;
038.  
039. publicclass HelloWorld extendsActivity {
040.     Button zoomin;
041.     Button zoomout;
042.     Button btnGps;
043.     ZoomControls zoomctrl;
044.     LocationManager locMag;
045.     Location loc ;
046.      
047.     MapView map =null;
048.     ArcGISTiledMapServiceLayer tileLayer;
049.     GraphicsLayer gLayerPos;
050.     Point point;
051.     Point wgspoint;
052.     Point mapPoint;
053.     PictureMarkerSymbol locationSymbol;
054.      
055.     ZoomControlView mZoomControlView;
056.      
057.     /** Called when the activity is first created. */
058.     @Override
059.     publicvoid onCreate(Bundle savedInstanceState) {
060.         super.onCreate(savedInstanceState);
061.         setContentView(R.layout.main);
062.          
063.         map = (MapView)findViewById(R.id.map);
064.          
065.         tileLayer =new ArcGISTiledMapServiceLayer(
066.                 "http://cache1.arcgisonline.cn/ArcGIS/rest/services/ChinaOnlineCommunity/MapServer");
067.          
068.         map.addLayer(tileLayer);
069.          
070.         //设置地图中心点
071.         point = (Point) GeometryEngine.project(newPoint(40.805,111.661),SpatialReference.create(4326),map.getSpatialReference());
072.         map.centerAt(point,true);
073.         /*
074.         zoomin=(Button)findViewById(R.id.zoomin);
075.         zoomout=(Button)findViewById(R.id.zoomout);
076.         zoomin.setOnClickListener(new OnClickListener(){
077.             @Override
078.             public void onClick(View arg0) {
079.                 // TODO Auto-generated method stub
080.                 map.zoomin();
081.             }          
082.         });
083.         zoomout.setOnClickListener(new OnClickListener(){
084.             @Override
085.             public void onClick(View arg0) {
086.                 // TODO Auto-generated method stub
087.                 map.zoomout();
088.             }          
089.         });*/
090.         //放大与缩小——自定义
091.         mZoomControlView = (ZoomControlView) findViewById(R.id.ZoomControlView);
092.         mZoomControlView.setMapView(map);
093.          
094.         //放大与缩小ZoomControls
095.         zoomctrl=(ZoomControls)findViewById(R.id.zoomCtrl);
096.         zoomctrl.setOnZoomInClickListener(newOnClickListener(){
097.             @Override
098.             publicvoid onClick(View v) {
099.                 // TODO Auto-generated method stub
100.                 map.zoomin();
101.             }          
102.         });
103.         zoomctrl.setOnZoomOutClickListener(newOnClickListener(){
104.             @Override
105.             publicvoid 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.         finalList<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.                 publicvoid onLocationChanged(Location location) {
133.                     //刷新图层
134.                     markLocation(location);
135.                 }
136.                 //Provider失效时调用
137.                 publicvoid onProviderDisabled(String arg0)
138.                 {
139.                 }
140.                 //Provider生效时调用
141.                 publicvoid onProviderEnabled(String arg0)
142.                 {
143.                 }
144.                 //状态改变时调用
145.                 publicvoid 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.     privatevoid markLocation(Location location)
159.     {      
160.         gLayerPos.removeAll();
161.         doublelocx = location.getLongitude();
162.         doublelocy = 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.     protectedvoid onPause() {
174.         super.onPause();
175.         map.pause();
176.     }
177.  
178.     @Override
179.     protectedvoid onResume() {
180.         super.onResume();
181.         map.unpause();
182.     }
183. }</string>

 

 

你可能感兴趣的:(ArcgisforAndrod API地图显示与GPS定位)