Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to zoom camera to cover the path in android googlemap?

I have covered multiple locations in Google map using LatLng.Builder and it's working. Is there any way I could cover the whole path between two location in Google map?

My Curent Code to include multiple locations

builder = new LatLngBounds.Builder();
builder.include(LatLng1);
builder.include(LatLng2);
googleMap.animateCamera(CameraUpdateFactory.newLatLngBounds(builder.build(), 17));

Any suggestion please?
Thanks

like image 887
Michael Shrestha Avatar asked Aug 26 '14 06:08

Michael Shrestha


1 Answers

I know you already find the answer, but here is my method to solve the problem

boolean hasPoints = false;
        Double maxLat = null, minLat = null, minLon = null, maxLon = null;

        if (polyline != null && polyline.getPoints() != null) {
            List<LatLng> pts = polyline.getPoints();
            for (LatLng coordinate : pts) {
                // Find out the maximum and minimum latitudes & longitudes
                // Latitude
                maxLat = maxLat != null ? Math.max(coordinate.latitude, maxLat) : coordinate.latitude;
                minLat = minLat != null ? Math.min(coordinate.latitude, minLat) : coordinate.latitude;

                // Longitude
                maxLon = maxLon != null ? Math.max(coordinate.longitude, maxLon) : coordinate.longitude;
                minLon = minLon != null ? Math.min(coordinate.longitude, minLon) : coordinate.longitude;

                hasPoints = true;
            }
        }

        if (hasPoints) {
            LatLngBounds.Builder builder = new LatLngBounds.Builder();
            builder.include(new LatLng(maxLat, maxLon));
            builder.include(new LatLng(minLat, minLon));
            map.moveCamera(CameraUpdateFactory.newLatLngBounds(builder.build(), 48));
        }
like image 85
JCDecary Avatar answered Sep 26 '22 16:09

JCDecary