Android setRotation on marker hiding marker - android

I want to move and rotate marker as per direction, move marker is working perfectly but when I adding marker rotation it hide marker, I am using following code for marker move animation and rotation when I comment setRotation() method it work perfectly but when i uncomment setRotation() line it hide marker on location update
public void animateMarker(final LatLng toPosition,final LatLng neLatLong,
final boolean hideMarker) {
final Handler handler = new Handler();
final long start = SystemClock.uptimeMillis();
final long duration = 200;
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);
curMarker.setPosition(toPosition);
curMarker.setRotation(getBearing(toPosition,neLatLong));
if (t < 1.0) {
// Post again 16ms later.
handler.postDelayed(this, 16);
} else {
if (hideMarker) {
curMarker.setVisible(false);
} else {
curMarker.setVisible(true);
}
}
}
});
}
private float getBearing(LatLng begin, LatLng end) {
double lat = Math.abs(begin.latitude - end.latitude);
double lng = Math.abs(begin.longitude - end.longitude);
if (begin.latitude < end.latitude && begin.longitude < end.longitude)
return (float) (Math.toDegrees(Math.atan(lng / lat)));
else if (begin.latitude >= end.latitude && begin.longitude < end.longitude)
return (float) ((90 - Math.toDegrees(Math.atan(lng / lat))) + 90);
else if (begin.latitude >= end.latitude && begin.longitude >= end.longitude)
return (float) (Math.toDegrees(Math.atan(lng / lat)) + 180);
else if (begin.latitude < end.latitude && begin.longitude >= end.longitude)
return (float) ((90 - Math.toDegrees(Math.atan(lng / lat))) + 270);
return -1;
}

Make sure that the .anchor(0.5f, 0.5f) set for your marker and better use SphericalUtil.computeHeading() from Google Maps Android API Utility Library instead of your private float getBearing(LatLng begin, LatLng end)

Related

Animate marker to new location like uber car animation whenever driver location (lat lng) changes on the server

