1. Android系统的手机一台,我用的是4.0
2. 确定你的手机能够正常定位,因为我自己的手机无法定位,用同事的就可以定位了。
开发环境搭建:
Eclipse + ADT + ANDROID-SDK 2.3.3 ( API 10 ) + ARCGIS-ANDROID SDK 10.1.1
本文将代码分为片段讲解,最后会附上本文代码。
- textView = new TextView(this);
- LinearLayout layout = new LinearLayout(this);
- layout.setOrientation(LinearLayout.VERTICAL);
-
- mMapView = new MapView(this);
- mMapView.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT));
- ArcGISTiledMapServiceLayer tileLayout = new ArcGISTiledMapServiceLayer(super.getString(R.string.map_url));
- mMapView.addLayer(tileLayout);
-
- gLayer = new GraphicsLayer();
- mMapView.addLayer(gLayer);
textView = new TextView(this);
LinearLayout layout = new LinearLayout(this);
layout.setOrientation(LinearLayout.VERTICAL);
mMapView = new MapView(this);
mMapView.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT));
ArcGISTiledMapServiceLayer tileLayout = new ArcGISTiledMapServiceLayer(super.getString(R.string.map_url));
mMapView.addLayer(tileLayout);
gLayer = new GraphicsLayer();
mMapView.addLayer(gLayer);
上面的代码往android手机上添加了两个控件,一个文本,一个地图。
地图的创建也是通过代码形式,如果你是利用xml配置的,需要改为你自己的地图实例。
要在地图上定位自己的位置,首先需要android手机帮你获取LocationManager
- locMag = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
//要定位在地图中的位置,需要知道当前位置,而当前位置有Location对象决定,
//但是,Location对象又需要LocationManager对象来创建。
locMag = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);//创建LocationManager的唯一方法
接着需要获取当前的Location
- String provider = LocationManager.NETWORK_PROVIDER;
- loc = locMag.getLastKnownLocation(provider);
- if(loc==null){
- provider = LocationManager.NETWORK_PROVIDER;
- loc = locMag.getLastKnownLocation(provider);
- }
String provider = LocationManager.NETWORK_PROVIDER;
loc = locMag.getLastKnownLocation(provider);
if(loc==null){
provider = LocationManager.NETWORK_PROVIDER;
loc = locMag.getLastKnownLocation(provider);
}
这里我使用了两个LocationManager.NETWORK_PROVIDER 是因为,我把第一个换成GPS_PROVIDER的时候,我下面的事件无法触发(因为我在房屋内开发)。
- LocationListener locationListener = new LocationListener(){
- @Override
- public void onLocationChanged(Location location) {
- textView.setText("位置发生变化,新位置: " + location.getLatitude() + " , " + location.getLongitude());
- System.out.println("位置发生变化,新位置: " + location.getLatitude() + " , " + location.getLongitude());
- markLocation(location);
- }
- @Override
- public void onProviderDisabled(String arg0) {
- }
- @Override
- public void onProviderEnabled(String arg0) {
- }
- @Override
- public void onStatusChanged(String arg0, int arg1, Bundle arg2) {
- }
- };
LocationListener locationListener = new LocationListener(){
@Override
public void onLocationChanged(Location location) {
textView.setText("位置发生变化,新位置: " + location.getLatitude() + " , " + location.getLongitude());
System.out.println("位置发生变化,新位置: " + location.getLatitude() + " , " + location.getLongitude());
//刷新图层
markLocation(location);
}
@Override
public void onProviderDisabled(String arg0) {
}
@Override
public void onProviderEnabled(String arg0) {
}
@Override
public void onStatusChanged(String arg0, int arg1, Bundle arg2) {
}
};
创建了这个监听后,需要让我们的位置管理器来启动这个监听,这样我们才能够实时的获取我们当前的位置,并作出相应的事件处理。
启用监听
- locMag.requestLocationUpdates(provider, 100, 0, locationListener);
- Location loc = locMag.getLastKnownLocation(provider);
- if(loc!=null){
- double latitude = loc.getLatitude();
- double longitude = loc.getLongitude();
- textView.setText(latitude + " , " + longitude);
- markLocation(loc);
- }
locMag.requestLocationUpdates(provider, 100, 0, locationListener);
Location loc = locMag.getLastKnownLocation(provider);
if(loc!=null){
double latitude = loc.getLatitude();
double longitude = loc.getLongitude();
textView.setText(latitude + " , " + longitude);
//开始画图
markLocation(loc);
}
上面的这段代码会试着在图层上面绘制出当前的位置。
- private void markLocation(Location location){
- double locx = location.getLongitude();
- double locy = location.getLatitude();
- gLayer.removeAll();
-
- Point wgspoint = new Point(locx, locy);
- Point mapPoint = (Point) GeometryEngine.project(wgspoint,SpatialReference.create(4326),mMapView.getSpatialReference());
-
- Graphic graphic = new Graphic(mapPoint,new SimpleMarkerSymbol(Color.RED,25,STYLE.CIRCLE));
- gLayer.addGraphic(graphic);
- }
private void markLocation(Location location){
double locx = location.getLongitude();
double locy = location.getLatitude();
gLayer.removeAll();
Point wgspoint = new Point(locx, locy);
Point mapPoint = (Point) GeometryEngine.project(wgspoint,SpatialReference.create(4326),mMapView.getSpatialReference());
//图层的创建
Graphic graphic = new Graphic(mapPoint,new SimpleMarkerSymbol(Color.RED,25,STYLE.CIRCLE));
gLayer.addGraphic(graphic);
}
上面的markLocation就是标记当前位置的代码。注意,GeometryEngine.project
这个方法是将经纬度投影到我们屏幕上地图的坐标位置。具体的API还没完全看明白,而这样定位到的地点也会有一小段距离的偏差,具体如何矫正可能要联系地图的服务商。
一个由于项目需要接触android的人——忆梦竹 写于2013/1/30