百度地图的使用

  1. 在地图中添加Maker
private void addEndMaker() {
    MapView bMapView;
    LatLng endLatLng;
    BaiduMap mBaiduMap;//百度地图
    mBaiduMap = bMapView.getMap();
    //构建Marker图标
    BitmapDescriptor bitmap = BitmapDescriptorFactory
            .fromResource(R.mipmap.icon_marka);
    //构建MarkerOption,用于在地图上添加Marker
    MarkerOptions option = new MarkerOptions()
            .position(endLatLng)
            .icon(bitmap).title("导航");
    //在地图上添加Marker,并显示
    mBaiduMap.addOverlay(option);
    mBaiduMap.setOnMarkerClickListener(marker -> {
        Button button = new Button(_activity);
        button.setBackgroundResource(R.drawable.rect_transparent_black_with_gray_stroke);
        button.setTextColor(getResources().getColorStateList(R.color.color_defaultTextLighter));
        button.setTextSize(18);
        button.setText(marker.getTitle());
        button.setOnClickListener(v -> {
            mBaiduMap.hideInfoWindow();
            try {
                if (null == currentLatLng) {
                    return;
                }
                // 构建 导航参数
                NaviParaOption para = new NaviParaOption()
                        .startPoint(currentLatLng).endPoint(endLatLng);
                BaiduMapNavigation.openBaiduMapNavi(para, this);
            } catch (BaiduMapAppNotSupportNaviException e) {
                e.printStackTrace();
            } catch (Exception e) {
                e.printStackTrace();
            }
        });
        LatLng markerLatlng = marker.getPosition();
        InfoWindow mInfoWindow = new InfoWindow(button, markerLatlng, -70);
        mBaiduMap.showInfoWindow(mInfoWindow);
        return true;
    });
    mBaiduMap.setOnMapClickListener(new BaiduMap.OnMapClickListener() {
        @Override
        public void onMapClick(LatLng latLng) {
            mBaiduMap.hideInfoWindow();
        }
        @Override
        public boolean onMapPoiClick(MapPoi mapPoi) {
            return false;
        }
    });
}
  1. 在界面上同时显示若干位置;
private void zoom2FitVIew() {
    MapView bMapView = null;
    BaiduMap mBaiduMap = bMapView.getMap();
    LatLng p1 = null;
    LatLng p2 = null;
    mBaiduMap.setOnMapLoadedCallback(() -> {
        LatLngBounds llb = new LatLngBounds.Builder().include(p1).include(p2).build();
        mBaiduMap.animateMapStatus(MapStatusUpdateFactory.newLatLngBounds(llb));
    });
}

你可能感兴趣的:(百度地图的使用)