如何创建Android折线的边界以适应屏幕?

时间:2014-07-17 20:01:57

标签: android google-maps-android-api-2

我有一个LatLng坐标的arraylist。我根据坐标列表绘制折线。如何将折线绘图放入我的屏幕? LatLngBounds.Builder是否是正确的解决方案?如果是我怎么用呢?

3 个答案:

答案 0 :(得分:8)

您可以尝试这样的事情:

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);
}

答案 1 :(得分:3)

我对答案做了一些改进: Melo的答案是放慢速度

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 :(得分:0)

您可以使用标记和折线点来完成此操作。

    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);
}