Ejemplo n.º 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);
    }
  }
  /**
   * 节点浏览示例
   *
   * @param v
   */
  public void nodeClick(View v) {

    if (nodeIndex < -1 || route == null || nodeIndex >= route.getNumSteps()) return;
    viewCache = getLayoutInflater().inflate(R.layout.custom_text_view, null);
    popupText = (TextView) viewCache.findViewById(R.id.textcache);
    // 上一个节点
    if (mBtnPre.equals(v) && nodeIndex > 0) {
      // 索引减
      nodeIndex--;
      // 移动到指定索引的坐标
      mMapView.getController().animateTo(route.getStep(nodeIndex).getPoint());
      // 弹出泡泡
      popupText.setText(route.getStep(nodeIndex).getContent());
      popupText.setBackgroundResource(R.drawable.popup);
      pop.showPopup(BMapUtil.getBitmapFromView(popupText), route.getStep(nodeIndex).getPoint(), 5);
    }
    // 下一个节点
    if (mBtnNext.equals(v) && nodeIndex < (route.getNumSteps() - 1)) {
      // 索引加
      nodeIndex++;
      // 移动到指定索引的坐标
      mMapView.getController().animateTo(route.getStep(nodeIndex).getPoint());
      // 弹出泡泡
      popupText.setText(route.getStep(nodeIndex).getContent());
      popupText.setBackgroundDrawable(getResources().getDrawable(R.drawable.popup));
      pop.showPopup(BMapUtil.getBitmapFromView(popupText), route.getStep(nodeIndex).getPoint(), 5);
    }
  }
Ejemplo n.º 3
0
 private void initMap() {
   mMapView =
       ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getMapView();
   GeoPoint p = new GeoPoint((int) (39.945 * 1E6), (int) (116.404 * 1E6));
   mMapView.getController().setCenter(p);
   mMapView.getController().setZoom(13);
 }
Ejemplo n.º 4
0
 @Override
 public void onCreate(Bundle savedInstanceState) {
   super.onCreate(savedInstanceState);
   /**
    * 使用地图sdk前需先初始化BMapManager. BMapManager是全局的,可为多个MapView共用,它需要地图模块创建前创建,
    * 并在地图地图模块销毁后销毁,只要还有地图模块在使用,BMapManager就不应该销毁
    */
   DemoApplication app = (DemoApplication) this.getApplication();
   if (app.mBMapManager == null) {
     app.mBMapManager = new BMapManager(getApplicationContext());
     /** 如果BMapManager没有初始化则初始化BMapManager */
     app.mBMapManager.init(DemoApplication.strKey, new DemoApplication.MyGeneralListener());
   }
   /** 由于MapView在setContentView()中初始化,所以它需要在BMapManager初始化之后 */
   setContentView(R.layout.activity_uisetting);
   mMapView = (MapView) findViewById(R.id.bmapView);
   /** 获取地图控制器 */
   mMapController = mMapView.getController();
   /** 设置地图是否响应点击事件 . */
   mMapController.enableClick(true);
   /** 设置地图缩放级别 */
   mMapController.setZoom(12);
   /** 设置地图俯角 */
   mMapController.setOverlooking(-30);
   /**
    * 将地图移动至天安门 使用百度经纬度坐标,可以通过http://api.map.baidu.com/lbsapi/getpoint/index.html查询地理坐标
    * 如果需要在百度地图上显示使用其他坐标系统的位置,请发邮件至[email protected]申请坐标转换接口
    */
   double cLat = 39.945;
   double cLon = 116.404;
   GeoPoint p = new GeoPoint((int) (cLat * 1E6), (int) (cLon * 1E6));
   mMapController.setCenter(p);
 }
Ejemplo n.º 5
0
 protected void initMapController(MapView mapView) {
   mMapController = mapView.getController();
   mMapController.enableClick(true);
   mMapController.setZoom(ZOOM_LEVEL);
   mapView.setBuiltInZoomControls(true);
   mapView.showScaleControl(true);
 }
