示例#1
1
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    instance = this;
    // Gl app = (Gl)this.getApplication();
    if (mBMapManager == null) {
      initEngineManager(this.getApplicationContext());
    }
    setContentView(R.layout.activity_baidumap);
    mMapView = (MapView) findViewById(R.id.bmapView);
    mMapController = mMapView.getController();
    sendButton = (Button) findViewById(R.id.btn_location_send);
    initMapView();

    mMapView.getController().setZoom(17);
    mMapView.getController().enableClick(true);
    mMapView.setBuiltInZoomControls(true);
    Intent intent = getIntent();
    double latitude = intent.getDoubleExtra("latitude", 0);
    if (latitude == 0) {
      showMapWithLocationClient();
    } else {
      double longtitude = intent.getDoubleExtra("longitude", 0);
      String address = intent.getStringExtra("address");
      showMap(latitude, longtitude, address);
    }
  }
示例#2
0
 protected void initMapController(MapView mapView) {
   mMapController = mapView.getController();
   mMapController.enableClick(true);
   mMapController.setZoom(ZOOM_LEVEL);
   mapView.setBuiltInZoomControls(true);
   mapView.showScaleControl(true);
 }
  @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);
    }
  }
示例#4
0
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    AppContext app = AppContext.getInstance();
    setContentView(R.layout.routeplan);
    CharSequence titleLable = "路线规划功能";
    setTitle(titleLable);
    // 初始化地图
    mMapView = (MapView) findViewById(R.id.bmapView);

    // GeoPoint mGeoPoint = new GeoPoint((int) (31.224078 * 1E6), (int)
    // (121.540419 * 1E6));

    mMapController = mMapView.getController();
    // 设置地图的中心点
    mMapView.setBuiltInZoomControls(false);
    // mMapView.getController().setCenter(mGeoPoint);
    mMapView.getController().setZoom(12);
    mMapView.getController().enableClick(true);

    // 初始化按键
    mBtnDrive = (Button) findViewById(R.id.drive);
    mBtnTransit = (Button) findViewById(R.id.transit);
    mBtnWalk = (Button) findViewById(R.id.walk);
    mBtnPre = (Button) findViewById(R.id.pre);
    mBtnNext = (Button) findViewById(R.id.next);
    mBtnPre.setVisibility(View.INVISIBLE);
    mBtnNext.setVisibility(View.INVISIBLE);

    // 处理搜索按钮响应
    editSt = (AutoCompleteTextView) findViewById(R.id.start);
    editEn = (AutoCompleteTextView) findViewById(R.id.end);
    adapter = new ArrayAdapter<String>(this, android.R.layout.simple_dropdown_item_1line);
    editEn.setAdapter(adapter);
    editSt.setAdapter(adapter);

    editSt.addTextChangedListener(
        new TextWatcher() {

          @Override
          public void onTextChanged(CharSequence s, int start, int before, int count) {
            // mSearch.poiSearchInCity("上海", editEn.getText().toString());
            ptStart = null;
            if (s.length() <= 0) {
              return;
            }
            /** 使用建议搜索服务获取建议列表,结果在onSuggestionResult()中更新 */
            mSearch.suggestionSearch(s.toString(), "上海");
          }

          @Override
          public void beforeTextChanged(CharSequence s, int start, int count, int after) {}

          @Override
          public void afterTextChanged(Editable s) {}
        });

    editEn.addTextChangedListener(
        new TextWatcher() {

          @Override
          public void onTextChanged(CharSequence s, int start, int before, int count) {
            // mSearch.poiSearchInCity("上海", editEn.getText().toString());
            ptEnd = null;
            if (s.length() <= 0) {
              return;
            }
            /** 使用建议搜索服务获取建议列表,结果在onSuggestionResult()中更新 */
            mSearch.suggestionSearch(s.toString(), "上海");
          }

          @Override
          public void beforeTextChanged(CharSequence s, int start, int count, int after) {}

          @Override
          public void afterTextChanged(Editable s) {}
        });
    // 按键点击事件
    OnClickListener clickListener =
        new OnClickListener() {
          public void onClick(View v) {
            // 发起搜索
            SearchButtonProcess(v);
          }
        };
    OnClickListener nodeClickListener =
        new OnClickListener() {
          public void onClick(View v) {
            // 浏览路线节点
            nodeClick(v);
          }
        };

    mBtnDrive.setOnClickListener(clickListener);
    mBtnTransit.setOnClickListener(clickListener);
    mBtnWalk.setOnClickListener(clickListener);
    mBtnPre.setOnClickListener(nodeClickListener);
    mBtnNext.setOnClickListener(nodeClickListener);
    // 创建 弹出泡泡图层
    createPaopao();

    // 地图点击事件处理
    mMapView.regMapTouchListner(
        new MKMapTouchListener() {

          @Override
          public void onMapClick(GeoPoint point) {
            // 在此处理地图点击事件
            // 消隐pop
            if (pop != null) {
              pop.hidePop();
            }
          }

          @Override
          public void onMapDoubleClick(GeoPoint point) {}

          @Override
          public void onMapLongClick(GeoPoint point) {}
        });
    // 初始化搜索模块,注册事件监听
    mSearch = new MKSearch();
    // mSearch.geocode("栖山路1460弄28号", "上海");

    mSearch.init(
        app.mBMapManager,
        new MKSearchListener() {

          public void onGetDrivingRouteResult(MKDrivingRouteResult res, int error) {
            pb.dismiss();
            // 起点或终点有歧义,需要选择具体的城市列表或地址列表
            if (error == MKEvent.ERROR_ROUTE_ADDR) {
              // 遍历所有地址
              Toast.makeText(RoutePlanDemo.this, "起点或终点有歧义,需要选择具体的地址列表", Toast.LENGTH_LONG).show();
              return;
            }
            // 错误号可参考MKEvent中的定义
            if (error != 0 || res == null) {
              Toast.makeText(RoutePlanDemo.this, "抱歉,未找到结果", Toast.LENGTH_SHORT).show();
              return;
            }

            searchType = 0;
            routeOverlay = new RouteOverlay(RoutePlanDemo.this, mMapView);
            // 此处仅展示一个方案作为示例
            routeOverlay.setData(res.getPlan(0).getRoute(0));
            // 清除其他图层
            mMapView.getOverlays().clear();
            // 添加路线图层
            mMapView.getOverlays().add(routeOverlay);
            // 执行刷新使生效
            mMapView.refresh();
            // 使用zoomToSpan()绽放地图,使路线能完全显示在地图上
            mMapView
                .getController()
                .zoomToSpan(routeOverlay.getLatSpanE6(), routeOverlay.getLonSpanE6());
            // 移动地图到起点
            mMapView.getController().animateTo(res.getStart().pt);
            // 将路线数据保存给全局变量
            route = res.getPlan(0).getRoute(0);
            // 重置路线节点索引,节点浏览时使用
            nodeIndex = -1;
            mBtnPre.setVisibility(View.VISIBLE);
            mBtnNext.setVisibility(View.VISIBLE);
          }

          public void onGetTransitRouteResult(MKTransitRouteResult res, int error) {
            pb.dismiss();
            // 起点或终点有歧义,需要选择具体的城市列表或地址列表
            if (error == MKEvent.ERROR_ROUTE_ADDR) {
              // 遍历所有地址
              // ArrayList<MKPoiInfo> stPois =
              // res.getAddrResult().mStartPoiList;
              // ArrayList<MKPoiInfo> enPois =
              // res.getAddrResult().mEndPoiList;
              // listAddress = new ArrayList<String>();
              // for (int i = 0; i < enPois.size(); i++) {
              // listAddress.add(enPois.get(i).name);
              // System.out.println("stPois.get(i).name------------"+enPois.get(i).name);
              // }
              //
              // ArrayList<MKCityListInfo> stCities =
              // res.getAddrResult().mStartCityList;
              // ArrayList<MKCityListInfo> enCities =
              // res.getAddrResult().mEndCityList;
              Toast.makeText(RoutePlanDemo.this, "起点或终点有歧义,需要选择具体的地址列表", Toast.LENGTH_LONG).show();
              return;
            }
            if (error != 0 || res == null) {
              Toast.makeText(RoutePlanDemo.this, "抱歉,未找到结果", Toast.LENGTH_SHORT).show();
              return;
            }

            searchType = 1;
            transitOverlay = new TransitOverlay(RoutePlanDemo.this, mMapView);
            // 此处仅展示一个方案作为示例
            transitOverlay.setData(res.getPlan(0));
            // 清除其他图层
            mMapView.getOverlays().clear();
            // 添加路线图层
            mMapView.getOverlays().add(transitOverlay);
            // 执行刷新使生效
            mMapView.refresh();
            // 使用zoomToSpan()绽放地图,使路线能完全显示在地图上
            mMapView
                .getController()
                .zoomToSpan(transitOverlay.getLatSpanE6(), transitOverlay.getLonSpanE6());
            // 移动地图到起点
            mMapView.getController().animateTo(res.getStart().pt);
            // 重置路线节点索引,节点浏览时使用
            nodeIndex = 0;
            mBtnPre.setVisibility(View.VISIBLE);
            mBtnNext.setVisibility(View.VISIBLE);
          }

          public void onGetWalkingRouteResult(MKWalkingRouteResult res, int error) {
            pb.dismiss();
            // 起点或终点有歧义,需要选择具体的城市列表或地址列表
            if (error == MKEvent.ERROR_ROUTE_ADDR) {
              // 遍历所有地址
              Toast.makeText(RoutePlanDemo.this, "起点或终点有歧义,需要选择具体的地址列表", Toast.LENGTH_LONG).show();
              return;
            }
            if (error != 0 || res == null) {
              Toast.makeText(RoutePlanDemo.this, "抱歉,未找到结果", Toast.LENGTH_SHORT).show();
              return;
            }

            searchType = 2;
            routeOverlay = new RouteOverlay(RoutePlanDemo.this, mMapView);
            // 此处仅展示一个方案作为示例
            routeOverlay.setData(res.getPlan(0).getRoute(0));
            // 清除其他图层
            mMapView.getOverlays().clear();
            // 添加路线图层
            mMapView.getOverlays().add(routeOverlay);
            // 执行刷新使生效
            mMapView.refresh();
            // 使用zoomToSpan()绽放地图,使路线能完全显示在地图上
            mMapView
                .getController()
                .zoomToSpan(routeOverlay.getLatSpanE6(), routeOverlay.getLonSpanE6());
            // 移动地图到起点
            mMapView.getController().animateTo(res.getStart().pt);
            // 将路线数据保存给全局变量
            route = res.getPlan(0).getRoute(0);
            // 重置路线节点索引,节点浏览时使用
            nodeIndex = -1;
            mBtnPre.setVisibility(View.VISIBLE);
            mBtnNext.setVisibility(View.VISIBLE);
          }

          // 通过 经度纬度 找到地址
          //
          //
          public void onGetAddrResult(MKAddrInfo res, int error) {
            if (res == null) {
              return;
            }

            // 判断该地址附近是否有POI(Point of Interest,即兴趣点)
            if (null != res.poiList) {
              // 将地址信息、兴趣点信息显示在TextView上
              editSt.setText(res.poiList.get(0).name);
              ptStart =
                  new GeoPoint(
                      (int) (res.poiList.get(0).pt.getLatitudeE6()),
                      (int) (res.poiList.get(0).pt.getLongitudeE6()));

              mSearch.poiSearchInCity("上海", editEn.getText().toString());
            } else {
              Toast.makeText(RoutePlanDemo.this, "抱歉,没找到您的当前位置", Toast.LENGTH_LONG).show();
            }
          }

          public void onGetPoiResult(MKPoiResult res, int arg1, int error) {
            System.out.println(res + "-------------res");
            if (pb1 != null) pb1.dismiss();
            // 错误号可参考MKEvent中的定义
            if (error != 0 || res == null) {
              Toast.makeText(RoutePlanDemo.this, "抱歉,未找到结果", Toast.LENGTH_LONG).show();
              return;
            }
            // 将地图移动到第一个POI中心点
            if (res.getCurrentNumPois() > 0) {
              // 将poi结果显示到地图上
              try {
                MyPoiOverlay poiOverlay = new MyPoiOverlay(RoutePlanDemo.this, mMapView, mSearch);
                ArrayList<MKPoiInfo> list = new ArrayList<MKPoiInfo>();
                res.getAllPoi().get(0).name = editEn.getText().toString();
                list.add(res.getAllPoi().get(0)); // 只加一项
                // editEn.setText(res.getAllPoi().get(0).name);
                ptEnd =
                    new GeoPoint(
                        (int) (res.getAllPoi().get(0).pt.getLatitudeE6()),
                        (int) (res.getAllPoi().get(0).pt.getLongitudeE6()));

                poiOverlay.setData(list);

                mMapView.getOverlays().clear();
                mMapView.getOverlays().add(poiOverlay);

                mMapView.refresh();
                // 当ePoiType为2(公交线路)或4(地铁线路)时, poi坐标为空
                for (MKPoiInfo info : res.getAllPoi()) {
                  if (info.pt != null) {
                    mMapView.getController().animateTo(info.pt);
                    break;
                  }
                }
              } catch (Exception e) {
                e.printStackTrace();
              }
            } else if (res.getCityListNum() > 0) {
              // 当输入关键字在本市没有找到,但在其他城市找到时,返回包含该关键字信息的城市列表
              String strInfo = "在";
              for (int i = 0; i < res.getCityListNum(); i++) {
                strInfo += res.getCityListInfo(i).city;
                strInfo += ",";
              }
              strInfo += "找到结果";
              Toast.makeText(RoutePlanDemo.this, strInfo, Toast.LENGTH_LONG).show();
            }
          }

          public void onGetBusDetailResult(MKBusLineResult result, int iError) {}

          @Override
          public void onGetSuggestionResult(MKSuggestionResult res, int arg1) {
            // if (!isFirstEdit) {
            if (res == null || res.getAllSuggestions() == null) {
              return;
            }
            adapter.clear();
            for (MKSuggestionInfo info : res.getAllSuggestions()) {
              if (info.key != null) adapter.add(info.key);
            }
            adapter.notifyDataSetChanged();

            // } else {
            // if (res == null || res.getAllSuggestions() == null) {
            // return;
            // }
            // editEn.setText(res.getAllSuggestions().get(0).key);
            // isFirstEdit = false;
            // }
          }

          @Override
          public void onGetPoiDetailSearchResult(int type, int iError) {}

          @Override
          public void onGetShareUrlResult(MKShareUrlResult result, int type, int error) {}
        });
    editEn.setText(getIntent().getStringExtra(WebViewActivity.ENDSTR));
    pb1 = new ProgressDialog(this);
    pb1.setMessage("正在定位...");
    pb1.show();
    new Thread() {
      public void run() {
        try {
          Thread.sleep(0);
        } catch (InterruptedException e) {
          e.printStackTrace();
        }
        runOnUiThread(
            new Runnable() {
              @Override
              public void run() {
                // mSearch.suggestionSearch(editEn.getText().toString(),
                // "上海");
                // mSearch.poiSearchInCity("上海",
                // editEn.getText().toString());
                // 定位初始化
                mLocClient = new LocationClient(RoutePlanDemo.this);
                locData = new LocationData();
                mLocClient.registerLocationListener(myListener);
                LocationClientOption option = new LocationClientOption();
                option.setOpenGps(true); // 打开gps
                option.setCoorType("bd09ll"); // 设置坐标类型
                option.setScanSpan(1000);
                mLocClient.setLocOption(option);
                mLocClient.start();
              }
            });
      }
    }.start();

    pb = new ProgressDialog(this);
    pb.setMessage("正在搜索...");
    // 定位图层初始化
    // myLocationOverlay = new locationOverlay(mMapView);
    // // 设置定位数据
    // myLocationOverlay.setData(locData);
    // // 添加定位图层
    // mMapView.getOverlays().add(myLocationOverlay);
    // myLocationOverlay.enableCompass();
    // // 修改定位数据后刷新图层生效
    // mMapView.refresh();
  }
 private void initMapView() {
   mMapView.setLongClickable(true);
   mMapView.getController().setZoom(14);
   mMapView.getController().enableClick(true);
   mMapView.setBuiltInZoomControls(true);
 }
示例#6
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);
  }
 /**
  * 是否显示内置绽放控件
  *
  * @param v
  */
 public void setBuiltInZoomControllEnable(View v) {
   mMapView.setBuiltInZoomControls(((CheckBox) v).isChecked());
 }