1

私はGoogleマップ上にマーカーとして表示したいLatLngポイントのリストを持っています。ポイントとパディングからCameraUpdateを作成してマーカーをうまく表示

  1. まず、私はすべての私のポイントでLatLngBoundsを計算: は、私は現在、次のように私のすべての点にフィットするカメラビューを計算しています。

    CameraUpdate cameraUpdate = CameraUpdateFactory.newLatLngBounds(bounds, padding); 
    

更新はいくつかの素晴らしいパディングと、すべての私の点で良好な眺めを提供しています:

Iterator<LatLng> i = items.iterator(); 
LatLngBounds.Builder builder = LatLngBounds.builder(); 
while (i.hasNext()) { 
    builder.include(i.next()); 
} 
LatLngBounds bounds = builder.build(); 
  • は、それから私は、適切な方法を使用します。

    質問

    私はpaddingTop、paddingBottomの、paddingLeftおよびpaddingRightに異なる値を適用したいと思います。すなわち、私は両側に何も持っていない、私は上部にフローティングツールバー(約50dp)と下部にフローティングカード(約200dp)があります。

    パディングを追加しないと、ポイントの一部が上部のツールバーまたは下部のカードで覆われます。

    最大パディング(200dp)を追加すると、上下左右に大きなギャップがあります。何か案が?

    __________________________ 
    | ___________o__________ | 
    | |     | | 
    | |____________________|o| 
    |   o    | 
    |     o  | 
    | o     | 
    |   MAP   | 
    |  o     | 
    |     o | 
    | ______________________ | 
    | |     | | 
    |o|     | | 
    | |     |o| 
    | |____________________| | 
    |____________________o___| 
    

    私の唯一のアイデア右今、ピクセル単位でのマップ幅を取得するLNG(明確に定義されていない)のマップ幅を取得し、ピクセル/ LNGの比率を求め、LNGへのピクセルから希望のパディングを変換して再計算LatLngBoundsは、元の境界から離れている偽のポイント(各面に1つずつ)を追加します。

  • 答えて

    1

    これはうまくいくようですが、もしあれば、もっと良い解決策を待つつもりです。

    マップを保持するビューの幅と高さ(ピクセル単位)を渡す必要があります。これにより、ピクセルを投影座標係数に計算できます。 次に、Rectオブジェクトに希望のパディングを渡します。

    多くの点がある場合は、おそらくバックグラウンドタスクでの実行に最適です。

    public static CameraUpdate computeCameraView(final List<LatLng> items, 
                  final GoogleMap map, final Rect padding, 
                  final int viewWidth, final int viewHeight) { 
    
        // Compute bounds without padding 
        Iterator<LatLng> i = items.iterator(); 
        LatLngBounds.Builder builder = LatLngBounds.builder(); 
        while (i.hasNext()) { 
         builder.include(i.next()); 
        } 
        LatLngBounds bounds = builder.build(); 
    
        // Create a first CameraUpdate and apply 
        CameraUpdate cameraUpdate = CameraUpdateFactory.newLatLngBounds(bounds, 0); 
        map.moveCamera(cameraUpdate); 
    
        // Convert padding to lat/lng based on current projection 
        bounds = map.getProjection().getVisibleRegion().latLngBounds; 
        double mapWidth = bounds.northeast.longitude - bounds.southwest.longitude; 
        double mapHeight = bounds.northeast.latitude - bounds.southwest.latitude; 
        double pixelToLng = mapWidth/viewWidth; 
        double pixelToLat = mapHeight/viewHeight; 
        padding.top = (int) (padding.top * pixelToLat); 
        padding.bottom = (int) (padding.bottom * pixelToLat); 
        padding.left = (int) (padding.left * pixelToLng); 
        padding.right = (int) (padding.right * pixelToLng); 
    
        // Now padding holds insets in lat/lng values. 
        // Let's create two fake points and bound 
        LatLng northEast = new LatLng(bounds.northeast.latitude + padding.top, 
          bounds.northeast.longitude + padding.right); 
        LatLng southWest = new LatLng(bounds.southwest.latitude - padding.bottom, 
          bounds.southwest.longitude - padding.left); 
        LatLngBounds newBounds = new LatLngBounds.Builder() 
          .include(northEast).include(southWest).build(); 
    
        return CameraUpdateFactory.newLatLngBounds(newBounds, 0); 
    } 
    

    ここで弱点はdouble mapWidth = bounds.northeast.longitude - bounds.southwest.longitudeです。このような経度は循環変数であるため、計算することはできません(可視領域が180子午線を横切る場合に問題が発生する可能性があります)。 私はそれを調べます。

    これは十分なはずです:

    public static double computeLngDistance(LatLng east, LatLng west) { 
        if (east.longitude <= 0 && west.longitude >= 0) { 
         // We are crossing the 180 line. 
         return (east.longitude + 180d) + (180d - west.longitude); 
        } else { 
         return east.longitude - west.longitude; 
        } 
    } 
    
    関連する問題