2013-07-04

定位系统

全球定位系统(Global Positioning System, GPS), 又称全球卫星定位系统。

最少只需其中3颗卫星,就能迅速确定用户组地球所处的位置及海拔高度,所能连接的卫星数越多,解析出来的位置就越精确。

广泛应用于军事、物流、地理、移动电话、数码相机、航空等领域。

包android.location提供地理位置API ,其中几个重要的类:

LocationManager, 提供访问定位服务,获取最佳定位提供者,临近报警等功能。

LocationProvider, 具备周期性报告设备地理位置的功能。

LocationListener, 提供定位信息发送变化时回调功能。

Criteria, 通过LocationProvider中设置的属性来选择合适的定位提供者。

Geocode, 处理地理编码(将地址或其他描述转变为经度和纬度)和反向地理编码(将经度和纬度转变为地址或描述)。

生词:

Criteria 标准,尺度,准则

// 得到LocationManager

LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);

// 注册LocationListener

locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 0, locationListener);

// LocationListener中的几个抽象方法

// 当坐标发生变化时调用

onLocationChanged(Location location)

// 当LocationProvider禁用时调用

onProviderDisabled(String provider)

// 当LocationProvider启用时调用

onProviderEnabled(String provider)

// 当LocationProvider的状态发生变化时调用

onStatusChanged(String provider, int status, Bundle extras)

// 在AndroidManifest.xml文件中添加权限

<uses-permission android:name=”android.permission.INTERNET” />

<uses-permission android:name=”android.permission.ACCESS_COARSE_LOCATION” />

<uses-permission android:name=”android.permission.ACCESS_FINE_LOCATION” />

// 在模拟器上设置坐标

Eclipse, Window->Show View->Emulator Control, 手动或通过KML和GPX文件来设置一个坐标。

// 使用geo命令

telnet到本机的5554端口,在命令行下输入geo fix-121.45354 46.5119 4392

后面3个参数分别代表经度,纬度,和海拔

 

示例:

public class LocationActivity extends MapActivity {

 

  private LocationManager locationManager;

  private MapView mapView;

  private MapController mapController;

  private GeoPoint geoPoint;

  private static final int ZOOM_IN = Menu.FIRST;

  private static final int ZOOM_OUT = Menu.FIRST + 1;

 

