2016-09-28 9 views
1

いくつかのマーカーでGMapsを使用するアプリを作成します。ユーザーはマーカーを追加または削除できますが、問題はすべてのマーカーを表示するための適切なズームレベルを選択することです。Googleマップで正しいズームを選択するには

ユーザーがマーカーを追加または削除すると、ズームレベルが変わる可能性があります。

double xMin = 90.0 ; 
double xMax = 0.0 ; 
double yMin = 90.0 ; 
double yMax = 0.0 ; 

LatLngBounds.Builder builder = new LatLngBounds.Builder(); 

for (Place place : places) { 
       if (place.getLatitude() < xMin) 
        xMin = place.getLatitude(); 

       if (place.getLatitude() > xMax) 
        xMax = place.getLatitude(); 

       if (place.getLongitude() < yMin) 
        yMin = place.getLongitude(); 

       if (place.getLongitude() > yMax) 
        yMax = place.getLongitude(); 

       MarkerOptions markerOptions = new MarkerOptions(); 
       markerOptions.position(new LatLng(place.getLatitude(), place.getLongitude())); 
       markerOptions.title(String.valueOf(place.getId())); 
       Marker marker = this.googleMap.addMarker(markerOptions); 
       allMarkersMap.put(marker, place); 
       displayMarkersMap.put(marker, place); 
       builder.include(marker.getPosition()); 
      } 

      double xPos = (xMax + xMin)/2; 
      double yPos = (yMax + yMin)/2; 

      LatLngBounds bounds = builder.build(); 
      int padding = 1; // offset from edges of the map in pixels 
      //CameraUpdate cameraUpdate = CameraUpdateFactory.newLatLngBounds(bounds, padding); 
      //this.googleMap.moveCamera(cameraUpdate); 
      LatLng latLng = new LatLng(xPos, yPos); 
      CameraPosition cameraPosition = new CameraPosition.Builder().target(latLng).zoom(2).build(); 
      this.googleMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition)); 

しかし、私は良いズーム値を持つための解決策を見つけることができません:私は次のコードを使用し、カメラをセンタリングするため

+0

をやって、このメソッドをテストすることができGoogle Maps API Utility Library

private LatLngBounds findBounds(List<LatLng> positions, LatLng center) { // Add all the positions to a LatLngBounds (including center) LatLngBounds.Builder boundsBuilder = new LatLngBounds.Builder(); for (LatLng position : positions) { boundsBuilder.include(position); } boundsBuilder.include(center); LatLngBounds bounds = boundsBuilder.build(); // Compute the farthest location from the center among the bound corners LatLng northWest = new LatLng(bounds.northeast.latitude, bounds.southwest.longitude); LatLng southEast = new LatLng(bounds.southwest.latitude, bounds.northeast.longitude); LatLng farthest = findFarthestLocation(center, bounds.northeast, northWest, southEast, bounds.southwest); // Expand the bounds adding the projection of the farthest location from the center // in the oposite direction bounds = bounds.including(SphericalUtil.computeOffset(center, SphericalUtil.computeDistanceBetween(center, farthest), SphericalUtil.computeHeading(center, farthest) + 180)); return bounds; } private LatLng findFarthestLocation(LatLng from, LatLng... locations) { LatLng farthest = null; for (LatLng location : locations) { if (farthest == null || SphericalUtil.computeDistanceBetween(from, location) > SphericalUtil.computeDistanceBetween(from, farthest)) { farthest = location; } } return farthest; } 

からSphericalUtil.computeOffsetSphericalUtil.computeDistanceBetweenSphericalUtil.computeHeadingメソッドを使用して、それを拡張することができます。この場合

私は最良の選択肢はあなたのコードにコメントした 'CameraUpdateFactory.newLatLngBounds(bounds、padding);'を使用していると思います – antonio

+0

oodズームレベル?地図上のすべてのマーカーを表示するズームレベルを選択したい – Fred37b

答えて

2

を見るには十分だろうと思います。あなたのマーカーを含むLatLngBoundsを計算して、あなたが

LatLng center = new LatLng(40.384213, -3.875244); 
List<LatLng> positions = new ArrayList<>(); 
positions.add(new LatLng(43.153102, 2.914307)); 
positions.add(new LatLng(42.976521, 1.508057)); 
positions.add(new LatLng(42.492353, 0.123779)); 

Display display = getActivity().getWindowManager().getDefaultDisplay(); 
       Point size = new Point(); 
       display.getSize(size); 
       int width = size.x; 
       int height = size.y; 

LatLngBounds bounds = findBounds(positions,center); 
mMap.animateCamera(CameraUpdateFactory.newLatLngBounds(bounds,width,height,10)); 
+0

よろしくお願い申し上げますが、どのように優れたズームレベルを選択しますか? – Fred37b

+0

ズームレベルを選択する必要はありません。このコードを使用すると、ビューが自動的に調整されてすべてのマーカーが表示され、マップが目的の位置に中央に配置されます。 – antonio

+0

うまくいくようですが、例外があります。私は画面サイズを計算していないので投げます。 – Fred37b

0
map.setMapType(GoogleMap.MAP_TYPE_TERRAIN); 
CameraUpdate update = CameraUpdateFactory.newLatLngZoom(LOCATION_MYPOSITION, 16); 
map.animateCamera(update); 

これを試してみてください、私は16ズームは、私はあなたがあなたのマップの中央を定義し、すべてのマーカーが表示範囲内にあることを確認する必要があることを理解マーカー

+0

マーカーの位置の関数としてズームレベルを動的に選択したいと考えています。ズームレベルの値を自分で選択することはできません。 – Fred37b

関連する問題