I put a marker for my location. I would like my marker to smoothly change the Google maps application. The blue circle moves very smoothly as I continue to move in my car. I would like to implement the same for my application. How to implement this in my application?
At the moment, my location marker just jumps for different places when the location changes a lot and marks the marker there.
Here is what I have done:
googleMap.setMyLocationEnabled(true);
So the onMyLocationChange method is called automatically:
@Override public void onMyLocationChange(Location location) { curlat = location.getLatitude(); curlong = location.getLongitude(); if (markermylocation != null) { markermylocation.remove(); } curlat = location.getLatitude(); curlong = location.getLongitude(); myLocation(curlat, curlong, username, imageURL, ZOOMVALUE); }
in the mylocaiton method:
private void myLocation(double lat, double lng, String name, String url, float zoom) { if(firsttime == 1) { LatLng ll = new LatLng(lat,lng); CameraUpdate update = CameraUpdateFactory.newLatLngZoom(ll, zoom); googleMap.animateCamera(update); firsttime = 0; } final String uname = name; curlat = lat; curlong = lng; LatLng position = new LatLng(curlat, curlong); markerOptionsmylocaiton = new MarkerOptions().position(position).icon(BitmapDescriptorFactory.fromBitmap(icon)).title(uname).anchor(0.5f, 1f); markermylocation = googleMap.addMarker(markerOptionsmylocaiton); LatLng latlang = new LatLng(curlat, curlong); animateMarker(markermylocation, latlang, false); }
So, every time mylocation is called a marker, it is deleted and calls the mylocation method, and then creates the marker in a new postulation. Instead, I would like to get a smooth transition of a marker, for example, a Google map? How to implement this?
Update:
After several times on this: I came to this code, but then my markers are not displayed:
I use the method below and each time I call it in the myLocation method.
public void animateMarker(final Marker marker, final LatLng toPosition, final boolean hideMarker) { final Handler handler = new Handler(); final long start = SystemClock.uptimeMillis(); Projection proj = googleMap.getProjection(); Point startPoint = proj.toScreenLocation(marker.getPosition()); final LatLng startLatLng = proj.fromScreenLocation(startPoint); final long duration = 500; final Interpolator interpolator = new LinearInterpolator(); handler.post(new Runnable() { @Override public void run() { long elapsed = SystemClock.uptimeMillis() - start; float t = interpolator.getInterpolation((float) elapsed / duration); double lng = t * toPosition.longitude + (1 - t) * startLatLng.longitude; double lat = t * toPosition.latitude + (1 - t) * startLatLng.latitude; marker.setPosition(new LatLng(lat, lng)); if (t < 1.0) {
Thanks!