@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); }
protected void initMapController(MapView mapView) { mMapController = mapView.getController(); mMapController.enableClick(true); mMapController.setZoom(ZOOM_LEVEL); mapView.setBuiltInZoomControls(true); mapView.showScaleControl(true); }
/** * 设置指南针位置,指南针在3D模式下自动显现 * * @param view */ public void setCompassLocation(View view) { boolean checked = ((RadioButton) view).isChecked(); switch (view.getId()) { case R.id.lefttop: if (checked) // 设置指南针显示在左上角 mMapController.setCompassMargin(100, 100); break; case R.id.righttop: if (checked) mMapController.setCompassMargin(mMapView.getWidth() - 100, 100); break; } }
private void showMap(double latitude, double longtitude, String address) { sendButton.setVisibility(View.GONE); GeoPoint point1 = new GeoPoint((int) (latitude * 1e6), (int) (longtitude * 1e6)); point1 = CoordinateConvert.fromGcjToBaidu(point1); mMapController.setCenter(point1); Drawable marker = this.getResources().getDrawable(R.drawable.icon_marka); // 为maker定义位置和边界 marker.setBounds(0, 0, marker.getIntrinsicWidth(), marker.getIntrinsicHeight()); mAddrOverlay = new ItemizedOverlay<OverlayItem>(marker, mMapView); GeoPoint point = new GeoPoint((int) (latitude * 1e6), (int) (longtitude * 1e6)); point = CoordinateConvert.fromGcjToBaidu(point); OverlayItem addrItem = new OverlayItem(point, "", address); mAddrOverlay.addItem(addrItem); mMapView.getOverlays().add(mAddrOverlay); mMapView.refresh(); }
@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 setOverlookEnable(View v) { mMapController.setOverlookingGesturesEnabled(((CheckBox) v).isChecked()); }
/** * 是否启用旋转手势 * * @param v */ public void setRotateEnable(View v) { mMapController.setRotationGesturesEnabled(((CheckBox) v).isChecked()); }
/** * 是否启用平移手势 * * @param v */ public void setScrollEnable(View v) { mMapController.setScrollGesturesEnabled(((CheckBox) v).isChecked()); }
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(); }