  public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);

    setContentView(R.layout.main);

 

    locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);

    mapView = (MapView) findViewById(R.id.mapView01);

    mapView.setTraffic(true);

    mapView.setSatellite(true);

    mapView.setStreetView(true);

    mapView.displayZoomControls(false);

    mapView.setEnabled(true);

    mapView.setClickable(true);

    mapView.setBuiltInZoomControls(true);

 

    mapController = mapView.getController();

    geoPoint = new GeoPoint((int)(30.659259*1000000), (int)(104.065762*1000000));

    mapController.animateTo(geoPoint);

    mapController.setZoom(17);

 

    LocationOverlay locationOverlay = new LocationOverlay();

    List<Overlay> list = mapView.getOverlays();

    list.add(locationOverlay);

 

    Criteria criteria = new Criteria();

    criteria.setAccuracy(Criteria.ACCURACY_FINE);

    criteria.setAltitudeRequired(false);

    criteria.setBearingRequired(false);

    criteria.setCostAllowed(false);

    criteria.setPowerRequirement(Criteria.POWER_LOW);

   

    // 得到最近位置

    String provider = locationManager.getBestProvider(criteria, true);

    Location location = locationManager.getLastKnownLocation(provider);

    updateWithNewLocation(location);

 

    // 注册周期性更新,每隔3秒更新一次

    locationManager.requestLocationUpdates(provider, 3000, 0, locationListener);

  }

  private void updateWithNewLocation(Location location) {

    String latLng = “”; // 经纬度

    TextView textView = (TextView) findViewById(R.id.textview01);

    String addressInfo = “没有找到地址\n”;

    if(location == null) {

      latLng = addressInfo;

    } else {

      locationOverlay.setLocation(location);

      // 得到经度和纬度

      Double geoLat = location.getLatitude() * 1E6;

      Double geoLng = location.getLongitude() * 1E6;

      GeoPoint point = new GeoPoint(geoLat.intValue(), geoLng.intValue());

      // 在地图上定位到指定位置

      mapController.animateTo(point);

      double lat = location.getLatitude();

      double lng = location.getLongitude();

      latLng = “经度:”+lat+”,纬度:”+lng;

      // 设置本地编码方式

      Geocode geocode = new Geocode(this, Locale.getDefault());

      try {

        List<Address> addresses = geocode.getFromLocation(lat, lng, 1);

        StringBuffer sb = new StringBuffer();

        if(addresses.size() > 0) {

            Address address = addresses.get(0);

            for(int i=0; i<address.getMaxAddressLineIndex(); i++) {

              sb.append(address.getAddressLine(i)).append(“\n”);

            }

            sb.append(address.getLocality(i)).append(“\n”);

            sb.append(address.getPostalCode(i)).append(“\n”);

            sb.append(address.getCountryName(i)).append(“\n”);

            addressInfo = sb.toString();

        }

      } catch(IOException ex) { }

    }

    textview.setText(“你当前的位置如下:\n”+latLng+"\n”+addressInfo);

  }

  protected boolean isRouteDisplayed() {

    return false;

  }

  private final LocationListener locationListener = new LocationListener() {

    // 坐标改变时调用

    public void onLocationChanged(Location loaction) {

      updateWithNewLocation(location);

    }

    // Provider禁用时调用

    public void onProviderDisabled(String provider) {

      updateWithNewLocation(null);

    }

    // Provider启用时调用

    public void onProviderEnabled(String provider) { }

    // Provider状态发生变化时调用
    public void onStatusChanged(String provider, int status, Bundle extras) { }

  };

  // 添加菜单

  public boolean onCreateOptionMenu(Menu menu) {

    super.onCreateOptionMenu(menu);

    menu.add(0, ZOOM_IN, Menu.NONE, “放大”);

    menu.add(0, ZOOM_OUT, Menu.NONE, “缩小”);

    return true;

  }

  public boolean onOptionsItemSelected(MenuItem item) {

    super.onOptionsItemSelected(item);

    switch(item.getItemId()) {

      case ZOOM_IN:

        // 放大

        mapController.zoomIn();

        return true;

      case ZOOM_OUT:

        // 缩小

        mapController.zoomOut();

        return true;

      return true;

    }

class LocationOverlay extends Overlay {

  public boolean draw(Canvas canvas, MapView mapView, boolean shadow, long when) {

    // 绘制图标文字等信息

    super.draw(canvas, mapView, shadow);

    Paint paint = new Paint();

    Point point = new Point();

    GeoPoint geoPoint = new GeoPoint(location.getLatitude() * 1E6, location.getLongitude() * 1E6);

    mapView.getProjection().toPixels(geoPoint, point);

    paint.setStrokeWidth(1);

    paint.setARGB(255, 255, 0, 0);

    paint.setStyle(Paint.Style.STROKE);

    Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.home);

    canvas.drawBitmap(bitmap, point.x, point.y, point);

    canvas.drawText(“I’m here.”, point.x, point.y, point);

    return true;

  }

}

  }

}

