如何在Android Google地图中缩放相机以覆盖路径?

10

我使用LatLng.BuilderGoogle地图中覆盖了多个位置,目前可以正常工作。 有没有办法覆盖两个位置之间的整个路径在Google地图中?

我目前用的代码包括多个位置:

builder = new LatLngBounds.Builder();
builder.include(LatLng1);
builder.include(LatLng2);
googleMap.animateCamera(CameraUpdateFactory.newLatLngBounds(builder.build(), 17));

有什么建议吗?
谢谢


计算您两个地点的平均值,并将您的摄像机动画移到该位置。 - Piyush
1
你的代码没有显示完整的路径区域吗? - Haresh Chhelana
谢谢您的回复...我已经通过将路径中的每个点添加到LAtLngBounds.Builer对象中,然后移动camera来解决了它。 - Michael Shrestha
3个回答

12

我知道你已经找到答案了,但这是我解决问题的方法

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

8

解决方案很简单。

如果将来有人寻找这种功能,这可能会对他们有所帮助。

此方法是在Google方向API返回从A到B位置的路线后调用的。 directionPoints 是路线中点的列表

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

更新

谷歌方向 API 的 JSON 响应

"routes" : [
      {
         "bounds" : {
            "northeast" : {
               "lat" : 27.7136953,
               "lng" : 85.32216629999999
            },
            "southwest" : {
               "lat" : 27.7103725,
               "lng" : 85.3214952
            }
         },
.... etc

所以我将这个范围的latlng用作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);

并将这些点包含在LatLngBounds.Builder中。


1
如何获取方向点? - Asim Gasimzade

1
这是一种最简单的方式。
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));

https://developers.google.com/maps/documentation/android-sdk/views


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接