いくつかのマーカーで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));
しかし、私は良いズーム値を持つための解決策を見つけることができません:私は次のコードを使用し、カメラをセンタリングするため
。
をやって、このメソッドをテストすることができGoogle Maps API Utility Library
から
SphericalUtil.computeOffset
、SphericalUtil.computeDistanceBetween
とSphericalUtil.computeHeading
メソッドを使用して、それを拡張することができます。この場合私は最良の選択肢はあなたのコードにコメントした 'CameraUpdateFactory.newLatLngBounds(bounds、padding);'を使用していると思います – antonio
oodズームレベル?地図上のすべてのマーカーを表示するズームレベルを選択したい – Fred37b