我正在开发一个 Android 应用程序,我有两个标记要放在谷歌地图上。这个想法是用户可以一眼看到这两个位置,而无需与 map 进行交互。
LatLngBounds.Builder b = new LatLngBounds.Builder();
b.include(userPos);
b.include(cardPos);
LatLngBounds bounds = b.build();
int width = (int) (0.7 * infoView.getWidth());
int height = (int) (0.7 * mapView.getHeight());
CameraUpdate cu = CameraUpdateFactory.newLatLngBounds(bounds, width, height, 5);
googleMap.animateCamera(cu);
当两个位置相距不是很远时,代码可以正常工作。但是当位置例如是新加坡和美国时,两个标记不能一起看到。 我尝试手动将缩放级别设置为 0,结果相同。 有什么方法可以让我一次在 android 设备上显示整个世界地图(即不需要用户滚动)?
更新: 我已尝试将 map 明确设置为缩放级别 0。该 map 不适合我创建的 View 。难道不能在屏幕上看到 map 的完整世界观吗?
最佳答案
您应该使用 CameraUpdate 类来执行(可能)所有程序化 map 移动。
为此,首先像这样计算所有标记的边界:
LatLngBounds.Builder builder = new LatLngBounds.Builder();
for (Marker marker : markers) {
builder.include(marker.getPosition());
}
LatLngBounds bounds = builder.build();
然后通过工厂CameraUpdateFactory获取一个运动描述对象:
int padding = 0; // offset from edges of the map in pixels
CameraUpdate cu = CameraUpdateFactory.newLatLngBounds(bounds, padding);
最后移动 map :
googleMap.moveCamera(cu);
或者如果你想要一个动画:
googleMap.animateCamera(cu);
就是这样:)
关于android - 在安卓屏幕上显示完整的世界地图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41659507/