private void GetDriverloc(HashMap<String, String> map) {
Call<DriverLocationResToCus> call = apiInterface.GetDriverLoc(map);
System.out.println("enter the currency alert api" + call.request().url());
call.enqueue(new Callback<DriverLocationResToCus>() {
#Override
public void onResponse(Call<DriverLocationResToCus> call, Response<DriverLocationResToCus> response) {
if (response.isSuccessful()) {
assert response.body() != null;
DriverStartLat = response.body().getDriverCurrentLatStart();
DriverStartLng = response.body().getDriverCurrentLngStart();
DriverEndLat = response.body().getDriverCurrentLatEnd();
DriverEndLng = response.body().getDriverCurrentLngEnd();
/here I am getting the driver's location from server. I need to pass the driver lat lnt to the loop and animate the marker accordingly. I tried to pass it directly to the startpostion and endpostion. But the marker flickers continuesly.
Utilities.printV("DriverStartLat", DriverStartLat);
ValueAnimator polylineAnimator = ValueAnimator.ofInt(0, 100);
polylineAnimator.setDuration(2000);
polylineAnimator.setInterpolator(new LinearInterpolator());
polylineAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
#Override
public void onAnimationUpdate(ValueAnimator valueAnimator) {
List<LatLng> points = greyPolyLine.getPoints();
int percentValue = (int) valueAnimator.getAnimatedValue();
int size = points.size();
int newPoints = (int) (size * (percentValue / 100.0f));
List<LatLng> p = points.subList(0, newPoints);
blackPolyline.setPoints(p);
}
});
polylineAnimator.start();
mHandler = new Handler();
index = -1;
next = 1;
mHandler.postDelayed(new Runnable() {
#Override
public void run() {
if (index < points.size() - 1) {
index++;
next = index + 1;
}
if (index < points.size() - 1) {
startPosition = points.get(index);
endPosition = points.get(next);
}
//when I pass the driver's lat lng directly to the startposition and endposition the marker moves to the new location but it comes back if it does not get new lat lat from the server and also flickers continuously.
ValueAnimator valueAnimator = ValueAnimator.ofFloat(0, 1);
valueAnimator.setDuration(3000);
valueAnimator.setInterpolator(new LinearInterpolator());
valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
#Override
public void onAnimationUpdate(ValueAnimator valueAnimator) {
v = valueAnimator.getAnimatedFraction();
lng = v * endPosition.longitude + (1 - v)
* startPosition.longitude;
lat = v * endPosition.latitude + (1 - v)
* startPosition.latitude;
LatLng newPos = new LatLng(lat, lng);
marker.setPosition(newPos);
marker.setAnchor(0.5f, 0.5f);
marker.setRotation(getBearing(startPosition, newPos));
mMap.moveCamera(CameraUpdateFactory
.newCameraPosition
(new CameraPosition.Builder()
.target(newPos)
.zoom(15.5f)
.build()));
}
});
valueAnimator.start();
mHandler.postDelayed(this, 3000);
}
}, 3000);
Utilities.printV("DriverLocationResToCus", "DriverLocationResToCus SUCCESS");
} else {
Utilities.printV("DriverLocationResToCus", "DriverLocationResToCus FAILURE");
}
}
#Override
public void onFailure(Call<DriverLocationResToCus> call, Throwable t) {
}
});
}
//can anyone help me with this pls
Download MarkerAnimation and LatLngInterpolator files from the below links
https://drive.google.com/file/d/1i-pDZYOtf-Vl8m6wnYulGnv3rb9aVMih/view
https://drive.google.com/file/d/1AypW3b6YIX6UtYFFuh1-lx7Id1lt0QqP/view
or Use the below posted code
MarkerAnimation code:
public class MarkerAnimation {
public static void animateMarkerToGB(final Marker marker, final LatLng finalPosition, final LatLngInterpolator latLngInterpolator) {
final LatLng startPosition = marker.getPosition();
final Handler handler = new Handler();
final long start = SystemClock.uptimeMillis();
final Interpolator interpolator = new AccelerateDecelerateInterpolator();
final float durationInMs = 3000;
handler.post(new Runnable() {
long elapsed;
float t;
float v;
#Override
public void run() {
// Calculate progress using interpolator
elapsed = SystemClock.uptimeMillis() - start;
t = elapsed / durationInMs;
v = interpolator.getInterpolation(t);
marker.setPosition(latLngInterpolator.interpolate(v, startPosition, finalPosition));
// Repeat till progress is complete.
if (t < 1) {
// Post again 16ms later.
handler.postDelayed(this, 16);
}
}
});
}
#TargetApi(Build.VERSION_CODES.HONEYCOMB)
static void animateMarkerToHC(final Marker marker, final LatLng finalPosition, final LatLngInterpolator latLngInterpolator) {
final LatLng startPosition = marker.getPosition();
ValueAnimator valueAnimator = new ValueAnimator();
valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
#Override
public void onAnimationUpdate(ValueAnimator animation) {
float v = animation.getAnimatedFraction();
LatLng newPosition = latLngInterpolator.interpolate(v, startPosition, finalPosition);
marker.setPosition(newPosition);
}
});
valueAnimator.setFloatValues(0, 1); // Ignored.
valueAnimator.setDuration(3000);
valueAnimator.start();
}
#TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH)
public static void animateMarkerToICS(Marker marker, LatLng finalPosition, final LatLngInterpolator latLngInterpolator) {
TypeEvaluator<LatLng> typeEvaluator = new TypeEvaluator<LatLng>() {
#Override
public LatLng evaluate(float fraction, LatLng startValue, LatLng endValue) {
return latLngInterpolator.interpolate(fraction, startValue, endValue);
}
};
Property<Marker, LatLng> property = Property.of(Marker.class, LatLng.class, "position");
ObjectAnimator animator = ObjectAnimator.ofObject(marker, property, typeEvaluator, finalPosition);
animator.setDuration(3000);
animator.start();
}
}
LatLngInterpolator Code :
public interface LatLngInterpolator {
public LatLng interpolate(float fraction, LatLng a, LatLng b);
public class Linear implements LatLngInterpolator {
#Override
public LatLng interpolate(float fraction, LatLng a, LatLng b) {
double lat = (b.latitude - a.latitude) * fraction + a.latitude;
double lng = (b.longitude - a.longitude) * fraction + a.longitude;
return new LatLng(lat, lng);
}
}
public class LinearFixed implements LatLngInterpolator {
#Override
public LatLng interpolate(float fraction, LatLng a, LatLng b) {
double lat = (b.latitude - a.latitude) * fraction + a.latitude;
double lngDelta = b.longitude - a.longitude;
// Take the shortest path across the 180th meridian.
if (Math.abs(lngDelta) > 180) {
lngDelta -= Math.signum(lngDelta) * 360;
}
double lng = lngDelta * fraction + a.longitude;
return new LatLng(lat, lng);
}
}
public class Spherical implements LatLngInterpolator {
/* From github.com/googlemaps/android-maps-utils */
#Override
public LatLng interpolate(float fraction, LatLng from, LatLng to) {
// http://en.wikipedia.org/wiki/Slerp
double fromLat = toRadians(from.latitude);
double fromLng = toRadians(from.longitude);
double toLat = toRadians(to.latitude);
double toLng = toRadians(to.longitude);
double cosFromLat = cos(fromLat);
double cosToLat = cos(toLat);
// Computes Spherical interpolation coefficients.
double angle = computeAngleBetween(fromLat, fromLng, toLat, toLng);
double sinAngle = sin(angle);
if (sinAngle < 1E-6) {
return from;
}
double a = sin((1 - fraction) * angle) / sinAngle;
double b = sin(fraction * angle) / sinAngle;
// Converts from polar to vector and interpolate.
double x = a * cosFromLat * cos(fromLng) + b * cosToLat * cos(toLng);
double y = a * cosFromLat * sin(fromLng) + b * cosToLat * sin(toLng);
double z = a * sin(fromLat) + b * sin(toLat);
// Converts interpolated vector back to polar.
double lat = atan2(z, sqrt(x * x + y * y));
double lng = atan2(y, x);
return new LatLng(toDegrees(lat), toDegrees(lng));
}
private double computeAngleBetween(double fromLat, double fromLng, double toLat, double toLng) {
// Haversine's formula
double dLat = fromLat - toLat;
double dLng = fromLng - toLng;
return 2 * asin(sqrt(pow(sin(dLat / 2), 2) +
cos(fromLat) * cos(toLat) * pow(sin(dLng / 2), 2)));
}
}
}
This is the bit of code that rotates the icon(marker) on the map
private float getBearing(LatLng begin, LatLng end) {
double lat = Math.abs(begin.latitude - end.latitude);
double lng = Math.abs(begin.longitude - end.longitude);
if (begin.latitude < end.latitude && begin.longitude < end.longitude)
return (float) (Math.toDegrees(Math.atan(lng / lat)));
else if (begin.latitude >= end.latitude && begin.longitude < end.longitude)
return (float) ((90 - Math.toDegrees(Math.atan(lng / lat))) + 90);
else if (begin.latitude >= end.latitude && begin.longitude >= end.longitude)
return (float) (Math.toDegrees(Math.atan(lng / lat)) + 180);
else if (begin.latitude < end.latitude && begin.longitude >= end.longitude)
return (float) ((90 - Math.toDegrees(Math.atan(lng / lat))) + 270);
return -1;
}
This is the bit of code that you need to add after getting Lat and Lng from api.
if(marker == null) {
marker = mMap.addMarker(new MarkerOptions().position(endPosition)
.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_ROSE)));
MarkerAnimation.animateMarkerToGB(marker, endPosition, new LatLngInterpolator.Spherical());
marker.setRotation(getBearing(endPosition, startPosition));
} else {
MarkerAnimation.animateMarkerToICS(marker, endPosition, new LatLngInterpolator.Spherical());
marker.setRotation(getBearing(endPosition, startPosition));
}
Happy coding :)

