gpt4 book ai didi

java - 如何在 Google map 上选择正确的缩放比例

转载 作者:行者123 更新时间:2023-11-29 19:32:17 25 4
gpt4 key购买 nike

我构建了一个使用带有多个标记的 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));

但我没有找到具有良好缩放值的解决方案。

最佳答案

我知道您需要为 map 定义一个中心并确保所有标记都在 View 内。

在这种情况下,您可以计算包含您的标记的 LatLngBounds,然后使用 SphericalUtil.computeOffsetSphericalUtil.computeDistanceBetween 和来自 Google Maps API Utility LibrarySphericalUtil.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;
}

你可以测试这个方法做

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));

关于java - 如何在 Google map 上选择正确的缩放比例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39745059/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com