有 Java 编程相关的问题?

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

java如何在谷歌地图上选择正确的缩放

我开发了一个应用程序,它使用带有多个标记的GMAP。用户可以添加或删除标记,但我的问题是选择良好的缩放级别以显示所有标记

如果用户添加或删除标记,缩放级别可能会改变

为了使摄像机居中,我使用以下代码:

double xMin = 90.0 ;
double xMax =  0.0 ;
double yMin = 90.0 ;
double yMax =  0.0 ;

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

for (Place place : places) {
                if (place.getLatitude() < xMin)
                    xMin = place.getLatitude();

                if (place.getLatitude() > xMax)
                    xMax = place.getLatitude();

                if (place.getLongitude() < yMin)
                    yMin = place.getLongitude();

                if (place.getLongitude() > yMax)
                    yMax = place.getLongitude();

                MarkerOptions markerOptions = new MarkerOptions();
                markerOptions.position(new LatLng(place.getLatitude(), place.getLongitude()));
                markerOptions.title(String.valueOf(place.getId()));
                Marker marker = this.googleMap.addMarker(markerOptions);
                allMarkersMap.put(marker, place);
                displayMarkersMap.put(marker, place);
                builder.include(marker.getPosition());
            }

            double xPos = (xMax + xMin) / 2;
            double yPos = (yMax + yMin) / 2;

            LatLngBounds bounds = builder.build();
            int padding = 1; // offset from edges of the map in pixels
            //CameraUpdate cameraUpdate = CameraUpdateFactory.newLatLngBounds(bounds, padding);
            //this.googleMap.moveCamera(cameraUpdate);
            LatLng latLng = new LatLng(xPos, yPos);
            CameraPosition cameraPosition = new CameraPosition.Builder().target(latLng).zoom(2).build();
            this.googleMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));

但我找不到一个有好的缩放值的解决方案


共 (1) 个答案

  1. # 1 楼答案

    我知道您需要为地图定义一个中心,并确保所有标记都在视图中

    在这种情况下,您可以计算包含标记的LatLngBounds,然后使用SphericalUtil.computeOffsetSphericalUtil.computeDistanceBetweenSphericalUtil.computeHeading方法从Google Maps API Utility Library扩展它

    private LatLngBounds findBounds(List<LatLng> positions, LatLng center) {
        // Add all the positions to a LatLngBounds (including center)
        LatLngBounds.Builder boundsBuilder = new LatLngBounds.Builder();
        for (LatLng position : positions) {
            boundsBuilder.include(position);
        }
        boundsBuilder.include(center);
        LatLngBounds bounds = boundsBuilder.build();
    
        // Compute the farthest location from the center among the bound corners
        LatLng northWest = new LatLng(bounds.northeast.latitude, bounds.southwest.longitude);
        LatLng southEast = new LatLng(bounds.southwest.latitude, bounds.northeast.longitude);
        LatLng farthest = findFarthestLocation(center, bounds.northeast, northWest, southEast, bounds.southwest);
    
        // Expand the bounds adding the projection of the farthest location from the center
        // in the oposite direction
        bounds = bounds.including(SphericalUtil.computeOffset(center,
                SphericalUtil.computeDistanceBetween(center, farthest),
                SphericalUtil.computeHeading(center, farthest) + 180));
    
        return bounds;
    }
    
    private LatLng findFarthestLocation(LatLng from, LatLng... locations) {
        LatLng farthest = null;
    
        for (LatLng location : locations) {
            if (farthest == null ||
                    SphericalUtil.computeDistanceBetween(from, location) > SphericalUtil.computeDistanceBetween(from, farthest)) {
                farthest = location;
            }
        }
    
        return farthest;
    }
    

    你可以测试这个方法

    LatLng center = new LatLng(40.384213, -3.875244);
    List<LatLng> positions = new ArrayList<>();
    positions.add(new LatLng(43.153102, 2.914307));
    positions.add(new LatLng(42.976521, 1.508057));
    positions.add(new LatLng(42.492353, 0.123779));
    
    Display display = getActivity().getWindowManager().getDefaultDisplay();
                    Point size = new Point();
                    display.getSize(size);
                    int width  = size.x;
                    int height = size.y;
    
    LatLngBounds bounds = findBounds(positions,center);
    mMap.animateCamera(CameraUpdateFactory.newLatLngBounds(bounds,width,height,10));