and*_*per 10 android google-maps google-maps-api-3
假设我有一个谷歌地图视图,另一个视图,它覆盖了它的一部分,隐藏了地图的一些内容.
我需要制作地图的"相机",聚焦并在坐标上有一个标记,但让它全部位于地图可见部分的中间.
像这样的东西:
原始代码专注于(约)整个屏幕的中心,使标记几乎不可见(底部视图覆盖它).
事实是,我找不到正确的方法来将正确的值设置为地图本身的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());
Run Code Online (Sandbox Code Playgroud)
可悲的是,这一点集中在标记之上.
我写的是什么问题?我该怎么办才能修复它?
好的,我找到了一个解决方法,我认为它适用于所有设备(在3上测试,每个设备具有不同的屏幕分辨率和大小):
我已经测量了标记本身有多少像素(然后转换为DP)一度的变化.
由此,我测量了每个视图的高度,并计算了移动相机所需的增量.
就我而言,就是这样(假设缩放是6.5f):
//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());
Run Code Online (Sandbox Code Playgroud)
它奏效了.
我想知道它是否可以调整以支持任何缩放值.
| 归档时间: |
|
| 查看次数: |
518 次 |
| 最近记录: |