Ejemplo n.º 6
0
  @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);
    }
  }
Ejemplo n.º 7
0
  /**
   * 节点浏览示例
   *
   * @param v
   */
  public void nodeClick(View v) {
    viewCache = getLayoutInflater().inflate(R.layout.custom_text_view, null);
    popupText = (TextView) viewCache.findViewById(R.id.textcache);
    if (searchType == 0 || searchType == 2) {
      // 驾车、步行使用的数据结构相同,因此类型为驾车或步行,节点浏览方法相同
      if (nodeIndex < -1 || route == null || nodeIndex >= route.getNumSteps()) return;

      // 上一个节点
      if (mBtnPre.equals(v) && nodeIndex > 0) {
        // 索引减
        nodeIndex--;
        // 移动到指定索引的坐标
        mMapView.getController().animateTo(route.getStep(nodeIndex).getPoint());
        // 弹出泡泡
        popupText.setBackgroundResource(R.drawable.popup);
        popupText.setText(route.getStep(nodeIndex).getContent());
        pop.showPopup(
            BMapUtil.getBitmapFromView(popupText), route.getStep(nodeIndex).getPoint(), 5);
      }
      // 下一个节点
      if (mBtnNext.equals(v) && nodeIndex < (route.getNumSteps() - 1)) {
        // 索引加
        nodeIndex++;
        // 移动到指定索引的坐标
        mMapView.getController().animateTo(route.getStep(nodeIndex).getPoint());
        // 弹出泡泡
        popupText.setBackgroundResource(R.drawable.popup);
        popupText.setText(route.getStep(nodeIndex).getContent());
        pop.showPopup(
            BMapUtil.getBitmapFromView(popupText), route.getStep(nodeIndex).getPoint(), 5);
      }
    }
    if (searchType == 1) {
      // 公交换乘使用的数据结构与其他不同,因此单独处理节点浏览
      if (nodeIndex < -1
          || transitOverlay == null
          || nodeIndex >= transitOverlay.getAllItem().size()) return;

      // 上一个节点
      if (mBtnPre.equals(v) && nodeIndex > 1) {
        // 索引减
        nodeIndex--;
        // 移动到指定索引的坐标
        mMapView.getController().animateTo(transitOverlay.getItem(nodeIndex).getPoint());
        // 弹出泡泡
        popupText.setBackgroundResource(R.drawable.popup);
        popupText.setText(transitOverlay.getItem(nodeIndex).getTitle());
        pop.showPopup(
            BMapUtil.getBitmapFromView(popupText), transitOverlay.getItem(nodeIndex).getPoint(), 5);
      }
      // 下一个节点
      if (mBtnNext.equals(v) && nodeIndex < (transitOverlay.getAllItem().size() - 2)) {
        // 索引加
        nodeIndex++;
        // 移动到指定索引的坐标
        mMapView.getController().animateTo(transitOverlay.getItem(nodeIndex).getPoint());
        // 弹出泡泡
        popupText.setBackgroundResource(R.drawable.popup);
        popupText.setText(transitOverlay.getItem(nodeIndex).getTitle());
        pop.showPopup(
            BMapUtil.getBitmapFromView(popupText), transitOverlay.getItem(nodeIndex).getPoint(), 5);
      }
    }
  }
