Esempio n. 1
0
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    // 初始化地图管理器
    mMapManager = new BMapManager(getApplication());
    mMapManager.init(accessKey, new MyMKGeneralListener());
    // 初始化各种与地图无关的成员变量
    appInit();

    regeisterSensorManager();

    textShow = (TextView) findViewById(R.id.textShow);
    textShow.setTextSize(TextShowTextSize);
    // 初始化mapview对象,并且设置显示缩放控件
    mMapView = (MapView) findViewById(R.id.bmapsView);

    mMapView.setBuiltInZoomControls(true);
    mMapView.showScaleControl(true);
    mMapView.setDoubleClickZooming(false);
    // 定义地图控件,获取mapview的控制,并把地图范围定位在宿舍
    mMapView.regMapViewListener(mMapManager, new MyMKMapViewListener());
    mMapView.regMapTouchListner(new MyMKMapTouchListener());
    mMapController = mMapView.getController();
    // 初始化locationClient服务
    clientInit();
    // 此处启动,将会在第一次定位后自动关闭
    mLocationClient.start();
    // 使用上一次退出时的位置数据作为初始化的地点
    mMapController.animateTo(new GeoPoint(appData.lastLatitude, appData.lastLongitude));
    mMapController.setZoom(appData.lastZoom);

    // 此处进行定时器设置,定时器每隔500ms执行一次
    handler =
        new Handler() {
          public void handleMessage(Message msg) {
            if (msg.what != MainActivity.UPDATE_TEXT) {
              return;
            }
            // 截图
            if (isCut) {
              cutSpeed++;
              if (cutSpeed % 8 == 1) {
                mMapView.getCurrentMap();
              }
            }

            if (!mLocationClient.isStarted() || appData.isFirst) {
              appData.textShowStrings[0] = "未启动导航功能";
              appData.textShowStrings[1] = "";
              textShow.setTextColor(Color.BLACK);
              appData.updateTextShow();
              mMapController.setRotation(0);
              return;
            }

            GeoPoint curPos = new GeoPoint(appData.lastLatitude, appData.lastLongitude);
            double radius = roadData.getRadius(curPos);
            double curSpeed, safeSpeed;

            // 返回的应该是米每秒
            safeSpeed = appData.getSafeSpeed(radius);
            if (safeSpeed > 120.0 / 3.6) {
              safeSpeed = 120.0 / 3.6;
            }

            // 该速度为 千米每小时
            curSpeed = appData.lastSpeed / 3.6f;

            appData.textShowStrings[0] = "当前车速为:" + (int) (curSpeed * 3.6) + "km/h";
            if (curSpeed <= safeSpeed) {
              appData.textShowStrings[1] = "安全车速为:" + (int) (safeSpeed * 3.6) + "km/h";
              textShow.setTextColor(Color.BLUE);
            } else {
              appData.textShowStrings[1] =
                  "安全车速为:" + (int) (safeSpeed * 3.6) + "km/h" + "\n您已超速!请迅速减速";
              textShow.setTextColor(Color.RED);
              Toast.makeText(MainActivity.this, "您已超过安全车速,请迅速减速!", Toast.LENGTH_LONG).show();
            }
            appData.updateTextShow();
          }
        };

    // 启动定时器
    new Timer()
        .schedule(
            new TimerTask() {
              @Override
              public void run() {
                // TODO Auto-generated method stub
                Message msg = new Message();
                msg.what = MainActivity.UPDATE_TEXT;
                handler.sendMessage(msg);
              }
            },
            500,
            500);
  }
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    DemoApplication app = (DemoApplication) this.getApplication();
    if (app.mBMapManager == null) {
      app.mBMapManager = new BMapManager(this);
      app.mBMapManager.init(DemoApplication.strKey, new DemoApplication.MyGeneralListener());
    }
    setContentView(R.layout.activity_itemizedoverlay);
    mMapView = (MapView) findViewById(R.id.bmapView);
    mMapController = mMapView.getController();
    initMapView();
    mMapView.getController().setZoom(13);
    mMapView.getController().enableClick(true);
    mMapView.setBuiltInZoomControls(true);
    testItemButton = (Button) findViewById(R.id.button1);
    removeItemButton = (Button) findViewById(R.id.button2);
    removeAllItemButton = (Button) findViewById(R.id.button3);

    Drawable marker = ItemizedOverlayDemo.this.getResources().getDrawable(R.drawable.icon_marka);
    mMapView.getOverlays().clear();
    ov = new OverlayTest(marker, this, mMapView);
    mMapView.getOverlays().add(ov);

    OnClickListener clickListener =
        new OnClickListener() {
          public void onClick(View v) {
            testItemClick();
          }
        };
    OnClickListener removeListener =
        new OnClickListener() {
          public void onClick(View v) {
            testRemoveItemClick();
          }
        };
    OnClickListener removeAllListener =
        new OnClickListener() {
          public void onClick(View v) {
            testRemoveAllItemClick();
          }
        };

    testItemButton.setOnClickListener(clickListener);
    removeItemButton.setOnClickListener(removeListener);
    removeAllItemButton.setOnClickListener(removeAllListener);
    mMapListener =
        new MKMapViewListener() {

          @Override
          public void onMapMoveFinish() {
            // TODO Auto-generated method stub

          }

          @Override
          public void onClickMapPoi(MapPoi mapPoiInfo) {
            // TODO Auto-generated method stub
            String title = "";
            if (mapPoiInfo != null) {
              title = mapPoiInfo.strText;
              Toast.makeText(ItemizedOverlayDemo.this, title, Toast.LENGTH_SHORT).show();
            }
          }

          @Override
          public void onGetCurrentMap(Bitmap b) {}

          @Override
          public void onMapAnimationFinish() {
            // TODO Auto-generated method stub

          }
        };
    mMapView.regMapViewListener(DemoApplication.getInstance().mBMapManager, mMapListener);

    res.add(getResources().getDrawable(R.drawable.icon_marka));
    res.add(getResources().getDrawable(R.drawable.icon_markb));
    res.add(getResources().getDrawable(R.drawable.icon_markc));
    res.add(getResources().getDrawable(R.drawable.icon_markd));
    res.add(getResources().getDrawable(R.drawable.icon_markf));
    res.add(getResources().getDrawable(R.drawable.icon_markg));
    res.add(getResources().getDrawable(R.drawable.icon_markh));
    res.add(getResources().getDrawable(R.drawable.icon_marki));

    // overlay 数量
    int iSize = 9;
    double pi = 3.1415926;
    // overlay半径
    int r = 50000;
    // 准备overlay 数据
    for (int i = 0; i < iSize; i++) {
      int lat = (int) (cLat + r * Math.cos(2 * i * pi / iSize));
      int lon = (int) (cLon + r * Math.sin(2 * i * pi / iSize));
      OverlayItem item = new OverlayItem(new GeoPoint(lat, lon), "item" + i, "item" + i);
      item.setMarker(res.get(i % (res.size())));
      mGeoList.add(item);
    }
  }
