我有一个 map View ,它填满了屏幕。在 map 顶部,我有一个“显示和隐藏区域”,其中包含一些详细信息,如下所示:
在这种情况下,我想在 map 上的特定位置(纬度、经度)上显示标记。 我的问题是使标记始终可见,因此当显示“显示和隐藏区域”时,标记应该位于底部 map 的可见区域中(在红色框中标记)。现在,标记显示在“显示和隐藏区域”后面(位于 map 中心)。 我只能通过执行以下操作来获取 map View 的区域(以像素(高度)为单位):(height_of_map) - (height_of_details_area)。但是,当我使用 animateCamera 方法时,这对我没有多大帮助,因为它需要纬度和经度。
这是我设置标记并将相机动画移动到其位置的代码:
private void setMarkerOnMap(double latitude, double longitude) {
if (marker == null) {
CameraUpdate zoom = CameraUpdateFactory.zoomTo(12.0f);
map.animateCamera(zoom);
marker = map.addMarker(new MarkerOptions().position((new LatLng(latitude, longitude))));
marker.setIcon(BitmapDescriptorFactory.fromResource(R.drawable.pin_blue));
} else {
marker.setPosition(new LatLng(latitude, longitude));
}
CameraUpdate center = CameraUpdateFactory.newLatLngZoom(new LatLng(latitude, longitude), 12.0f);
map.animateCamera(center);
}
我希望任何人都能理解这个问题并知道如何解决它。
(是的,我考虑过在显示/隐藏详细信息区域时调整 map 大小,但我发现这个解决方案有点“丑陋”)
最佳答案
我自己找到了方法。我希望这对其他人有用:
private void setMarkerOnMap(double latitude, double longitude) {
if (marker == null) {
marker = map.addMarker(new MarkerOptions().position((new LatLng(latitude, longitude))));
marker.setIcon(BitmapDescriptorFactory.fromResource(R.drawable.pin_blue));
} else {
marker.setPosition(new LatLng(latitude, longitude));
}
Projection projection = map.getProjection();
LatLng markerLatLng = new LatLng(marker.getPosition().latitude, marker.getPosition().longitude);
Point markerScreenPosition = projection.toScreenLocation(markerLatLng);
Point pointHalfScreenAbove = new Point(markerScreenPosition.x, (markerScreenPosition.y - (mapViewHeightFull / 2)) + mapViewHeightHalfPart);
LatLng aboveMarkerLatLng = projection.fromScreenLocation(pointHalfScreenAbove);
CameraUpdate camPosition = CameraUpdateFactory.newLatLngZoom(aboveMarkerLatLng, defaultZoom);
map.animateCamera(camPosition);
}
mapViewHeightHalfPart 变量,是问题中标有红色边框的区域高度的一半。
关于java - Android MapView - 如何获取 map 选定部分的中心?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33010140/