繁体   English   中英

Android谷歌地图,折线和缩放

[英]Android Google Map, Polylines and zoom

我正在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(...)方法更改折线宽度,但我找不到计算宽度值的“公式”。 现在,修复的折线不依赖于缩放级别。

有什么建议么 ?

您可以使用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。

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);
        }
    });

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM