欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

百度地图相关

程序员文章站 2022-06-10 18:32:13
...

更换地图主题

将设置好的主题样式放入assets中

代码中(在setContentView(R.layout.activity_main)之前调用)

    private void initBaiduMap(){
        SDKInitializer.initialize(this);
        new Thread(new Runnable() {
            @Override
            public void run() {
                try
                {
                    File mapStyleFile = new File(getFilesDir().getAbsolutePath() + "/custom_config_dark.txt");//主题样式的 
                    if(!mapStyleFile.exists()) {
                        InputStream is = getAssets().open("customConfigdir/custom_config_dark.txt");
                        FileOutputStream fos = new FileOutputStream(mapStyleFile);
                        byte[] buffer = new byte[1024];
                        int byteCount = 0;
                        while ((byteCount = is.read(buffer)) != -1) {//循环从输入流读取 buffer字节
                            fos.write(buffer, 0, byteCount);//将读取的输入流写入到输出流
                        }
                        fos.flush();//刷新缓冲区
                        is.close();
                        fos.close();
                    }
                    TextureMapView.setCustomMapStylePath(mapStyleFile.getAbsolutePath());
                    TextureMapView.setMapCustomEnable(true);
                } catch (Exception e){
                    e.printStackTrace();
                }
            }
        }).start();
    }

最后在需要出现这个地图主题的ativity中

  map.setMapCustomEnable(true); //设置个性化地图样式是否生效

去除logo

        View child = map.getChildAt(1);
        if (child != null && (child instanceof ImageView || child instanceof ZoomControls))
        {
            child.setVisibility(View.INVISIBLE);
        }


多个market的点击事件

循环设置market

  private void setMarket(latitude,longitude)
  {
       LatLng point = new LatLng(latitude, longitude);
        //定义Maker坐标点
//        //构建Marker图标
        BitmapDescriptor bitmap = BitmapDescriptorFactory.fromResource(R.mipmap.ic_empoison_commonly);
        //构建MarkerOption,用于在地图上添加Marker
        OverlayOptions options = new MarkerOptions()
                .position(point)  //设置marker的位置
                .icon(bitmap)  //设置marker图标
//                .zIndex(2)  //设置marker所在层级
                .draggable(false);  //设置手势拖拽
        //在地图上添加Marker,并显示
        Overlay marker = mBaiduMap.addOverlay(options);
        Bundle bundle = new Bundle();
        bundle.putSerializable("point", point);
        marker.setExtraInfo(bundle);
    }

    private void setCircle(latitude,longitude)
    {
        LatLng point = new LatLng(latitude,longitude);
        int circleColor = 0;
        switch (dataMap.type)
        {
            case 1:
                circleColor = getResources().getColor(R.color.item_good);
                break;
            case 2:
                circleColor = getResources().getColor(R.color.item_opacitas);
                break;
            case 3:
                circleColor = getResources().getColor(R.color.item_alga);
                break;
            case 4:
                circleColor = getResources().getColor(R.color.item_element);
                break;
        }


        OverlayOptions ooCircle = new CircleOptions()
                .fillColor(circleColor)
                .center(point)
//                .stroke(new Stroke(5, circleColor))
                .radius(1400);
        mBaiduMap.addOverlay(ooCircle);
    }

onCreate中设置mBaiduMap的监听

  mBaiduMap.setOnMarkerClickListener(new BaiduMap.OnMarkerClickListener()
        {
            @Override
            public boolean onMarkerClick(Marker marker)
            {
                DataMap dataMap = (DataMap) marker.getExtraInfo().get("point");
               
                return true;
            }
        });

百度地图在listview/recyclerview的滑动冲突
  mBaiduMap.setOnMapStatusChangeListener(new BaiduMap.OnMapStatusChangeListener()
        {
            LatLng startLng, finishLng;

            @Override
            public void onMapStatusChangeStart(MapStatus mapStatus)
            {
                startLng = mapStatus.target;
            }

            @Override
            public void onMapStatusChange(MapStatus mapStatus)
            {
            }

            @Override
            public void onMapStatusChangeFinish(MapStatus mapStatus)
            {
                // 滑动搜索
                finishLng = mapStatus.target;
                if (startLng.latitude != finishLng.latitude
                        || startLng.longitude != finishLng.longitude)
                {
                    Projection ject = mBaiduMap.getProjection();
                    Point startPoint = ject.toScreenLocation(startLng);
                    Point finishPoint = ject.toScreenLocation(finishLng);
                    double x = Math.abs(finishPoint.x - startPoint.x);
                    double y = Math.abs(finishPoint.y - startPoint.y);
                    if (x > 50 || y > 50)
                    {
                        //在这处理滑动
                        ll_detail.setVisibility(View.GONE);
                    }
                }
            }
        });