如何在Android中的倾斜地图上将相机放置在屏幕底部

时间:2022-11-18 17:12:22

I want to render a map in my android app that guides the user in 3D. To achieve this I use the com.google.android.gms.maps.GoogleMap object. When updating the position of the GoogleMapit centers on the provided position. I want the map to place the provided position on the bottom of the screen. I've found some examples on how this could be achieved. This is the code I use to update the map position:

我想在我的Android应用程序中渲染一个用3D引导用户的地图。为此,我使用了com.google.android.gms.maps.GoogleMap对象。在更新GoogleMapit的位置时,会在提供的位置上进行更新。我希望地图将提供的位置放在屏幕的底部。我已经找到了一些如何实现这一目标的例子。这是我用来更新地图位置的代码:

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

This works fine if I do not use tilt, or if the transformation is reversed (like p.set(p.x, p.y + (gMapView.getHeight()/2));), but when transforming the center to the bottom of the screen like the code above, the positioning jumps back and forth between positions.

如果我不使用倾斜,或者转换是反转的(如p.set(px,py +(gMapView.getHeight()/ 2));),但是当将中心转换为屏幕底部时,这可以正常工作像上面的代码一样,定位在位置之间来回跳跃。

I've found one solution that alternative solution that places the camera correctly:

我找到了一种可以正确放置相机的替代解决方案的解决方案:

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

But it renders twice, causing the map to flicker.

但它呈现两次,导致地图闪烁。

Do anyone have a solution on how to render a tilted map with reference point on the bottom of the screen?

有没有人有关于如何使用屏幕底部的参考点渲染倾斜地图的解决方案?

1 个解决方案

#1


I solved this using the Java Geodesy Library.

我使用Java Geodesy Library解决了这个问题。

Transforming origin with the map is not accurate when the map is tilted. So using the map to calculate the transformation is FUBAR. Using an independent api however works fine.

当地图倾斜时,使用地图变换原点不准确。因此,使用地图计算转换是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);
}

#1


I solved this using the Java Geodesy Library.

我使用Java Geodesy Library解决了这个问题。

Transforming origin with the map is not accurate when the map is tilted. So using the map to calculate the transformation is FUBAR. Using an independent api however works fine.

当地图倾斜时,使用地图变换原点不准确。因此,使用地图计算转换是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);
}