zoukankan      html  css  js  c++  java
  • Android开发之定位系统

    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 = “没有找到地址 ”;

        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(“ ”);

                }

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

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

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

                addressInfo = sb.toString();

            }

          } catch(IOException ex) { }

        }

        textview.setText(“你当前的位置如下: ”+latLng+" ”+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;

      }

    }

      }

    }

  • 相关阅读:
    网页定位导航
    position元素的定位
    节点属性
    css控制换行,断词
    css隐藏多余文字显示...
    重绘和回流
    CSS属性书写顺序
    模拟select
    常用html标签
    clientHeight、scrollHeight和offsetHeight基本用法
  • 原文地址:https://www.cnblogs.com/fengzhblog/p/3170803.html
Copyright © 2011-2022 走看看