Android 在安卓谷歌地图中,如何缩放摄像机以覆盖路径?

Android 在安卓谷歌地图中,如何缩放摄像机以覆盖路径?,android,google-maps,android-maps-v2,Android,Google Maps,Android Maps V2,我已经使用LatLng.Builder在googlemap中覆盖了多个位置,并且它正在工作。 在谷歌地图中有没有办法覆盖两个位置之间的整个路径 我的当前代码包含多个位置 builder = new LatLngBounds.Builder(); builder.include(LatLng1); builder.include(LatLng2); googleMap.animateCamera(CameraUpdateFactory.newLatLngBounds(builder.build()

我已经使用
LatLng.Builder
googlemap
中覆盖了多个位置,并且它正在工作。 在
谷歌地图
中有没有办法覆盖两个位置之间的整个路径

我的当前代码包含多个位置

builder = new LatLngBounds.Builder();
builder.include(LatLng1);
builder.include(LatLng2);
googleMap.animateCamera(CameraUpdateFactory.newLatLngBounds(builder.build(), 17));
有什么建议吗?

谢谢

解决方案很简单

这可能有助于将来寻找此类功能的人

googledirectionapi
返回从
位置A到B的路由后,调用此方法<代码>方向点
是路线中点的
列表

public void handleResult(ArrayList<LatLng> directionPoints)
    {
        PolylineOptions rectLine = new PolylineOptions().width(3).color(Color.RED);
        for(int i = 0 ; i < directionPoints.size() ; i++)
        {
            rectLine.add(directionPoints.get(i));
        }

        //this polyline is stored so that it can be removed by calling drawnRoutePath.remove() if needed
        drawnRoutePath = googleMap.addPolyline(rectLine);
        prepareBuilder(directionPoints);
        googleMap.animateCamera(CameraUpdateFactory.newLatLngBounds(builder.build(), 17));
    }
因此,我使用这个边界的
lat
lng
作为
LatLngBounds.Builder
的参数

JSONObject jsonBound = ((JSONObject)jRoutes.get(i)).getJSONObject("bounds");
JSONObject jsonSouthWest = jsonBound.getJSONObject("southwest");
JSONObject jsonNorthEast = jsonBound.getJSONObject("northeast");
LatLng boundSouthWest = new LatLng(jsonSouthWest.getDouble("lat"),jsonSouthWest.getDouble("lng"));
LatLng boundNorthEast = new LatLng(jsonNorthEast.getDouble("lat"),jsonNorthEast.getDouble("lng"));
ArrayList<LatLng> bounds = new ArrayList<LatLng>();
bounds.add(boundNorthEast);
bounds.add(boundSouthWest);
JSONObject jsonBound=((JSONObject)jRoutes.get(i)).getJSONObject(“bounds”);
JSONObject jsonSouthWest=jsonBound.getJSONObject(“西南”);
JSONObject jsonNorthEast=jsonBound.getJSONObject(“东北”);
LatLng boundSouthWest=新LatLng(jsonSouthWest.getDouble(“lat”)、jsonSouthWest.getDouble(“lng”);
LatLng boundNorthEast=新的LatLng(jsonNorthEast.getDouble(“lat”)、jsonNorthEast.getDouble(“lng”);
ArrayList界限=新的ArrayList();
添加(boundNorthEast);
边界。添加(边界);

并在LatLngBounds.Builder中包含了这些要点,我知道您已经找到了答案,但下面是我解决问题的方法

boolean hasPoints = false;
        Double maxLat = null, minLat = null, minLon = null, maxLon = null;

        if (polyline != null && polyline.getPoints() != null) {
            List<LatLng> pts = polyline.getPoints();
            for (LatLng coordinate : pts) {
                // Find out the maximum and minimum latitudes & longitudes
                // Latitude
                maxLat = maxLat != null ? Math.max(coordinate.latitude, maxLat) : coordinate.latitude;
                minLat = minLat != null ? Math.min(coordinate.latitude, minLat) : coordinate.latitude;

                // Longitude
                maxLon = maxLon != null ? Math.max(coordinate.longitude, maxLon) : coordinate.longitude;
                minLon = minLon != null ? Math.min(coordinate.longitude, minLon) : coordinate.longitude;

                hasPoints = true;
            }
        }

        if (hasPoints) {
            LatLngBounds.Builder builder = new LatLngBounds.Builder();
            builder.include(new LatLng(maxLat, maxLon));
            builder.include(new LatLng(minLat, minLon));
            map.moveCamera(CameraUpdateFactory.newLatLngBounds(builder.build(), 48));
        }
boolean hasPoints=false;
Double maxLat=null,minLat=null,minLon=null,maxLon=null;
if(polyline!=null&&polyline.getPoints()!=null){
List pts=polyline.getPoints();
用于(车床坐标:pts){
//找出最大和最小纬度和经度
//纬度
maxLat=maxLat!=null?Math.max(坐标.纬度,maxLat):坐标.纬度;
minLat=minLat!=null?Math.min(坐标.纬度,minLat):坐标.纬度;
//经度
maxLon=maxLon!=null?Math.max(坐标.经度,maxLon):坐标.经度;
minLon=minLon!=null?Math.min(坐标.经度,minLon):坐标.经度;
hasPoints=true;
}
}
如果(hasPoints){
LatLngBounds.Builder=新的LatLngBounds.Builder();
建造商。包括(新板条(maxLat,maxLon));
建造商。包括(新板条(minLat,minLon));
moveCamera(CameraUpdateFactory.newLatLngBounds(builder.build(),48));
}
这里是最简单的方法

private GoogleMap mMap;
  // Create a LatLngBounds that includes Australia.
  private LatLngBounds AUSTRALIA = new LatLngBounds(
  new LatLng(-44, 113), new LatLng(-10, 154));

// Set the camera to the greatest possible zoom level that includes the
// bounds 
mMap.moveCamera(CameraUpdateFactory.newLatLngBounds(AUSTRALIA, 0));

计算两个位置的平均值,并将相机设置到该位置。您的代码是否未显示整个路径区域?谢谢您的回复。。。我通过将路线中的每个点添加到
LAtLngBounds.Builer
对象中,然后移动
摄影机来解决此问题。如何获取方向点
private GoogleMap mMap;
  // Create a LatLngBounds that includes Australia.
  private LatLngBounds AUSTRALIA = new LatLngBounds(
  new LatLng(-44, 113), new LatLng(-10, 154));

// Set the camera to the greatest possible zoom level that includes the
// bounds 
mMap.moveCamera(CameraUpdateFactory.newLatLngBounds(AUSTRALIA, 0));