Reputation: 854
I build an app which use GMaps with several markers. The user can add or delete markers but my problem is to select the good zoom level to display all the markers.
That zoom level can change if the user add or delete markers.
For centering the camera I use the following code :
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));
But I don't find a solution to have a good zoom value.
Upvotes: 1
Views: 264
Reputation: 18262
I understand that you need to define a center for your map and ensure that all your markers are within the view.
In this case you can compute the LatLngBounds
that include your markers and then extend it using the SphericalUtil.computeOffset
, SphericalUtil.computeDistanceBetween
and SphericalUtil.computeHeading
methods from the 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;
}
You can test this method doing
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));
Upvotes: 2
Reputation: 328
map.setMapType(GoogleMap.MAP_TYPE_TERRAIN);
CameraUpdate update = CameraUpdateFactory.newLatLngZoom(LOCATION_MYPOSITION, 16);
map.animateCamera(update);
Try this , i think 16 zoom will be enough to see the markers
Upvotes: 0