gpt4 book ai didi

android - 如何使相机居中以使标记位于屏幕底部? (谷歌地图api V2安卓)

转载 作者:IT王子 更新时间:2023-10-28 23:56:26 28 4
gpt4 key购买 nike

单击标记时,相机的默认行为是将其在屏幕上居中,但由于我通常在信息窗口中有很长的文字说明,因此实际更改相机位置以使标记处于打开状态会更方便屏幕底部(使信息窗口位于屏幕中央)。我想我应该可以通过覆盖下面的 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;
}

编辑:

使用公认答案的步骤解决问题,代码如下:

@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;



}

感谢帮助^^

最佳答案

稍后我可能会编辑此答案以提供一些代码,但我认为可行的是:

  1. 获取点击标记的LatLng (LatLng M)。
  2. 使用 Projection.toScreenLocation(LatLng)LatLng M 转换为 Point(Point M)方法。这将为您提供设备显示屏上标记的位置(以像素为单位)。
  3. 计算一个点(新点)的位置,该点高于点 M map 高度的一半。
  4. New Point 转换回 LatLng 并将 map 居中。

here关于如何获取 map 高度的答案。

    // 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);

关于android - 如何使相机居中以使标记位于屏幕底部? (谷歌地图api V2安卓),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16764002/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com