Reputation: 669
I'm trying to achieve similar to how navigation in google maps works, check the below image for reference. In google maps, the marker looks to be at a static location except the camera is changing
.
To achieve this I animate the marker every time my location changes and later I position it towards the bottom of the screen by panning it by -300px in Y coordinate.
But I cannot animate the camera for change in location an moving the screen 300px down at the same time.
When I animate the camera for change in location the marker moves exactly to the centre of the screen and later when I move the camera 300px below other animation takes place which looks very annoying.
Is there a way I can achieve what google maps do? Any reference or help is appreciated.
Upvotes: 4
Views: 3084
Reputation:
The code is completely implemented in the onLocationChanged
but as documented some of it can be extracted during init.
Essentially what is being done is a shadow target is computed from the current location using the distance derived from a screen pixel offset, extrapolated using a computed bearing from last location.
In the screen recording (below), the green circle represents the current location (also depicted by the map location dot) and the blue circle represents the shadow target which is always in the center of the screen as a result of being the target of the map animate.
Here's a quick drawing of concept:
And the code (with a screen recording below):
@Override
public void onLocationChanged(Location location) {
// First point processing
if (lastLocation == null) {
// save last location
lastLocation = location;
// initial camera
CameraPosition.Builder b = CameraPosition.builder().
zoom(15.0F).
target(new LatLng(lastLocation.getLatitude(), lastLocation.getLongitude()));
CameraUpdate cu = CameraUpdateFactory.newCameraPosition(b.build());
mMap.animateCamera(cu);
return;
}
// subsequent updates
LatLng oldPos = new LatLng(lastLocation.getLatitude(), lastLocation.getLongitude());
LatLng newPos = new LatLng(location.getLatitude(), location.getLongitude());
// ignore very small position deviations (prevents wild swinging)
double d = SphericalUtil.computeDistanceBetween(oldPos, newPos);
if (d < 10) {
return;
}
// compute our own bearing (do not use location bearing)
double bearing = SphericalUtil.computeHeading(oldPos, newPos);
//-----------------------------------------------
// Next section really only needs to be done once
// Compute distance of pixels on screen using some desirable "offset"
Projection p = mMap.getProjection();
Point bottomRightPoint = p.toScreenLocation(p.getVisibleRegion().nearRight);
Point center = new Point(bottomRightPoint.x/2,bottomRightPoint.y/2);
Point offset = new Point(center.x, (center.y + 300));
LatLng centerLoc = p.fromScreenLocation(center);
LatLng offsetNewLoc = p.fromScreenLocation(offset);
// this computed value only changes on zoom
double offsetDistance = SphericalUtil.computeDistanceBetween(centerLoc, offsetNewLoc);
//-----------------------------------------------
// Compute shadow target position from current position (see diagram)
LatLng shadowTgt = SphericalUtil.computeOffset(newPos,offsetDistance,bearing);
// update circles
if (centerCircle != null) {
centerCircle.setCenter(shadowTgt);
} else {
centerCircle = mMap.addCircle(new CircleOptions().strokeColor(Color.BLUE).center(shadowTgt).radius(50));
}
if (carCircle != null) {
carCircle.setCenter(newPos);
} else {
carCircle = mMap.addCircle(new CircleOptions().strokeColor(Color.GREEN).center(newPos).radius(50));
}
// update camera
CameraPosition.Builder b = CameraPosition.builder();
b.zoom(15.0F);
b.bearing((float)(bearing));
b.target(shadowTgt);
CameraUpdate cu = CameraUpdateFactory.newCameraPosition(b.build());
mMap.animateCamera(cu);
// save location as last for next update
lastLocation = location;
}
Notes:
offsetDistance
needs to be recomputed. (But as noted it does not need to be done every location changed.)Upvotes: 9