Android开发之定位系统的更多相关文章

  1. Android开发权威指南(第2版)新书发布

    <Android 开发权威指南(第二版)>是畅销书<Android开发权威指南>的升级版,内容更新超过80%,是一本全面介绍Android应用开发的专著,拥有45 章精彩内容供 ...

  2. 使用Visual Studio进行 Android开发的十大理由

    [原文发表地址]Top 10 reasons to use Visual Studio for C++ Android Development! Visual Studio: C++跨平台的移动解决方 ...

  3. Qt开发北斗定位系统融合百度地图API及Qt程序打包发布

    Qt开发北斗定位系统融合百度地图API及Qt程序打包发布 1.上位机介绍 最近有个接了一个小型项目,内容很简单,就是解析北斗GPS的串口数据然后输出经纬度,但接过来觉得太简单,就发挥了主观能动性,增加 ...

  4. Android学习探索之Java 8 在Android 开发中的应用

    前言: Java 8推出已经将近2年多了,引入很多革命性变化,加入了函数式编程的特征,使基于行为的编程成为可能,同时减化了各种设计模式的实现方式,是Java有史以来最重要的更新.但是Android上, ...

  5. Android 开发一定要看的15个实战项目

    前言: 虽说网上有太多的Android课程,但是大多都是视频,有Android在线开发环境的几乎没有,但是对于学习Android的人来说拥有在线的Android开发环境是非常好的,可以随时动手操作学习 ...

  6. Android开发学习之路-关于Exception

    Exception在Java中是表示异常的一个类.它是Throwable的子类. 而Exception的子类RuntimeException是一个特殊的异常类,在代码中不需要对此类进行throw,而是 ...

  7. Android开发学习之路-Android中使用RxJava

    RxJava的核心内容很简单,就是进行异步操作.类似于Handler和AsyncTask的功能,但是在代码结构上不同. RxJava使用了观察者模式和建造者模式中的链式调用(类似于C#的LINQ). ...

  8. Android开发学习之路-记一次CSDN公开课

    今天的CSDN公开课Android事件处理重难点快速掌握中老师讲到一个概念我觉得不正确. 原话是这样的:点击事件可以通过事件监听和回调两种方法实现. 我一听到之后我的表情是这样的: 这跟我学的看的都不 ...

  9. Android开发学习之路-RecyclerView滑动删除和拖动排序

    Android开发学习之路-RecyclerView使用初探 Android开发学习之路-RecyclerView的Item自定义动画及DefaultItemAnimator源码分析 Android开 ...

随机推荐

  1. c# -- 实现浏览功能(备忘)

    最近在做系统的时候,要实现浏览功能,但是由于本人记性一般,每次写完就忘,所以还是写篇随笔,备忘一下,方便以后查看@_@# 实现功能大概如下: 按钮1:点击浏览按钮后,选择文件(类型为.txt),默认位 ...

  2. Codeforces Round #300 C. Tourist's Notes 水题

    C. Tourist's Notes Time Limit: 1 Sec  Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/538/pr ...

  3. 【转】Internet连接正常但是没有网络,禁用以太网以后再重新启动就可以使用了,原因是什么?

    只是粘贴别人的答案,觉得有理,就放在博客里方便以后再学习~ 这个和网络中hdcp服务有关,网卡要在网路中通讯就必须要网络设备一般是路由器或者交换机分配地址,只有给了你电脑门牌号,信件投递能准确无误.你 ...

  4. React-如何在jsx中自动补全标签(vscode)

    痛点:  React库最近的增长趋势很明显, 很多朋友都在选择学习, 很多公司也在选择使用React栈. 但在使用React库写代码的时候, 有一个很让人苦恼的问题, 就是标签在jsx语法中不能自动补 ...

  5. OSChina.net 的 Tomcat 配置 server.xml 参考

    这是目前 oschina.net 正在使用的 tomcat 的 server.xml 的配置文件内容 <Server port="9005" shutdown="S ...

  6. Android的Master/Detail风格界面中实现自定义ListView的单选

    原文在这里:http://duduli.iteye.com/blog/1453576 可以实现多选,那么如何实现单选呢,这里我写了一个非常简单的方法: public void onListItemCl ...

  7. linux下的系统调用函数到内核函数的追踪

    http://blog.csdn.net/maochengtao/article/details/23598433

  8. JavaScript学习10:动态载入脚本和样式

    我们在写Web页面的时候,须要引入非常多的JavaScript脚本文件和CSS样式文件,尤其是在站点需求量非常大的时候,脚本的需求量也随之变大,这样一来,站点的性能就会大打折扣.因此就出现了动态载入的 ...

  9. The maximum number of processes for the user account running is currently , which can cause performance issues. We recommend increasing this to at least 4096.

    [root@localhost ~]# vi /etc/security/limits.conf # /etc/security/limits.conf # #Each line describes ...

  10. 体验NW.js打包一个桌面应用

    1.安装nw,(也可在官网下载然后配置变量) npm install nw -g 一个最最简单的nw应用,只需要有index.html和package.json文件即可 2.项目准备,目录结构 app ...