zoom over specific route google map

前端 未结 9 1075
感动是毒
感动是毒 2021-02-05 11:23

I have a list of random latitude and longitude points and I am drawing a route between them. My question is how to bound this route within google map I made below utili

9条回答
  •  抹茶落季
    2021-02-05 11:53

    I implemented a similar solution to @rishabh-dutt-sharma with a couple of twists:

    • First of all, before zooming check if the points are already inside the view. If they are, do not change it. This means less unneeded changes in the UI for the user.
    • Second, a special case is when there is just one point. This is useful when showing a list of markers, and maybe is not needed in your particular case. The reason of why is this useful is because in the case of single point, the zoom usually is increased to the maximum, and usually this is too much.

    This is the code (adapted from my original code, I hope there are no typos):

        public void zoomRoute(GoogleMap googleMap, List lstLatLngRoute) {
    
            if (googleMap == null || lstLatLngRoute == null || lstLatLngRoute.isEmpty()) return;
    
            LatLngBounds currentLatLongBounds =
                    googleMap.getProjection().getVisibleRegion().latLngBounds;
            boolean updateBounds = false;
    
            for (LatLng latLng : lstLatLngRoute) {
                if (!currentLatLongBounds.contains(latLng)) {
                    updateBounds = true;
                }
            }
    
            if (updateBounds) {
    
                CameraUpdate cameraUpdate;
    
                if (lstLatLngRoute.size() == 1) {
    
                    LatLng latLng = lstLatLngRoute.iterator().next();
                    cameraUpdate = CameraUpdateFactory.newLatLng(latLng);
    
                } else {
    
                    LatLngBounds.Builder builder = LatLngBounds.builder();
                    for (LatLng latLng : lstLatLngRoute) {
                        builder.include(latLng);
                    }
                    LatLngBounds latLongBounds = builder.build();
    
                    cameraUpdate =
                            CameraUpdateFactory.newLatLngBounds(latLongBounds, MAP_ZOOM_PADDING);
    
                }
    
                try {
                    googleMap.animateCamera(cameraUpdate, MAP_CAMERA_ANIMATION_DURATION_IN_MILLIS,
                            new GoogleMap.CancelableCallback() {
                                @Override
                                public void onFinish() {
                                }
    
                                @Override
                                public void onCancel() {
                                }
                            });
                } catch (IllegalStateException ex) {
                    // Ignore it. We're just being a bit lazy, as this exception only happens if
                    // we try to animate the camera before the map has a size
                }
            }
        }
    

提交回复
热议问题