How to move latitude and longitude coordinates using a virtual joystick?

I have an 8 directional joystick (consisting of a knob and pad),
after setting an onTouchListener to the pad, in MotionEvent.ACTION_MOVE, I'm calculating position_x, position_y, distance, and angle, to get a direction[1-8]. Given a direction[1-8], I'd like to continuously execute a specific movement(); but MotionEvent.ACTION_MOVE only executes while finger is moving..
How can I execute a movement of the lat lng continuously?
--Lat/Lng Movement (d=direction)--
private void movement(int d, double lat, double lng) {
if (d==1) { //up
lat = lat + 0.0000002;}
else if (d==2){ //upright
lat = lat + 0.0000001;
lng = lng + 0.0000001;
}
else if (d==3) { //right
lng = lng + 0.0000002;
}
else if (d==4) { //downright
lng = lng + 0.0000001;
lat = lat - 0.0000001;
}
else if (d==5) { //down
lat = lat - 0.0000002;
}
else if (d==6) { //downleft
lat = lat - 0.0000001;
lng = lng - 0.0000001;
}
else if (d==7) { //left
lng = lng - 0.0000002;
}
else if (d==8) { //upleft
lat = lat + 0.0000001;
lng = lng - 0.0000001;
}
}
--action_move, getangle, direction--
case MotionEvent.ACTION_MOVE: {
position_x = (int) (pad.getX() + pad.getWidth() / 2 - knob.getWidth() / 2 * -1 - knob.getX() - pad.getPivotX());
position_y = (int) (pad.getY() + pad.getHeight() / 2 - knob.getHeight() / 2 * -1 - knob.getY() - pad.getPivotY());
distance = (float) Math.sqrt(Math.pow(position_x, 2) + Math.pow(position_y, 2));
angle = (float) getangle(position_x, position_y);
knob.setX(event.getX() + pad.getX() - knob.getWidth() / 2);
knob.setY(event.getY() + pad.getY() - knob.getHeight() / 2);
direction();
movement(direction(), lat, lng);
private double getangle(float x, float y) {
if (x >= 0 && y >= 0) return Math.toDegrees(Math.atan(y / x));
else if (x < 0 && y >= 0) return Math.toDegrees(Math.atan(y / x)) + 180;
else if (x < 0 && y < 0) return Math.toDegrees(Math.atan(y / x)) + 180;
else if (x >= 0 && y < 0) return Math.toDegrees(Math.atan(y / x)) + 360;
return 0;
}
private int direction() {
if (distance > 50) {
if (angle >= 67.5 && angle < 112.5) return 1;
else if (angle >= 112.5 && angle < 157.5) return 2;
else if (angle >= 157.5 && angle < 202.5) return 3;
else if (angle >= 202.5 && angle < 247.5) return 4;
else if (angle >= 247.5 && angle < 292.5) return 5;
else if (angle >= 292.5 && angle < 337.5) return 6;
else if (angle >= 337.5 || angle < 22.5) return 7;
else if (angle >= 22.5 && angle < 67.5) return 8;
} else if (distance <= 50) { //knob at rest in middle
return 0;
}
return 0;
}
You can check the following question's top answer and apply the same logic of using threads and have a while loop in your ui thread.
Continuous "Action_DOWN" in Android

How to display smooth movement of current location in google map android

I am implementing a map navigation application. I get device's current location every 1000 milliseconds and draw a polyline, from the current location to fixed marker point. But when using device inside a vehicle, polyline and current location update not animating like uber or google-map. It's changing suddenly from one point to another.
How to get a smooth animation of the current location change.
you have to try this way it will help you
private void animateMarkerNew(final LatLng startPosition, final LatLng destination, final Marker marker) {
if (marker != null) {
final LatLng endPosition = new LatLng(destination.latitude, destination.longitude);
final float startRotation = marker.getRotation();
final LatLngInterpolatorNew latLngInterpolator = new LatLngInterpolatorNew.LinearFixed();
ValueAnimator valueAnimator = ValueAnimator.ofFloat(0, 1);
valueAnimator.setDuration(2000); // duration 3 second
valueAnimator.setInterpolator(new LinearInterpolator());
valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
#Override
public void onAnimationUpdate(ValueAnimator animation) {
try {
float v = animation.getAnimatedFraction();
LatLng newPosition = latLngInterpolator.interpolate(v, startPosition, endPosition);
marker.setPosition(newPosition);
googleMap.moveCamera(CameraUpdateFactory.newCameraPosition(new CameraPosition.Builder()
.target(newPosition)
.zoom(18f)
.build()));
marker.setRotation(getBearing(startPosition, new LatLng(destination.latitude, destination.longitude)));
} catch (Exception ex) {
//I don't care atm..
}
}
});
valueAnimator.addListener(new AnimatorListenerAdapter() {
#Override
public void onAnimationEnd(Animator animation) {
super.onAnimationEnd(animation);
// if (mMarker != null) {
// mMarker.remove();
// }
// mMarker = googleMap.addMarker(new MarkerOptions().position(endPosition).icon(BitmapDescriptorFactory.fromResource(R.drawable.icon_car)));
}
});
valueAnimator.start();
}
}
Note: marker is mean which marker you want to animate that one.
private interface LatLngInterpolatorNew {
LatLng interpolate(float fraction, LatLng a, LatLng b);
class LinearFixed implements LatLngInterpolatorNew {
#Override
public LatLng interpolate(float fraction, LatLng a, LatLng b) {
double lat = (b.latitude - a.latitude) * fraction + a.latitude;
double lngDelta = b.longitude - a.longitude;
// Take the shortest path across the 180th meridian.
if (Math.abs(lngDelta) > 180) {
lngDelta -= Math.signum(lngDelta) * 360;
}
double lng = lngDelta * fraction + a.longitude;
return new LatLng(lat, lng);
}
}
}
//Method for finding bearing between two points
private float getBearing(LatLng begin, LatLng end) {
double lat = Math.abs(begin.latitude - end.latitude);
double lng = Math.abs(begin.longitude - end.longitude);
if (begin.latitude < end.latitude && begin.longitude < end.longitude)
return (float) (Math.toDegrees(Math.atan(lng / lat)));
else if (begin.latitude >= end.latitude && begin.longitude < end.longitude)
return (float) ((90 - Math.toDegrees(Math.atan(lng / lat))) + 90);
else if (begin.latitude >= end.latitude && begin.longitude >= end.longitude)
return (float) (Math.toDegrees(Math.atan(lng / lat)) + 180);
else if (begin.latitude < end.latitude && begin.longitude >= end.longitude)
return (float) ((90 - Math.toDegrees(Math.atan(lng / lat))) + 270);
return -1;
}

