我想在我的Android应用程序中渲染一张地图,该应用程序以3D指导用户。为此,我使用com.google.android.gms.maps.GoogleMap
对象。更新GoogleMap
的位置时,它以提供的位置为中心。我希望地图将提供的位置放在屏幕的底部上。我找到了一些有关如何实现这一目标的例子。这是我用来更新地图位置的代码:
private static final int ZOOM_LEVEL = 20;
private static final int TILT = 90;
private float bearing; // from censors
private MapView gMapView;
private void updatePosition(final LatLng pos){
GoogleMap map = gMapView.getMap();
Point p = map.getProjection().toScreenLocation(pos);
p.set(p.x, p.y - (gMapView.getHeight()/2));
LatLng target = map.getProjection().fromScreenLocation(p);
CameraPosition cap = new CameraPosition.Builder().target(target)
.bearing(bearing).tilt(TILT).zoom(ZOOM_LEVEL).build();
map.moveCamera(CameraUpdateFactory.newCameraPosition(cap));
}
如果我不使用倾斜,或者转换逆转(如p.set(p.x, p.y + (gMapView.getHeight()/2));
),但是当将中心转换到屏幕底部时,就像上面的代码一样,位置在位置之间来回跳跃。
我找到了一种解决方案的解决方案,可以正确放置相机:
CameraPosition cap = new CameraPosition.Builder().target(pos)
.bearing(bearing).tilt(TILT).zoom(ZOOM_LEVEL).build();
map.moveCamera(CameraUpdateFactory.newCameraPosition(cap));
map.moveCamera(CameraUpdateFactory.scrollBy(0, -gMapView.getHeight()/2));
但它呈现两次,导致地图闪烁。
有人有解决方案如何在屏幕底部使用参考点渲染的倾斜地图吗?
我使用Java Geodesy库解决了此问题。
在倾斜地图时,用地图转换原点是不准确的。因此,使用地图计算转换为fubar。但是,使用独立的API工作正常。
private LatLng currentLatLng;
private float AR_MAP_TRANSLATE = 100f;
private CameraUpdate arCamera() {
GlobalCoordinates start = new GlobalCoordinates(currentLatLng.latitude, currentLatLng.longitude);
GlobalCoordinates target = geoCalc.calculateEndingGlobalCoordinates(
Ellipsoid.WGS84, start, mapBearing, AR_MAP_TRANSLATE);
LatLng mapTarget = new LatLng(target.getLatitude(), target.getLongitude());
CameraPosition cap = new CameraPosition(mapTarget,
AR_MAP_ZOOM, AR_MAP_TILT, mapBearing);
return CameraUpdateFactory.newCameraPosition(cap);
}