Ejemplo n.º 8
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();
  }
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    /**
     * 使用地图sdk前需先初始化BMapManager. BMapManager是全局的,可为多个MapView共用,它需要地图模块创建前创建,
     * 并在地图地图模块销毁后销毁,只要还有地图模块在使用,BMapManager就不应该销毁
     */
    DemoApplication app = (DemoApplication) this.getApplication();
    if (app.mBMapManager == null) {
      app.mBMapManager = new BMapManager(getApplicationContext());
      /** 如果BMapManager没有初始化则初始化BMapManager */
      app.mBMapManager.init(DemoApplication.strKey, new DemoApplication.MyGeneralListener());
    }
    setContentView(R.layout.activity_share_demo_activity);
    mMapView = (MapView) findViewById(R.id.bmapView);
    mMapView.getController().enableClick(true);
    mMapView.getController().setZoom(12);

    // 初始化搜索模块,注册搜索事件监听
    mSearch = new MKSearch();
    mSearch.init(
        app.mBMapManager,
        new MKSearchListener() {

          @Override
          public void onGetPoiDetailSearchResult(int type, int error) {}
          /** 在此处理poi搜索结果 , 用poioverlay 显示 */
          public void onGetPoiResult(MKPoiResult res, int type, int error) {
            // 错误号可参考MKEvent中的定义
            if (error != 0 || res == null) {
              Toast.makeText(ShareDemoActivity.this, "抱歉,未找到结果", Toast.LENGTH_LONG).show();
              return;
            }
            // 将地图移动到第一个POI中心点
            if (res.getCurrentNumPois() > 0) {
              // 将poi结果显示到地图上
              PoiShareOverlay poiOverlay = new PoiShareOverlay(ShareDemoActivity.this, mMapView);
              poiOverlay.setData(res.getAllPoi());
              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;
                }
              }
            }
          }

          public void onGetDrivingRouteResult(MKDrivingRouteResult res, int error) {}

          public void onGetTransitRouteResult(MKTransitRouteResult res, int error) {}

          public void onGetWalkingRouteResult(MKWalkingRouteResult res, int error) {}
          /** 在此处理反地理编结果 */
          public void onGetAddrResult(MKAddrInfo res, int error) {
            // 错误号可参考MKEvent中的定义
            if (error != 0 || res == null) {
              Toast.makeText(ShareDemoActivity.this, "抱歉,未找到结果", Toast.LENGTH_LONG).show();
              return;
            }
            AddrShareOverlay addrOverlay =
                new AddrShareOverlay(
                    getResources().getDrawable(R.drawable.icon_marka), mMapView, res);
            mMapView.getOverlays().clear();
            mMapView.getOverlays().add(addrOverlay);
            mMapView.refresh();
          }

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

          @Override
          public void onGetSuggestionResult(MKSuggestionResult res, int arg1) {}

          @Override
          public void onGetShareUrlResult(MKShareUrlResult result, int type, int error) {
            // 分享短串结果
            Intent it = new Intent(Intent.ACTION_SEND);
            it.putExtra(
                Intent.EXTRA_TEXT, "您的朋友通过百度地图SDK与您分享一个位置: " + currentAddr + " -- " + result.url);
            it.setType("text/plain");
            startActivity(Intent.createChooser(it, "将短串分享到"));
          }
        });
  }
Ejemplo n.º 10
0
 private void initMapView() {
   mMapView.setLongClickable(true);
   mMapView.getController().setZoom(14);
   mMapView.getController().enableClick(true);
   mMapView.setBuiltInZoomControls(true);
 }
Ejemplo n.º 11
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);
  }
