Android谷歌地图,折线和缩放

6

我正在在 Google 地图上绘制折线列表。 我正在寻找一种解决方案,根据绘制的折线来适应地图的缩放。 我已经计算出所有折线的中心点,因此我知道在哪个点上将地图居中。 但是,我找不到任何解决方案来获取缩放级别。

这里是我使用的代码:

    mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(getZoomPoint(), 15));

private LatLng getZoomPoint() {
    Double minLatitude = null;
    Double minLongitude = null;
    Double maxLatitude = null;
    Double maxLongitude = null;

    for (Feature feature : features) {
        List<Coordinates> coordinates = ((LineString) feature.getGeometry()).getCoordinates();
        for (Coordinates coordinate : coordinates) {
            // --------------------------------------------------------- INITIALISATION
            if (minLatitude == null) { // No matter on wich var we check
                minLatitude = coordinate.getLatitude();
                minLongitude = coordinate.getLongitude();
                maxLatitude = coordinate.getLatitude();
                maxLongitude = coordinate.getLongitude();
            } else {
                if (coordinate.getLatitude() < minLatitude) {
                    minLatitude = coordinate.getLatitude();
                }
                if (coordinate.getLatitude() > maxLatitude) {
                    maxLatitude = coordinate.getLatitude();
                }
                if (coordinate.getLongitude() < minLongitude) {
                    minLongitude = coordinate.getLongitude();
                }
                if (coordinate.getLongitude() > maxLongitude) {
                    maxLongitude = coordinate.getLongitude();
                }
            }
        }
    }
    double meanLatitude = (minLatitude + maxLatitude) / 2;
    double meanLongitude = (minLongitude + maxLongitude) / 2;
    return new LatLng(meanLatitude, meanLongitude);
}

我的第一个问题是:有没有一种方法来计算缩放级别的值?(这里硬编码为“15”)。

我的第二个问题是:如何根据相机缩放级别调整折线宽度?我已经添加了一个监听器:

mMap.setOnCameraChangeListener(new GoogleMap.OnCameraChangeListener() {
            @Override
            public void onCameraChange(CameraPosition cameraPosition) {
                Log.d("ZOOM = ", "" +mMap.getCameraPosition().zoom);

        });

我可以使用 setWidth(...) 方法更改折线的宽度,但是我找不到计算宽度值的“公式”。现在,折线是固定的,不受缩放级别的影响。有什么建议吗?
2个回答

26

您可以使用LatLngBounds来创建一个固定范围的焦点。

    /**Latlng's to get focus*/
    LatLng Delhi = new LatLng(28.61, 77.2099);
    LatLng Chandigarh = new LatLng(30.75, 76.78);
    LatLng SriLanka = new LatLng(7.000, 81.0000);
    LatLng America = new LatLng(38.8833, 77.0167);
    LatLng Arab = new LatLng(24.000, 45.000);

    /**create for loop/manual to add LatLng's to the LatLngBounds.Builder*/
    LatLngBounds.Builder builder = new LatLngBounds.Builder();
    builder.include(Delhi);
    builder.include(Chandigarh);
    builder.include(SriLanka);
    builder.include(America);
    builder.include(Arab);

    /**initialize the padding for map boundary*/
    int padding = 50;
    /**create the bounds from latlngBuilder to set into map camera*/
    LatLngBounds bounds = builder.build();
    /**create the camera with bounds and padding to set into map*/
    final CameraUpdate cu = CameraUpdateFactory.newLatLngBounds(bounds, padding);
    /**call the map call back to know map is loaded or not*/
    map.setOnMapLoadedCallback(new GoogleMap.OnMapLoadedCallback() {
        @Override
        public void onMapLoaded() {
            /**set animated zoom camera into map*/
            map.animateCamera(cu);
        }
    });

就像上面的代码一样,假设你有一个坐标列表List<Coordinates>,因为我在代码中手动添加了LatLng,请将这些坐标的LatLng对象添加到LatLngBounds.Builder中,然后对相机进行动画处理,它会自动缩放以覆盖所有的LatLng。


太棒了,我以为这会是一项艰巨的工作,但我有一个问题,这仅适用于近距离路线,源和目标标记可见,但路线折线超出视图范围,有什么想法吗? - Ramkesh Yadav
@RamkeshYadav 假设您已经构建好了 Builder。您可以继续执行 points.forEach { point -> builder.include(point) } - joninx

0
mMap = googleMap;
    Log.d("mylog", "Added Markers");
    mMap.addMarker(place1);
    mMap.addMarker(place2);
    int padding = 50;
    /**create the bounds from latlngBuilder to set into map camera*/
    LatLngBounds.Builder builder = new LatLngBounds.Builder();
    builder.include(new LatLng(28.429730, 77.055400));
    builder.include(new LatLng(28.403000, 77.318800));
    LatLngBounds bounds = builder.build();
    /**create the camera with bounds and padding to set into map*/
    final CameraUpdate cu = CameraUpdateFactory.newLatLngBounds(bounds, padding);
    /**call the map call back to know map is loaded or not*/
    mMap.setOnMapLoadedCallback(new GoogleMap.OnMapLoadedCallback() {
        @Override
        public void onMapLoaded() {
            /**set animated zoom camera into map*/
            mMap.animateCamera(cu);
        }
    });

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接