Animate marker on Polyline path

I have 3 markers on a Google Map.
Two Markers to show starting and ending points
Here is the code using to draw a Polyline between these two points:
private void polyLine() {
LatLng starting = new LatLng(##.######, ##.######);
LatLng ending = new LatLng(##.######, ##.######);
PolylineOptions line = new PolylineOptions().add(starting, ending);
mGoogleMap.addMarker(new MarkerOptions().position(starting).title("Start"));
mGoogleMap.addMarker(new MarkerOptions().position(ending).title("End"));
mGoogleMap.addPolyline(line);
}
One Marker to show current Location [HUE_ROSE]
And to animate marker to current location using:
#Override
public void onLocationChanged(Location location)
{
Toast.makeText(this, "Location Changed " + location.getLatitude()
+ location.getLongitude(), Toast.LENGTH_LONG).show();
mLastLocation = location;
if (mCurrLocationMarker != null) {
mCurrLocationMarker.remove();
}
LatLng latLng = new LatLng(location.getLatitude(), location.getLongitude());
if(ourGlobalMarker == null) { // First time adding marker to map
ourGlobalMarker = mGoogleMap.addMarker(new MarkerOptions().position(latLng)
.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_ROSE)));
MarkerAnimation.animateMarkerToICS(ourGlobalMarker, latLng, new LatLngInterpolator.Spherical());
mGoogleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(latLng, 18));
} else {
MarkerAnimation.animateMarkerToICS(ourGlobalMarker, latLng, new LatLngInterpolator.Spherical());
mGoogleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(latLng, 18));
}
}
PROBLEM:
Getting Animating Marker, but right side of Polyline
SOLUTION:
How Can I show Animated Marker on Polyline Path
I tried a lot to find solution for this one, but did not find any thing, share your suggestions.
I am assuming you have 3 marker
1. Source point
2. Destination Point
3. Moving marker
you have to try this way it will help you
private void animateMarkerNew(final LatLng startPosition, final LatLng destination, final Marker marker) {
if (marker != null) {
final LatLng endPosition = new LatLng(destination.latitude, destination.longitude);
final float startRotation = marker.getRotation();
final LatLngInterpolatorNew latLngInterpolator = new LatLngInterpolatorNew.LinearFixed();
ValueAnimator valueAnimator = ValueAnimator.ofFloat(0, 1);
valueAnimator.setDuration(2000); // duration 3 second
valueAnimator.setInterpolator(new LinearInterpolator());
valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
#Override
public void onAnimationUpdate(ValueAnimator animation) {
try {
float v = animation.getAnimatedFraction();
LatLng newPosition = latLngInterpolator.interpolate(v, startPosition, endPosition);
marker.setPosition(newPosition);
googleMap.moveCamera(CameraUpdateFactory.newCameraPosition(new CameraPosition.Builder()
.target(newPosition)
.zoom(18f)
.build()));
marker.setRotation(getBearing(startPosition, new LatLng(destination.latitude, destination.longitude)));
} catch (Exception ex) {
//I don't care atm..
}
}
});
valueAnimator.addListener(new AnimatorListenerAdapter() {
#Override
public void onAnimationEnd(Animator animation) {
super.onAnimationEnd(animation);
// if (mMarker != null) {
// mMarker.remove();
// }
// mMarker = googleMap.addMarker(new MarkerOptions().position(endPosition).icon(BitmapDescriptorFactory.fromResource(R.drawable.icon_car)));
}
});
valueAnimator.start();
}
}
Note: marker is mean which marker you want to animate that one.
private interface LatLngInterpolatorNew {
LatLng interpolate(float fraction, LatLng a, LatLng b);
class LinearFixed implements LatLngInterpolatorNew {
#Override
public LatLng interpolate(float fraction, LatLng a, LatLng b) {
double lat = (b.latitude - a.latitude) * fraction + a.latitude;
double lngDelta = b.longitude - a.longitude;
// Take the shortest path across the 180th meridian.
if (Math.abs(lngDelta) > 180) {
lngDelta -= Math.signum(lngDelta) * 360;
}
double lng = lngDelta * fraction + a.longitude;
return new LatLng(lat, lng);
}
}
}
//Method for finding bearing between two points
private float getBearing(LatLng begin, LatLng end) {
double lat = Math.abs(begin.latitude - end.latitude);
double lng = Math.abs(begin.longitude - end.longitude);
if (begin.latitude < end.latitude && begin.longitude < end.longitude)
return (float) (Math.toDegrees(Math.atan(lng / lat)));
else if (begin.latitude >= end.latitude && begin.longitude < end.longitude)
return (float) ((90 - Math.toDegrees(Math.atan(lng / lat))) + 90);
else if (begin.latitude >= end.latitude && begin.longitude >= end.longitude)
return (float) (Math.toDegrees(Math.atan(lng / lat)) + 180);
else if (begin.latitude < end.latitude && begin.longitude >= end.longitude)
return (float) ((90 - Math.toDegrees(Math.atan(lng / lat))) + 270);
return -1;
}
Try out with setting anchor like follows
mDetailPositionMarker = mDetailGoogleMap.addMarker(new MarkerOptions()
.position(newLatLonValue)
.anchor(0.5f, 0.5f)
.rotation(bearingValue)
.flat(true)
.icon(BitmapDescriptorFactory.fromResource(R.drawable.biketopicon)));
And make sure your icon will not be having any padding or margin. Avoid unnecessary space in icon image than the content like shown below.
// Animation handler for old APIs without animation support
private void animateMarkerTo(final Marker marker, final double lat, final double lng) {
final Handler handler = new Handler();
final long start = SystemClock.uptimeMillis();
final long DURATION_MS = 3000;
final Interpolator interpolator = new AccelerateDecelerateInterpolator();
final LatLng startPosition = marker.getPosition();
handler.post(new Runnable() {
#Override
public void run() {
float elapsed = SystemClock.uptimeMillis() - start;
float t = elapsed/DURATION_MS;
float v = interpolator.getInterpolation(t);
double currentLat = (lat - startPosition.latitude) * v + startPosition.latitude;
double currentLng = (lng - startPosition.longitude) * v + startPosition.longitude;
marker.setPosition(new LatLng(currentLat, currentLng));
// if animation is not finished yet, repeat
if (t < 1) {
handler.postDelayed(this, 16);
}
}
});
}
call this method inside onLocationChange method and pass location lat and lang then you will see a magic ;)

