How to zoom camera to cover the path in android googlemap?
Asked Answered
A

3

10

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

Atchley answered 26/8, 2014 at 6:43 Comment(3)
Calculate average of your two locations and animate your camera to that location.Denticle
is your code not shown whole path area ?Administrative
Thanks for your replies... I have solved it by adding every point in the route to the LAtLngBounds.Builer object and then moving the camera.Atchley
A
8

The solution was simple

This might help someone in the future if looking for this sort of feature.

This method is called after the google direction API returns the route from location A to B. directionPoints is the list of Points in the route.

public void handleResult(ArrayList<LatLng> directionPoints)
    {
        PolylineOptions rectLine = new PolylineOptions().width(3).color(Color.RED);
        for(int i = 0 ; i < directionPoints.size() ; i++)
        {
            rectLine.add(directionPoints.get(i));
        }

        //this polyline is stored so that it can be removed by calling drawnRoutePath.remove() if needed
        drawnRoutePath = googleMap.addPolyline(rectLine);
        prepareBuilder(directionPoints);
        googleMap.animateCamera(CameraUpdateFactory.newLatLngBounds(builder.build(), 17));
    }

UPDATE

The json response from the Google Direction API

"routes" : [
      {
         "bounds" : {
            "northeast" : {
               "lat" : 27.7136953,
               "lng" : 85.32216629999999
            },
            "southwest" : {
               "lat" : 27.7103725,
               "lng" : 85.3214952
            }
         },
.... etc

So I used this bound's lat and lng as parameter to LatLngBounds.Builder.

JSONObject jsonBound = ((JSONObject)jRoutes.get(i)).getJSONObject("bounds");
JSONObject jsonSouthWest = jsonBound.getJSONObject("southwest");
JSONObject jsonNorthEast = jsonBound.getJSONObject("northeast");
LatLng boundSouthWest = new LatLng(jsonSouthWest.getDouble("lat"),jsonSouthWest.getDouble("lng"));
LatLng boundNorthEast = new LatLng(jsonNorthEast.getDouble("lat"),jsonNorthEast.getDouble("lng"));
ArrayList<LatLng> bounds = new ArrayList<LatLng>();
bounds.add(boundNorthEast);
bounds.add(boundSouthWest);

and included these points in LatLngBounds.Builder

Atchley answered 26/8, 2014 at 7:2 Comment(1)
How do you get directionPointsBillhead
M
12

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));
        }
Mungo answered 18/2, 2016 at 20:13 Comment(0)
A
8

The solution was simple

This might help someone in the future if looking for this sort of feature.

This method is called after the google direction API returns the route from location A to B. directionPoints is the list of Points in the route.

public void handleResult(ArrayList<LatLng> directionPoints)
    {
        PolylineOptions rectLine = new PolylineOptions().width(3).color(Color.RED);
        for(int i = 0 ; i < directionPoints.size() ; i++)
        {
            rectLine.add(directionPoints.get(i));
        }

        //this polyline is stored so that it can be removed by calling drawnRoutePath.remove() if needed
        drawnRoutePath = googleMap.addPolyline(rectLine);
        prepareBuilder(directionPoints);
        googleMap.animateCamera(CameraUpdateFactory.newLatLngBounds(builder.build(), 17));
    }

UPDATE

The json response from the Google Direction API

"routes" : [
      {
         "bounds" : {
            "northeast" : {
               "lat" : 27.7136953,
               "lng" : 85.32216629999999
            },
            "southwest" : {
               "lat" : 27.7103725,
               "lng" : 85.3214952
            }
         },
.... etc

So I used this bound's lat and lng as parameter to LatLngBounds.Builder.

JSONObject jsonBound = ((JSONObject)jRoutes.get(i)).getJSONObject("bounds");
JSONObject jsonSouthWest = jsonBound.getJSONObject("southwest");
JSONObject jsonNorthEast = jsonBound.getJSONObject("northeast");
LatLng boundSouthWest = new LatLng(jsonSouthWest.getDouble("lat"),jsonSouthWest.getDouble("lng"));
LatLng boundNorthEast = new LatLng(jsonNorthEast.getDouble("lat"),jsonNorthEast.getDouble("lng"));
ArrayList<LatLng> bounds = new ArrayList<LatLng>();
bounds.add(boundNorthEast);
bounds.add(boundSouthWest);

and included these points in LatLngBounds.Builder

Atchley answered 26/8, 2014 at 7:2 Comment(1)
How do you get directionPointsBillhead
W
1

Here is simplest way

private GoogleMap mMap;
  // Create a LatLngBounds that includes Australia.
  private LatLngBounds AUSTRALIA = new LatLngBounds(
  new LatLng(-44, 113), new LatLng(-10, 154));

// Set the camera to the greatest possible zoom level that includes the
// bounds 
mMap.moveCamera(CameraUpdateFactory.newLatLngBounds(AUSTRALIA, 0));

https://developers.google.com/maps/documentation/android-sdk/views

Wobble answered 4/4, 2020 at 21:24 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.