I have an arraylist of LatLng coordinates. And I draw a polyline based on that coordinates list. How can I fit that polyline drawing into my screen? Is LatLngBounds.Builder the correct solution for that? If it is how can I use it?
A polyline is a list of points, where line segments are drawn between consecutive points.
A polyline consists of a set of points connected by straight line segments. A polyline can cross itself. If the coordinates of the first and last points are the same, the polyline is called a closed polyline. A polygon is set using one or more closed polylines.
You can try something like this:
private void moveToBounds(Polyline p){
LatLngBounds.Builder builder = new LatLngBounds.Builder();
for(int i = 0; i < p.getPoints().size();i++){
builder.include(p.getPoints().get(i));
}
LatLngBounds bounds = builder.build();
int padding = 0; // offset from edges of the map in pixels
CameraUpdate cu = CameraUpdateFactory.newLatLngBounds(bounds, padding);
mMap.animateCamera(cu);
}
I have do some improvements to the answer: The answer from Melo ist to slow
private void moveToBounds(Polyline p)
{
LatLngBounds.Builder builder = new LatLngBounds.Builder();
List<LatLng> arr = p.getPoints();
for(int i = 0; i < arr.size();i++){
builder.include(arr.get(i));
}
LatLngBounds bounds = builder.build();
int padding = 40; // offset from edges of the map in pixels
CameraUpdate cu = CameraUpdateFactory.newLatLngBounds(bounds, padding);
mMap.animateCamera(cu);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With