Issue
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 GoogleMap
it 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:
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.
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?
Solution
I solved this using the 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.
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);
}
Answered By - Smorka
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.