本篇呢,是用“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文件的内容:
01.
<!--?xml version="1.0" encoding="utf-8"?-->
02.
<
relativelayout
android: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.mapview
android: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.zoomcontrolview
android: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.
<
zoomcontrols
android: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.searchview
android: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"
><
button
android: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的内容:
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.
package
com.esri.arcgis.android.samples.helloworld;
015.
016.
import
java.util.List;
017.
018.
import
android.app.Activity;
019.
import
android.content.Context;
020.
import
android.location.Location;
021.
import
android.location.LocationListener;
022.
import
android.location.LocationManager;
023.
import
android.os.Bundle;
024.
import
android.util.Log;
025.
import
android.view.View;
026.
import
android.view.View.OnClickListener;
027.
import
android.widget.Button;
028.
import
android.widget.ZoomControls;
029.
030.
import
com.esri.android.map.GraphicsLayer;
031.
import
com.esri.android.map.MapView;
032.
import
com.esri.android.map.ags.ArcGISTiledMapServiceLayer;
033.
import
com.esri.core.geometry.GeometryEngine;
034.
import
com.esri.core.geometry.Point;
035.
import
com.esri.core.geometry.SpatialReference;
036.
import
com.esri.core.map.Graphic;
037.
import
com.esri.core.symbol.PictureMarkerSymbol;
038.
039.
public
class
HelloWorld
extends
Activity {
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.
public
void
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(
new
Point(
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(
new
OnClickListener(){
097.
@Override
098.
public
void
onClick(View v) {
099.
// 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.
}</string>