0

I am trying to make it so that when I launch my app the phone will zoom in and center to the area in which I have set PolyLines to via an ArrayList of lat and lang points.

    @Override
public void onMapReady(GoogleMap googleMap) {
    mMap = googleMap;
    ArrayList<MyPoint> points = Data.getPoints();
    PolylineOptions polylineOptions = new PolylineOptions();
    for (int i = 0; i < points.size(); i++) {
        MyPoint point = points.get(i);
        polylineOptions.add(new LatLng(point.getLat(), point.getLng()));
    }
    Polyline polyline = mMap.addPolyline(polylineOptions);
}

I start up the app in genymotion and it zooms in and then centers at the lat/lang ArrayList points I've set. Currently, I just get a map that shows nearly the whole globe and I have to zoom in manually to the polylines I've set up.

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
brff19
  • 760
  • 1
  • 8
  • 21

2 Answers2

4

You need the camera update factory class to create a camera update and move the map's camera. Camera Update Factory Documentation

@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
ArrayList<MyPoint> points = Data.getPoints();

int padding = 20; // or prefer padding

LatLngBounds.Builder builder = new LatLngBounds.Builder();

PolylineOptions polylineOptions = new PolylineOptions();

for (int i = 0; i < points.size(); i++) {
    MyPoint point = points.get(i);
    polylineOptions.add(new LatLng(point.getLat(), point.getLng()));
    builder.include(new LatLng(point.getLat(), point.getLng()));
}

Polyline polyline = mMap.addPolyline(polylineOptions);
LatLngBounds bounds = builder.build();
map.animateCamera(CameraUpdateFactory.newLatLngBounds(bounds, padding));


}
Demilade
  • 513
  • 2
  • 7
3

You need to use LatLngBounds and then zoom your camera to cover all the included markers:

LatLngBounds.Builder builder = new LatLngBounds.Builder();

Use the following line in a loop for all points/markers: (You can use the same loop you are using to add points to polylineOptions.)

builder.include(pointLatLng);

And finally, (outside the loop) -

LatLngBounds bounds = builder.build();
map.animateCamera(CameraUpdateFactory.newLatLngBounds(bounds, 20));
Supriya
  • 1,940
  • 24
  • 23
  • I have tried your code but I get thrown some errors. Hope you can clear it for me and I would be happy to give you the upvote! Here is a link to what I tried with what you supplied me with (https://gist.github.com/BrianARuff/b4e42d1f6319b8f1435b52bfab310453), thanks. – brff19 Dec 09 '17 at 22:06
  • I believe the exception being thrown might be due to animateCamera being called inside onMapReady. Look at https://stackoverflow.com/questions/13692579/movecamera-with-cameraupdatefactory-newlatlngbounds-crashes , let me know if it helps. – Supriya Dec 09 '17 at 22:16
  • 1
    thanks so much. I was able to figure it out, and I ran it with the following code (https://gist.github.com/BrianARuff/77af0dcc060ff339b7ef9e79c49a1acf)! – brff19 Dec 09 '17 at 22:36