我构建了一个使用GMaps和几个标记的应用程序。用户可以添加或删除标记但我的问题是选择好的缩放级别来显示所有标记。
如果用户添加或删除标记,则缩放级别可能会发生变化。
为了使相机居中,我使用以下代码:
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 :(得分:2)
我了解您需要为地图定义一个中心,并确保所有标记都在视图中。
在这种情况下,您可以计算包含标记的font-family: 'KF-Kiran'; // here give the same name of .ttf file.
,然后使用Google Maps API Utility Library中的LatLngBounds
,SphericalUtil.computeOffset
和SphericalUtil.computeDistanceBetween
方法对其进行扩展
SphericalUtil.computeHeading
您可以测试此方法
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;
}
答案 1 :(得分:0)
map.setMapType(GoogleMap.MAP_TYPE_TERRAIN);
CameraUpdate update = CameraUpdateFactory.newLatLngZoom(LOCATION_MYPOSITION, 16);
map.animateCamera(update);
试试这个,我认为16变焦足以看到标记