Esempio n. 3
0
  private void showMapWithLocationClient() {
    progressDialog = new ProgressDialog(this);
    progressDialog.setCanceledOnTouchOutside(false);
    progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
    progressDialog.setMessage("正在确定你的位置...");

    progressDialog.setOnCancelListener(
        new OnCancelListener() {

          public void onCancel(DialogInterface arg0) {
            if (progressDialog.isShowing()) {
              progressDialog.dismiss();
            }
            Log.d("map cancel retrieve location");
            finish();
          }
        });

    progressDialog.show();

    mLocClient = new LocationClient(this);
    mLocClient.registerLocationListener(myListener);

    LocationClientOption option = new LocationClientOption();
    option.setOpenGps(true); // 打开gps
    // option.setCoorType("bd09ll"); //设置坐标类型
    // Johnson change to use gcj02 coordination. chinese national standard
    // so need to conver to bd09 everytime when draw on baidu map
    option.setCoorType("gcj02");
    option.setScanSpan(30000);
    option.setAddrType("all");
    mLocClient.setLocOption(option);

    Drawable marker = this.getResources().getDrawable(R.drawable.icon_marka);
    // 为maker定义位置和边界
    marker.setBounds(0, 0, marker.getIntrinsicWidth(), marker.getIntrinsicHeight());
    mAddrOverlay = new ItemizedOverlay<OverlayItem>(marker, mMapView);
    mMapView.getOverlays().add(mAddrOverlay);

    mMapListener =
        new MKMapViewListener() {

          @Override
          public void onMapMoveFinish() {
            // TODO Auto-generated method stub
          }

          @Override
          public void onClickMapPoi(MapPoi mapPoiInfo) {
            // TODO Auto-generated method stub
            String title = "";
            if (mapPoiInfo != null) {
              title = mapPoiInfo.strText;
              Toast.makeText(BaiduMapActivity.this, title, Toast.LENGTH_SHORT).show();
            }
          }

          @Override
          public void onGetCurrentMap(Bitmap b) {
            // TODO Auto-generated method stub

          }

          @Override
          public void onMapAnimationFinish() {}
        };
    mMapView.regMapViewListener(mBMapManager, mMapListener);

    if (lastLocation != null) {
      GeoPoint point1 =
          new GeoPoint(
              (int) (lastLocation.getLatitude() * 1e6), (int) (lastLocation.getLongitude() * 1e6));
      point1 = CoordinateConvert.fromGcjToBaidu(point1);
      mMapController.setCenter(point1);
    }
    mMapView.refresh();
    mMapView.invalidate();
  }