当一个标记被点击时,相机的默认行为是在屏幕上居中,但因为我通常在信息窗口中有很长的文本描述,所以更方便的是实际改变相机的位置,使标记在屏幕的底部(使信息窗口在屏幕的中心)。我想我应该能够通过重写onMarkerClick函数来做到这一点,如下所示(当这个函数返回true时,默认行为被取消)
@Override
public boolean onMarkerClick(final Marker marker) {
// Google sample code comment : We return false to indicate that we have not
// consumed the event and that we wish
// for the default behavior to occur (which is for the camera to move
// such that the
// marker is centered and for the marker's info window to open, if it
// has one).
marker.showInfoWindow();
CameraUpdate center=
CameraUpdateFactory.newLatLng(new LatLng(XXXX,
XXXX));
mMap.moveCamera(center);//my question is how to get this center
// return false;
return true;
}编辑:
使用accepted的步骤解决问题,代码如下:
@Override
public boolean onMarkerClick(final Marker marker) {
//get the map container height
LinearLayout mapContainer = (LinearLayout) findViewById(R.id.map_container);
container_height = mapContainer.getHeight();
Projection projection = mMap.getProjection();
LatLng markerLatLng = new LatLng(marker.getPosition().latitude,
marker.getPosition().longitude);
Point markerScreenPosition = projection.toScreenLocation(markerLatLng);
Point pointHalfScreenAbove = new Point(markerScreenPosition.x,
markerScreenPosition.y - (container_height / 2));
LatLng aboveMarkerLatLng = projection
.fromScreenLocation(pointHalfScreenAbove);
marker.showInfoWindow();
CameraUpdate center = CameraUpdateFactory.newLatLng(aboveMarkerLatLng);
mMap.moveCamera(center);
return true;
}感谢您的帮助^^
发布于 2020-08-14 15:37:42
我也遇到了这个问题,并以一种粗糙的方式解决了它。让我们先声明一个双精度字段。你需要根据你的需要调整它的值,但我建议你保持在0.001~0.009之间,否则在缩放动画之后你可能会错过你的标记。
double offset = 0.009
/*You can change it based on your requirement.
For left-right alignment please kindly keep it between 0.001~0.005 */底部居中的:
LatLng camera = new LatLng(marker.getPosition().latitude+offset , marker.getPosition().longitude);
//Here "marker" is your target market on which you want to focus顶部居中的:
LatLng camera = new LatLng(marker.getPosition().latitude-offset , marker.getPosition().longitude);左居中的:
LatLng camera = new LatLng(marker.getPosition().latitude, marker.getPosition().longitude+offset);右居中的:
LatLng camera = new LatLng(marker.getPosition().latitude-offset , marker.getPosition().longitude-offset);最后调用-
mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(camera, yourZoom));https://stackoverflow.com/questions/16764002
复制相似问题