【问题标题】:How to zoom camera to cover the path in android googlemap?如何缩放相机以覆盖android googlemap中的路径?
【发布时间】:2014-08-26 06:43:22
【问题描述】:

我使用LatLng.Builder 覆盖了Google map 的多个位置,并且它正在工作。 有什么办法可以覆盖Google map 中两个位置之间的整个路径?

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

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

有什么建议吗?
谢谢

【问题讨论】:

  • 计算两个位置的平均值并将您的相机动画到该位置。
  • 您的代码没有显示整个路径区域吗?
  • 感谢您的回复...我已通过将路径中的每个点添加到LAtLngBounds.Builer 对象然后移动camera 来解决它。

标签: android google-maps android-maps-v2


【解决方案1】:

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

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

【讨论】:

    【解决方案2】:

    解决方法很简单

    如果将来寻找此类功能,这可能会对某人有所帮助。

    该方法在google direction API返回location A to B的路由后调用。 directionPoints 是路径中点的list

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

    更新

    来自 Google Direction 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

    【讨论】:

    • 如何获得方向点
    【解决方案3】:

    这是最简单的方法

    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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-06-27
      • 1970-01-01
      • 2011-05-07
      • 1970-01-01
      • 2010-12-03
      • 1970-01-01
      • 2015-11-06
      相关资源
      最近更新 更多