开发硬件条件:

1. Android系统的手机一台,我用的是4.0
2. 确定你的手机能够正常定位,因为我自己的手机无法定位,用同事的就可以定位了。

开发环境搭建:
Eclipse + ADT + ANDROID-SDK 2.3.3 ( API 10 ) + ARCGIS-ANDROID SDK 10.1.1

本文将代码分为片段讲解,最后会附上本文代码。
Java代码
  1. textView = new TextView(this);
  2. LinearLayout layout = new LinearLayout(this);
  3. layout.setOrientation(LinearLayout.VERTICAL);
  4. mMapView = new MapView(this);
  5. mMapView.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT));
  6. ArcGISTiledMapServiceLayer tileLayout = new ArcGISTiledMapServiceLayer(super.getString(R.string.map_url));
  7. mMapView.addLayer(tileLayout);
  8. gLayer = new GraphicsLayer();
  9. 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

Java代码
  1. //要定位在地图中的位置,需要知道当前位置,而当前位置有Location对象决定,
  2. //但是,Location对象又需要LocationManager对象来创建。
  3. locMag = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);//创建LocationManager的唯一方法
//要定位在地图中的位置,需要知道当前位置,而当前位置有Location对象决定,//但是,Location对象又需要LocationManager对象来创建。locMag = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);//创建LocationManager的唯一方法

接着需要获取当前的Location
Java代码
  1. String provider = LocationManager.NETWORK_PROVIDER;
  2. loc = locMag.getLastKnownLocation(provider);
  3. if(loc==null){
  4. provider = LocationManager.NETWORK_PROVIDER;
  5. loc = locMag.getLastKnownLocation(provider);
  6. }
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的时候,我下面的事件无法触发(因为我在房屋内开发)。

Java代码
  1. LocationListener locationListener = new LocationListener(){
  2. @Override
  3. public void onLocationChanged(Location location) {
  4. textView.setText("位置发生变化,新位置: " + location.getLatitude() + " , " + location.getLongitude());
  5. System.out.println("位置发生变化,新位置: " + location.getLatitude() + " , " + location.getLongitude());
  6. //刷新图层
  7. markLocation(location);
  8. }
  9. @Override
  10. public void onProviderDisabled(String arg0) {
  11. }
  12. @Override
  13. public void onProviderEnabled(String arg0) {
  14. }
  15. @Override
  16. public void onStatusChanged(String arg0, int arg1, Bundle arg2) {
  17. }
  18. };
LocationListener locationListener = new LocationListener(){@Overridepublic void onLocationChanged(Location location) {textView.setText("位置发生变化,新位置: " + location.getLatitude() + "  ,  " + location.getLongitude());System.out.println("位置发生变化,新位置: " + location.getLatitude() + "  ,  " + location.getLongitude());//刷新图层markLocation(location);}@Overridepublic void onProviderDisabled(String arg0) {}@Overridepublic void onProviderEnabled(String arg0) {}@Overridepublic void onStatusChanged(String arg0, int arg1, Bundle arg2) {}};

创建了这个监听后,需要让我们的位置管理器来启动这个监听,这样我们才能够实时的获取我们当前的位置,并作出相应的事件处理。
启用监听

Java代码
  1. locMag.requestLocationUpdates(provider, 100, 0, locationListener);
  2. Location loc = locMag.getLastKnownLocation(provider);
  3. if(loc!=null){
  4. double latitude = loc.getLatitude();
  5. double longitude = loc.getLongitude();
  6. textView.setText(latitude + " , " + longitude);
  7. //开始画图
  8. markLocation(loc);
  9. }
    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);}


上面的这段代码会试着在图层上面绘制出当前的位置。

Java代码
  1. private void markLocation(Location location){
  2. double locx = location.getLongitude();
  3. double locy = location.getLatitude();
  4. gLayer.removeAll();
  5. Point wgspoint = new Point(locx, locy);
  6. Point mapPoint = (Point) GeometryEngine.project(wgspoint,SpatialReference.create(4326),mMapView.getSpatialReference());
  7. //图层的创建
  8. Graphic graphic = new Graphic(mapPoint,new SimpleMarkerSymbol(Color.RED,25,STYLE.CIRCLE));
  9. gLayer.addGraphic(graphic);
  10. }
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

更多相关文章

  1. Android(安卓)webview解决JS报错chromium: [INFO:CONSOLE(1)] "U
  2. "android sdk Content Loader's has encountered a problem"的解
  3. 基于ActionbarActivity中Actionbar自定义布局
  4. android应用框架搭建------BaseActivity
  5. Android(安卓)Studio实现微信摇一摇(传感器)
  6. 多重搜索算法_Android多重搜寻,例如传送,搜寻联络人
  7. Wifi定位Gps
  8. 将获取的html源代码格式化输出
  9. Android(安卓)连接.net WebService 工具类代码

随机推荐

  1. Android基于XMPP Smack openfire 开发的
  2. Failed to fetch URL http://dl-ssl.goog
  3. Android自定义时间控件不可选择未来时间
  4. Android中线程通讯类Handler
  5. Android(安卓)MediaPlayer与Http Proxy结
  6. android的binder机制研究(C++部分) 分享
  7. Android 开机启动过程
  8. androidの高仿支付宝扫描动画效果
  9. Android Studio Gradle两种更新方式
  10. 关于Android(安卓)O系统短信拦截的流程