Ejemplo n.º 12
0
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    DemoApplication app = (DemoApplication) this.getApplication();

    setContentView(R.layout.buslinesearch);
    CharSequence titleLable = "公交线路查询功能";
    setTitle(titleLable);
    // 地图初始化
    mMapView = (MapView) findViewById(R.id.bmapView);
    mMapView.getController().enableClick(true);
    mMapView.getController().setZoom(12);
    busLineIDList = new ArrayList<String>();
    // 创建 弹出泡泡图层
    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.init(
        app.mBMapManager,
        new MKSearchListener() {

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

          public void onGetPoiResult(MKPoiResult res, int type, int error) {
            // 错误号可参考MKEvent中的定义
            if (error != 0 || res == null) {
              Toast.makeText(BusLineSearchDemo.this, "抱歉,未找到结果", Toast.LENGTH_LONG).show();
              return;
            }

            // 找到公交路线poi node
            MKPoiInfo curPoi = null;
            int totalPoiNum = res.getCurrentNumPois();
            // 遍历所有poi,找到类型为公交线路的poi
            busLineIDList.clear();
            for (int idx = 0; idx < totalPoiNum; idx++) {
              if (2 == res.getPoi(idx).ePoiType) {
                // poi类型,0:普通点,1:公交站,2:公交线路,3:地铁站,4:地铁线路
                curPoi = res.getPoi(idx);
                // 使用poi的uid发起公交详情检索
                busLineIDList.add(curPoi.uid);
                System.out.println(curPoi.uid);
              }
            }
            SearchNextBusline();

            // 没有找到公交信息
            if (curPoi == null) {
              Toast.makeText(BusLineSearchDemo.this, "抱歉,未找到结果", Toast.LENGTH_LONG).show();
              return;
            }
            route = null;
          }

          public void onGetDrivingRouteResult(MKDrivingRouteResult res, int error) {}

          public void onGetTransitRouteResult(MKTransitRouteResult res, int error) {}

          public void onGetWalkingRouteResult(MKWalkingRouteResult res, int error) {}

          public void onGetAddrResult(MKAddrInfo res, int error) {}
          /** 获取公交路线结果,展示公交线路 */
          public void onGetBusDetailResult(MKBusLineResult result, int iError) {
            if (iError != 0 || result == null) {
              Toast.makeText(BusLineSearchDemo.this, "抱歉,未找到结果", Toast.LENGTH_LONG).show();
              return;
            }

            RouteOverlay routeOverlay = new RouteOverlay(BusLineSearchDemo.this, mMapView);
            // 此处仅展示一个方案作为示例
            routeOverlay.setData(result.getBusRoute());
            // 清除其他图层
            mMapView.getOverlays().clear();
            // 添加路线图层
            mMapView.getOverlays().add(routeOverlay);
            // 刷新地图使生效
            mMapView.refresh();
            // 移动地图到起点
            mMapView.getController().animateTo(result.getBusRoute().getStart());
            // 将路线数据保存给全局变量
            route = result.getBusRoute();
            // 重置路线节点索引,节点浏览时使用
            nodeIndex = -1;
            mBtnPre.setVisibility(View.VISIBLE);
            mBtnNext.setVisibility(View.VISIBLE);
            Toast.makeText(BusLineSearchDemo.this, result.getBusName(), Toast.LENGTH_SHORT).show();
          }

          @Override
          public void onGetSuggestionResult(MKSuggestionResult res, int arg1) {}

          @Override
          public void onGetShareUrlResult(MKShareUrlResult result, int type, int error) {
            // TODO Auto-generated method stub

          }
        });

    // 设定搜索按钮的响应
    mBtnSearch = (Button) findViewById(R.id.search);
    mBtnNextLine = (Button) findViewById(R.id.nextline);
    mBtnPre = (Button) findViewById(R.id.pre);
    mBtnNext = (Button) findViewById(R.id.next);
    mBtnPre.setVisibility(View.INVISIBLE);
    mBtnNext.setVisibility(View.INVISIBLE);

    OnClickListener clickListener =
        new OnClickListener() {
          public void onClick(View v) {
            // 发起搜索
            SearchButtonProcess(v);
          }
        };
    OnClickListener nextLineClickListener =
        new OnClickListener() {
          public void onClick(View v) {
            // 搜索下一条公交线
            SearchNextBusline();
          }
        };
    OnClickListener nodeClickListener =
        new OnClickListener() {
          public void onClick(View v) {
            // 浏览路线节点
            nodeClick(v);
          }
        };
    mBtnSearch.setOnClickListener(clickListener);
    mBtnNextLine.setOnClickListener(nextLineClickListener);
    mBtnPre.setOnClickListener(nodeClickListener);
    mBtnNext.setOnClickListener(nodeClickListener);
  }