How to create android polyline borders to fit the screen?

I have an arraylist of LatLng coordinates. And I draw a polyline based on this coordinate list. How can I put this polyline drawing on my screen? Is LatLngBounds.Builder the right solution for this? If so, how can I use it?

+4
source share
3 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);
}
+7
source

I have some improvements in the answer: Reply 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);
}
+2
source

.

    public void setMapMarkersBounds(List<Marker> markers, Polyline polyline) {
    LatLngBounds.Builder builder;
    float scale = getApplicationContext().getResources().getDisplayMetrics().density;
    int padding = (int) (40 * scale + 0.5f);
    builder = new LatLngBounds.Builder();

    for (Marker marker : markers) {
        builder.include(marker.getPosition());
    }

    for(int i = 0; i < polyline.getPoints().size(); i++){
        builder.include(polyline.getPoints().get(i));
    }

    LatLngBounds bounds = builder.build();
    CameraUpdate cu = CameraUpdateFactory.newLatLngBounds(bounds, padding);
    mMap.animateCamera(cu, 400, null);
}
0

All Articles