2017-01-20 7 views
1

私はGoogleマップ上にポリラインのリストを描いています。描画されたポリラインに応じてマップのズームに合わせるソリューションを探しています。私はすべてのポリラインの中心点を計算したので、どの点を中心にしているのか分かります。しかし、ズームレベルを取得するための解決策はありません。Android Googleマップ、ポリライン、ズーム

ここで私が使用するコード:

mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(getZoomPoint(), 15)); 

private LatLng getZoomPoint() { 
    Double minLatitude = null; 
    Double minLongitude = null; 
    Double maxLatitude = null; 
    Double maxLongitude = null; 

    for (Feature feature : features) { 
     List<Coordinates> coordinates = ((LineString) feature.getGeometry()).getCoordinates(); 
     for (Coordinates coordinate : coordinates) { 
      // --------------------------------------------------------- INITIALISATION 
      if (minLatitude == null) { // No matter on wich var we check 
       minLatitude = coordinate.getLatitude(); 
       minLongitude = coordinate.getLongitude(); 
       maxLatitude = coordinate.getLatitude(); 
       maxLongitude = coordinate.getLongitude(); 
      } else { 
       if (coordinate.getLatitude() < minLatitude) { 
        minLatitude = coordinate.getLatitude(); 
       } 
       if (coordinate.getLatitude() > maxLatitude) { 
        maxLatitude = coordinate.getLatitude(); 
       } 
       if (coordinate.getLongitude() < minLongitude) { 
        minLongitude = coordinate.getLongitude(); 
       } 
       if (coordinate.getLongitude() > maxLongitude) { 
        maxLongitude = coordinate.getLongitude(); 
       } 
      } 
     } 
    } 
    double meanLatitude = (minLatitude + maxLatitude)/2; 
    double meanLongitude = (minLongitude + maxLongitude)/2; 
    return new LatLng(meanLatitude, meanLongitude); 
} 

私の最初の質問は:ズームレベルの値を計算する方法はありますか? (ここでは '15'がハードコードされています)。

私の2番目の質問は、どのようにカメラのズームレベルに応じてポリラインの幅を調整できますか?

mMap.setOnCameraChangeListener(new GoogleMap.OnCameraChangeListener() { 
      @Override 
      public void onCameraChange(CameraPosition cameraPosition) { 
       Log.d("ZOOM = ", "" +mMap.getCameraPosition().zoom); 

     }); 

そして私はsetWidth(...)メソッドを使用してポリラインの幅を変更することができますが、私は幅の値を計算する「式」を見つけていない:私は、リスナーを追加しました。さて、修正されていないポリラインはズームレベルに依存します。

提案がありますか?

答えて

4

LatLngBoundsを使用して、修正バウンドのフォーカスを作成できます。

/**Latlng's to get focus*/ 
    LatLng Delhi = new LatLng(28.61, 77.2099); 
    LatLng Chandigarh = new LatLng(30.75, 76.78); 
    LatLng SriLanka = new LatLng(7.000, 81.0000); 
    LatLng America = new LatLng(38.8833, 77.0167); 
    LatLng Arab = new LatLng(24.000, 45.000); 

    /**create for loop/manual to add LatLng's to the LatLngBounds.Builder*/ 
    LatLngBounds.Builder builder = new LatLngBounds.Builder(); 
    builder.include(Delhi); 
    builder.include(Chandigarh); 
    builder.include(SriLanka); 
    builder.include(America); 
    builder.include(Arab); 

    /**initialize the padding for map boundary*/ 
    int padding = 50; 
    /**create the bounds from latlngBuilder to set into map camera*/ 
    LatLngBounds bounds = builder.build(); 
    /**create the camera with bounds and padding to set into map*/ 
    final CameraUpdate cu = CameraUpdateFactory.newLatLngBounds(bounds, padding); 
    /**call the map call back to know map is loaded or not*/ 
    map.setOnMapLoadedCallback(new GoogleMap.OnMapLoadedCallback() { 
     @Override 
     public void onMapLoaded() { 
      /**set animated zoom camera into map*/ 
      map.animateCamera(cu); 
     } 
    }); 

私はコード内で手動で緯度経度を追加していて、上記のコードのように、あなたはList<Coordinates>座標のリストを持っています。これらの座標LatLngオブジェクトをLatLngBounds.Builderに追加し、カメラをアニメーション化すると、対象となるすべてのLatLngが自動的にズームされます。もっと見る:Manage LatLng bounds zoom

関連する問題