【问题标题】:How to create bounds of a android polyline in order to fit the screen?如何创建 android 折线的边界以适应屏幕?
【发布时间】:2014-07-17 20:01:56
【问题描述】:

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

【问题讨论】:

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


    【解决方案1】:

    你可以试试这样的:

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

    【讨论】:

      【解决方案2】:

      我对答案做了一些改进: Melo ist 的回答是要慢

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

      【讨论】:

        【解决方案3】:

        您可以使用标记和折线点来做到这一点。

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

        【讨论】:

          【解决方案4】:

          试试:

          private void moveToBounds(Polyline polyline, int padding){
          
              LatLngBounds.Builder builder = new LatLngBounds.Builder();
          
                  for(LatLng latLng : polyline.getPoints()){
                      builder.include(latLng);
                  }
          
              googleMap.animateCamera(CameraUpdateFactory.newLatLngBounds(builder.build(), padding));
          }
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2019-01-22
            • 1970-01-01
            • 2018-10-30
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多