Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create bounds of a android polyline in order to fit the screen?

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?

like image 354
Can Uludağ Avatar asked Jul 17 '14 20:07

Can Uludağ


People also ask

What is polyline in Google map?

A polyline is a list of points, where line segments are drawn between consecutive points.

What is polyline and polygon?

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.


2 Answers

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);
}
like image 58
Melo Avatar answered Oct 19 '22 15:10

Melo


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);
}
like image 43
schiefersoft Avatar answered Oct 19 '22 17:10

schiefersoft