当一个标记被点击时,相机的默认行为是在屏幕上居中,但因为我通常在信息窗口中有很长的文本描述,所以更方便的是实际改变相机的位置,使标记在屏幕的底部(使信息窗口在屏幕的中心)。我想我应该能够通过重写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;
}感谢您的帮助^^
发布于 2013-05-27 06:09:03
我可能会在稍后编辑此答案以提供一些代码,但我认为可以工作的是:
使用方法将所单击的marker.
LatLng (LatLng M)获取到一个点(点M)。这将为您提供设备显示屏上标记的位置(在pixels).LatLng并使地图居中。关于如何获得地图的高度,请在here上查找我的答案。
// googleMap is a GoogleMap object
// view is a View object containing the inflated map
// marker is a Marker object
Projection projection = googleMap.getProjection();
LatLng markerPosition = marker.getPosition();
Point markerPoint = projection.toScreenLocation(markerPosition);
Point targetPoint = new Point(markerPoint.x, markerPoint.y - view.getHeight() / 2);
LatLng targetPosition = projection.fromScreenLocation(targetPoint);
googleMap.animateCamera(CameraUpdateFactory.newLatLng(targetPosition), 1000, null);发布于 2013-08-06 00:16:45
我更喜欢Larry McKenzie的答案,它不依赖于屏幕投影(即mProjection.toScreenLocation()),我猜测当地图缩放级别较低时,投影分辨率会变差,这使得我有时无法获得准确的位置。因此,基于google地图规范的计算肯定能解决这个问题。
下面是一个示例代码,将标记从底部移动到屏幕尺寸的30%。
zoom_lvl = mMap.getCameraPosition().zoom;
double dpPerdegree = 256.0*Math.pow(2, zoom_lvl)/170.0;
double screen_height = (double) mapContainer.getHeight();
double screen_height_30p = 30.0*screen_height/100.0;
double degree_30p = screen_height_30p/dpPerdegree;
LatLng centerlatlng = new LatLng( latlng.latitude + degree_30p, latlng.longitude );
mMap.animateCamera( CameraUpdateFactory.newLatLngZoom( centerlatlng, 15 ), 1000, null);发布于 2016-09-25 13:45:11
如果您不关心地图的放大,只希望标记位于底部,请参阅下面的内容,我认为这是一个更简单的解决方案
double center = mMap.getCameraPosition().target.latitude;
double southMap = mMap.getProjection().getVisibleRegion().latLngBounds.southwest.latitude;
double diff = (center - southMap);
double newLat = marker.getPosition().latitude + diff;
CameraUpdate centerCam = CameraUpdateFactory.newLatLng(new LatLng(newLat, marker.getPosition().longitude));
mMap.animateCamera(centerCam);https://stackoverflow.com/questions/16764002
复制相似问题