javaandroidgoogle-maps

Android Google Map, Polylines and zoom


I am drawing a list of polylines on a Google Map. I am looking for a solution to fit the zoom of the map depending of the drawn polylines. I've calculated the center point of all my polyline so I know on which point to center the map. But I don't find any solution to get the zoom level.

Here the code I use: 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 which 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);
}

My first question is: Is there a way to compute the zoom level value? (here is '15' hard-coded).

My second question is: How can I fit the polylines width depending on the camera zoom level? I've added a listener:

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

And I can change the polylines width using the setWidth(...) method but I don't find a "formula" to compute the width value. Now, the polylines which is fixed and doesn't depends on the zoom level.

Any suggestions?


Solution

  • You can use LatLngBounds to make a fix bound focus.

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

    As like above code you have a list of coordinates List<Coordinates> as I am adding manual LatLng in code. add these coordinates LatLng object to the LatLngBounds.Builder then animate the camera, it will automatically zoom for the all covered LatLng's.