【发布时间】:2017-01-20 09:59:52
【问题描述】:
我正在 Google 地图上绘制折线列表。我正在寻找一种解决方案,以根据绘制的折线来适应地图的缩放。我已经计算了所有折线的中心点,所以我知道在哪个点上将地图居中。但我没有找到任何解决方案来获得缩放级别。
这里是我使用的代码:
mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(getZoomPoint(), 15));
private LatLng getZoomPoint() {
Double minLatitude = null;
Double minLongitude = null;
Double maxLatitude = null;
Double maxLongitude = null;
for (Feature feature : features) {
List<Coordinates> coordinates = ((LineString) feature.getGeometry()).getCoordinates();
for (Coordinates coordinate : coordinates) {
// --------------------------------------------------------- INITIALISATION
if (minLatitude == null) { // No matter on wich var we check
minLatitude = coordinate.getLatitude();
minLongitude = coordinate.getLongitude();
maxLatitude = coordinate.getLatitude();
maxLongitude = coordinate.getLongitude();
} else {
if (coordinate.getLatitude() < minLatitude) {
minLatitude = coordinate.getLatitude();
}
if (coordinate.getLatitude() > maxLatitude) {
maxLatitude = coordinate.getLatitude();
}
if (coordinate.getLongitude() < minLongitude) {
minLongitude = coordinate.getLongitude();
}
if (coordinate.getLongitude() > maxLongitude) {
maxLongitude = coordinate.getLongitude();
}
}
}
}
double meanLatitude = (minLatitude + maxLatitude) / 2;
double meanLongitude = (minLongitude + maxLongitude) / 2;
return new LatLng(meanLatitude, meanLongitude);
}
我的第一个问题是:有没有办法计算缩放级别值? (这里是硬编码的 '15')。
我的第二个问题是:如何根据相机缩放级别调整折线宽度?我添加了一个监听器:
mMap.setOnCameraChangeListener(new GoogleMap.OnCameraChangeListener() {
@Override
public void onCameraChange(CameraPosition cameraPosition) {
Log.d("ZOOM = ", "" +mMap.getCameraPosition().zoom);
});
我可以使用setWidth(...) 方法更改折线宽度,但我没有找到计算宽度值的“公式”。现在,固定的折线不依赖于缩放级别。
有什么建议吗?
【问题讨论】:
标签: java android google-maps