有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java在RecyclerView中定位和缩放Google地图

我的应用程序的一部分使用带有CardView元素的RecyclerView,每个CardView元素都包含绘制特定路线的Google地图。在每一张地图上,我都在地图内部缩放和定位路线,这样它就可以填充整个视图,但当我开始滚动时,问题就出现了,并且每一张下一张CardView都不会将我的路线定位在地图内部,而是将其未缩放和未定位。图片如下:

First CardView zoom and position is ok but when scrolled down position of next CardView is not ok

我使用onBindViewHolder设置地图和路线图,然后使用zoomToLatLng缩放地图

    @Override
    public void onBindViewHolder(RouteViewHolder holder, int position) {
        holder.destination.setText(routesCV.get(position).getDestination());
        holder.startingPoint.setText(routesCV.get(position).getStartingPoint());

        GoogleMap gMap = holder.map.getMap();

        if (gMap != null) {

            ArrayList<PointModel> mapPosition = routesCV.get(position).getPoints();
            ArrayList<LatLng> points = new ArrayList<LatLng>();

            for (PointModel item : mapPosition) {
                points.add(new LatLng(item.getLatitude(),item.getLongitude()));
            }

            for (LatLng item : points) {
                gMap.addMarker(new MarkerOptions().position(item));
            }

            zoomMapToLatLngBounds(holder.linearlayout, gMap, points);
        }

    }

缩放方法:

private void zoomMapToLatLngBounds(final LinearLayout layout,final GoogleMap mMap, final ArrayList<LatLng> bounds){

    LatLngBounds.Builder boundsBuilder = new LatLngBounds.Builder();

    for (int i=0; i<bounds.size(); i++) {
        boundsBuilder.include(bounds.get(i));
    }

    final LatLngBounds boundsfinal = boundsBuilder.build();

    final ViewTreeObserver vto = layout.getViewTreeObserver();
    vto.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
        @SuppressWarnings("deprecation")
        @Override
        public void onGlobalLayout() {
            layout.getViewTreeObserver().removeGlobalOnLayoutListener(this);
            mMap.moveCamera(CameraUpdateFactory.newLatLngBounds(boundsfinal, 25));
        }
    });

从日志中,public void onGlobalLayout()甚至不执行。你知道有什么问题吗?Tnx


共 (1) 个答案

  1. # 1 楼答案

    我做了一个OnMapLoadedCallback()函数来定位地图。现在可以了