Reputation: 808
I want the google map in my application to always be perfectly centered on the user, and move with them as their current location changes. (Think pokemon go, how the map actually moves with the user)
My current best implementation simply updates the camera location with an animation every time the location is changed, like so:
// update the location of the camera based on the new latlng, but keep the zoom, tilt and bearing the same
CameraUpdate cameraUpdate = CameraUpdateFactory.newCameraPosition(new CameraPosition(latLng,
googleMap.getCameraPosition().zoom, MAX_TILT, googleMap.getCameraPosition().bearing));
googleMap.animateCamera(cameraUpdate);
googleMap.setLatLngBoundsForCameraTarget(toBounds(latLng, 300));
This however makes the camera movement somewhat choppy and it lags behind the actual user location marker, especially if the user is moving quickly.
Is there a way to tie the movement of the google maps camera so it exactly matches the movement of the user?
Upvotes: 3
Views: 2749
Reputation: 12782
I dont think that the marker is actually on the GoogleMap in case of Pokemon Go. If you want to fix an image (or any kind of view) in the center of the map... Just make sure that the image is at the center of the map in your xml file.
Like this:
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:src="@mipmap/ic_self_position"/>
<fragment
android:layout_width="match_parent"
android:layout_height="match_parent"
class="com.google.android.gms.maps.SupportMapFragment"/>
</RelativeLayout>
So now you have a marker in the center of the map. Ok, but you still don't have it synchronized with the user position... So let's solve this.
I am going to assume that you don't have problems to start your map. So, do that, i'll wait. Done? Ok. Map set.
Just don't forget to disable drags in your map:
@Override
public void onMapReady(GoogleMap googleMap) {
googleMap.getUiSettings().setScrollGesturesEnabled(false);
...
}
Let's receive the user location and move the map.
Using this link:
https://developer.android.com/training/location/receive-location-updates.html
But change some part of the code to this:
public class MainActivity extends ActionBarActivity implements
ConnectionCallbacks, OnConnectionFailedListener, LocationListener {
...
@Override
public void onLocationChanged(Location location) {
mCurrentLocation = location;
mLastUpdateTime = DateFormat.getTimeInstance().format(new Date());
moveUser(Location location);
}
private void moveUser(Location location) {
LatLng latLng = new LatLng(location.getLatitude(), location.getLongitude());
mGoogleMap.animateCamera(CameraUpdateFactory.newLatLng(latLng));
mCharacterImageView.animate(pretendThatYouAreMovingAnimation)
[you can make a animation in the image of the user... like turn left/right of make walk movements]
}
}
If you want to rotate your character in the movement direction, you will need to compare the previous Latlng with the new one and rotate your image (or view... or anything) to point to the moving direction.
If you need more info, maybe this repo could help you: CurrentCenterPositionMap
(The repo don't do what you want... it only uses the same concept of my explanation.)
Upvotes: 5