Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set PolyLine Fit to our Google Map Fragment

My polyline Going out of focus. I'm using map.moveCamera(CameraUpdateFactory.newLatLngZoom(route.startLocation, 10)); but this is not a solution. How to do slove this issue

enter image description here

like image 769
Darshan Khatri Avatar asked Jan 10 '17 07:01

Darshan Khatri


1 Answers

To solve your problem, lets first understand how the google map camera works. When you do:

map.moveCamera(CameraUpdateFactory.newLatLngZoom(route.startLocation, 10));

The camera focuses the startlocation in the middle with 10 as the zoom level. What we need is to focus the route. For this we need to use the latLng bounds. Once we create a bound with two or more latLngs, we can set it to the camera of the map. This way your route is visible to your users.

Create a bound with all the start and end markers on your route:

LatLngBounds.Builder builder = new LatLngBounds.Builder();
builder.include(startMarker);
builder.include(endMarker);
LatLngBounds bounds = builder.build();

Then obtain a movement description object by using the factory: CameraUpdateFactory:

int padding = 0; // padding around start and end marker
CameraUpdate cu = CameraUpdateFactory.newLatLngBounds(bounds, padding);
googleMap.animateCamera(cu);
like image 150
amalBit Avatar answered Nov 13 '22 04:11

amalBit