背景
假设我有一个google maps视图,上面还有一个视图,它覆盖了其中的一部分,隐藏了地图的一些内容。
问题
我需要制作地图的“相机”,在坐标上对焦并有一个标记,但要让它都在地图可见部分的中间。
像这样的:
最初的代码集中在(关于)整个屏幕的中心,使得标记几乎不可见(因为底部视图覆盖了它)。
问题是,我找不到正确的方法来将正确的值设置为地图本身的y坐标(即纬度)。
我试过的
考虑到底部视图的高度和我放置标记的坐标,我试图计算delta(当然不会改变标记本身):
final float neededZoom = 6.5f;
int bottomViewHeight = bottomView.getHeight();
LatLng posToFocusOn = ...;
final Point point = mMap.getProjection().toScreenLocation(posToFocusOn);
final float curZoom = mMap.getCameraPosition().zoom;
point.y += bottomViewHeight * curZoom / neededZoom;
posToFocusOn = mMap.getProjection().fromScreenLocation(point);
final CameraUpdate cameraPosition = CameraUpdateFactory.newCameraPosition(new Builder().target(posToFocusOn).zoom(neededZoom).build());
遗憾的是,这一点远远超出了标记。
问题
我写的东西怎么了?我能做什么来修理它?
最佳答案
好的,我找到了一个解决方法,我认为它适用于所有设备(在3台设备上测试,每台设备的屏幕分辨率和大小都不同):
我已经测量了多少像素(然后转换为dp)一个度的变化有多少标记本身。
由此,我测量了每个视图的高度,并计算了移动相机所需的增量。
在我的例子中,是这样的(假设变焦为6.5华氏度):
//measured as 223 pixels on Nexus 5, which has xxhdpi, so divide by 3
final float oneDegreeInPixels = convertDpToPixels( 223.0f / 3.0f);
final float mapViewCenter = mapViewHeight / 2.0f;
final float bottomViewHeight = ...;
final float posToFocusInPixelsFromTop = (mapViewHeight - bottomViewHeight) / 2.0f ;// can optionally add the height of the view on the top area
final float deltaLatDegreesToMove = (mapViewCenter - posToFocusInPixelsFromTop) / oneDegreeInPixels;
LatLng posToFocusOn = new LatLng(latitude - deltaLatDegreesToMove, longitude);
final CameraUpdate cameraPosition = CameraUpdateFactory.newCameraPosition(new Builder().target(posToFocusOn).zoom(neededZoom).build());
而且成功了。
我想知道它是否可以调整以支持任何缩放值。