Integrate Compass With Maps Android

i m working on Android Maps and i need to Add Compass functionality in it that is When I click on Compass Button it shows separate compass.
Can any one guide me the Right direction or refrence me any tutorial. I need to add Compass seperatly
thanks in Advance for reading and Replying
The Scenario in My Cass is To Use Separately use The Compass When click on Compass Button
it Will Show
Distance from Current Location(My Location) to the Destination location
Compass
Compass Head Towards North Direction
So to achieve all these I've used the following approach
hope this will help anyone who has the same scenario as I have
The Code Snippet of my Class That will get Distance between two points around the globe and show image as Compass with head towards north direction
public class MapController extends MapActivity implements SensorEventListener {
public static GeoPoint Mypoint, MapPoint;
double km, meter;
public static int kmInDec, meterInDec;
private SensorManager mSensorManager;
private Sensor mAccelerometer, mField;
Location myLoc, mapLoc;
double myLat, myLong, mapLat, mapLong;
public static double lat, lng;
public static double mylat = Double.parseDouble(Constants.DEVICE_OBJ.lat);//31.567615d;
public static double mylon = Double.parseDouble(Constants.DEVICE_OBJ.lng);//74.360962d;
com.google.android.maps.MapController mapController;
public static boolean enabled = false;
#Override
protected void onCreate(Bundle icicle) {
// TODO Auto-generated method stub
super.onCreate(icicle);
setContentView(R.layout.map_layout);
Log.v(Constants.TAG, "In Map Controller");
fetchUI();
}
public void fetchUI() {
myLoc = new Location("Test");
mapLoc = new Location("Test");
mSensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
mAccelerometer = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
mField = mSensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD);
myLat = (31.567615 * 1e6) / 1E6;
myLong = (74.360962d * 1e6) / 1E6;
mapLat = (MapPoint.getLatitudeE6()) / 1E6;
mapLong = (MapPoint.getLongitudeE6()) / 1E6;
myLoc.setLatitude(31.567615);
myLoc.setLongitude(74.360962);
mapLoc.setLatitude(mapLat);
mapLoc.setLongitude(mapLong);
}
protected void onResume() {
super.onResume();
mSensorManager.registerListener(this, mAccelerometer, SensorManager.SENSOR_DELAY_UI);
mSensorManager.registerListener(this, mField, SensorManager.SENSOR_DELAY_UI);
}
protected void onPause() {
super.onPause();
mSensorManager.unregisterListener(this);
}
#Override
protected boolean isRouteDisplayed() {
// TODO Auto-generated method stub
return false;
}
///Method to calculate the Distance between 2 Geo Points aroud the Globe
public double CalculationByDistance(GeoPoint StartP, GeoPoint EndP) {
double lat1 = StartP.getLatitudeE6() / 1E6;
double lat2 = EndP.getLatitudeE6() / 1E6;
double lon1 = StartP.getLongitudeE6() / 1E6;
double lon2 = EndP.getLongitudeE6() / 1E6;
double dLat = Math.toRadians(lat2 - lat1);
double dLon = Math.toRadians(lon2 - lon1);
double a = Math.sin(dLat / 2) * Math.sin(dLat / 2) +
Math.cos(Math.toRadians(lat1)) * Math.cos(Math.toRadians(lat2)) *
Math.sin(dLon / 2) * Math.sin(dLon / 2);
double c = 2 * Math.asin(Math.sqrt(a));
double valueResult = Radius * c;
double km = valueResult / 1;
DecimalFormat newFormat = new DecimalFormat("####");
kmInDec = Integer.valueOf(newFormat.format(km));
meter = valueResult % 1000;
meterInDec = Integer.valueOf(newFormat.format(meter));
Log.i("Radius Value", "" + valueResult + " KM " + kmInDec + " Meter " + meterInDec);
return Radius * c;
}
#Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
// TODO Auto-generated method stub
}
#Override
public void onSensorChanged(SensorEvent event) {
// TODO Auto-generated method stub
if (myLoc == null) return;
float azimuth = event.values[0];
float baseAzimuth = azimuth;
GeomagneticField geoField = new GeomagneticField(Double
.valueOf(myLoc.getLatitude()).floatValue(), Double
.valueOf(myLoc.getLongitude()).floatValue(),
Double.valueOf(myLoc.getAltitude()).floatValue(),
System.currentTimeMillis());
azimuth -= geoField.getDeclination(); // converts magnetic north into true north
// Store the bearingTo in the bearTo variable
float bearTo = myLoc.bearingTo(mapLoc);
// If the bearTo is smaller than 0, add 360 to get the rotation clockwise.
if (bearTo < 0) {
bearTo = bearTo + 360;
}
//This is where we choose to point it
float direction = bearTo - azimuth;
// If the direction is smaller than 0, add 360 to get the rotation clockwise.
if (direction < 0) {
direction = direction + 360;
}
rotateImageView(compasView, R.drawable.app_icon, direction);
//Set the field
String bearingText = "N";
if ((360 >= baseAzimuth && baseAzimuth >= 337.5) || (0 <= baseAzimuth && baseAzimuth <= 22.5))
bearingText = "N";
else if (baseAzimuth > 22.5 && baseAzimuth < 67.5) bearingText = "NE";
else if (baseAzimuth >= 67.5 && baseAzimuth <= 112.5) bearingText = "E";
else if (baseAzimuth > 112.5 && baseAzimuth < 157.5) bearingText = "SE";
else if (baseAzimuth >= 157.5 && baseAzimuth <= 202.5) bearingText = "S";
else if (baseAzimuth > 202.5 && baseAzimuth < 247.5) bearingText = "SW";
else if (baseAzimuth >= 247.5 && baseAzimuth <= 292.5) bearingText = "W";
else if (baseAzimuth > 292.5 && baseAzimuth < 337.5) bearingText = "NW";
else bearingText = "?";
Distance.setText(kmInDec + " Km " + " " + meterInDec + " m");
}
////This is the method to Show the Image as Compass
private void rotateImageView(ImageView imageView, int drawable, float rotate) {
// Decode the drawable into a bitmap
Bitmap bitmapOrg = BitmapFactory.decodeResource(getResources(),
drawable);
// Get the width/height of the drawable
DisplayMetrics dm = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);
int width = bitmapOrg.getWidth(), height = bitmapOrg.getHeight();
// Initialize a new Matrix
Matrix matrix = new Matrix();
// Decide on how much to rotate
rotate = rotate % 360;
// Actually rotate the image
matrix.postRotate(rotate, width, height);
// recreate the new Bitmap via a couple conditions
Bitmap rotatedBitmap = Bitmap.createBitmap(bitmapOrg, 0, 0, width, height, matrix, true);
//BitmapDrawable bmd = new BitmapDrawable( rotatedBitmap );
//imageView.setImageBitmap( rotatedBitmap );
imageView.setImageDrawable(new BitmapDrawable(getResources(), rotatedBitmap));
imageView.setScaleType(ScaleType.CENTER);
}
}
use this guide:
Google Maps Tutorial
search for compass, try launching a new activity with your needs when pressing on the compass (an overlay)
anyway, the tutorial is very comprehensive, I believe it will solve most of your needs =).

Categories

Resources