How To Move An Image Over The Path Between Two Location On Map In Android
I am working on a project in which I am showing two random location and the path between them.I have used this tutorial to accomplish. Now i want to show the moving image from one
Solution 1:
privatedoublebearingBetweenLocations(LatLng latLng1, LatLng latLng2){
finaldouble PI = 3.14159;
finaldouble lat1 = latLng1.latitude * PI / 180;
finaldouble long1 = latLng1.longitude * PI / 180;
finaldouble lat2 = latLng2.latitude * PI / 180;
finaldouble long2 = latLng2.longitude * PI / 180;
finaldouble dLon = (long2 - long1);
finaldouble y = Math.sin(dLon) * Math.cos(lat2);
finaldouble x = Math.cos(lat1) * Math.sin(lat2) - Math.sin(lat1)
* Math.cos(lat2) * Math.cos(dLon);
double brng = Math.atan2(y, x);
brng = Math.toDegrees(brng);
brng = (brng + 360) % 360;
return brng;
}
Solution 2:
Suppose you have current and destination co-ordinates like below.
privateLatLngCURRENT_LOC=newLatLng(23.013171, 72.522300);
privateLatLngDESTINATION_LOC=newLatLng(23.013481, 72.522496);
After that add marker on google map
if (googleMap != null)
{
BitmapDescriptoricon= BitmapDescriptorFactory.fromResource(R.drawable.truck_16);
MarkerOptionscurrent=newMarkerOptions().position(CURRENT_LOC).title("Current Point");
current_marker = googleMap.addMarker(current);
current_marker.setIcon(icon);
current_marker.setFlat(true);
MarkerOptionsdestination=newMarkerOptions().position(DESTINATION_LOC).title("Destination Point");
destination_marker = googleMap.addMarker(destination);
destination_marker.setFlat(true);
}
And move marker on click
@OverridepublicvoidonClick(View v)
{
switch (v.getId())
{
case R.id.btn_move:
floatrotate= (float) bearingBetweenLocations(CURRENT_LOC, DESTINATION_LOC);
rotateMarker(rotate);
break;
}
}
below all are methods that move marker from current location to destnation
privatedoublebearingBetweenLocations(LatLng latLng1,LatLng latLng2)
{
doublePI=3.14159;
doublelat1= latLng1.latitude * PI / 180;
doublelong1= latLng1.longitude * PI / 180;
doublelat2= latLng2.latitude * PI / 180;
doublelong2= latLng2.longitude * PI / 180;
doubledLon= (long2 - long1);
doubley= Math.sin(dLon) * Math.cos(lat2);
doublex= Math.cos(lat1) * Math.sin(lat2) - Math.sin(lat1)
* Math.cos(lat2) * Math.cos(dLon);
doublebrng= Math.atan2(y, x);
brng = Math.toDegrees(brng);
brng = (brng + 360) % 360;
return brng;
}
publicvoidrotateMarker(float rotate)
{
if (current_marker != null)
{
//final LatLngInterpolator latLngInterpolator = new LatLngInterpolator.LinearFixed();ValueAnimatorvalueAnimator=newValueAnimator();
//final LatLng startPosition = current_marker.getPosition();finalfloatstartRotation= current_marker.getRotation();
finalfloatangle=180 - Math.abs(Math.abs(startRotation - rotate) - 180);
finalfloatright= WhichWayToTurn(startRotation, rotate);
valueAnimator.addUpdateListener(newValueAnimator.AnimatorUpdateListener()
{
@OverridepublicvoidonAnimationUpdate(ValueAnimator animation)
{
try
{
if (current_marker == null) // oops... destroying map during animation...
{
return;
}
floatv= animation.getAnimatedFraction();
//newPosition = latLngInterpolator.interpolate(v, startPosition, toLatLng(location));floatrotation= startRotation + right * v * angle;
current_marker.setRotation(rotation);
//current_marker.setPosition(newPosition);
}
catch (Exception ex)
{
ex.printStackTrace();
}
}
});
valueAnimator.addListener(newAnimatorListenerAdapter()
{
@OverridepublicvoidonAnimationEnd(Animator animation)
{
//current_marker.setPosition(newPosition);
animateMarker(current_marker, DESTINATION_LOC, false);
}
});
valueAnimator.setFloatValues(0, 1);
valueAnimator.setDuration(1000);
valueAnimator.start();
}
}
publicvoidanimateMarker(final Marker marker, final LatLng toPosition, finalboolean hideMarker)
{
finalHandlerhandler=newHandler();
finallongstart= SystemClock.uptimeMillis();
Projectionproj= googleMap.getProjection();
PointstartPoint= proj.toScreenLocation(marker.getPosition());
finalLatLngstartLatLng= proj.fromScreenLocation(startPoint);
finallongduration=5000;
finalInterpolatorinterpolator=newLinearInterpolator();
handler.post(newRunnable() {
@Overridepublicvoidrun() {
longelapsed= SystemClock.uptimeMillis() - start;
floatt= interpolator.getInterpolation((float) elapsed / duration);
doublelng= t * toPosition.longitude + (1 - t) * startLatLng.longitude;
doublelat= t * toPosition.latitude + (1 - t) * startLatLng.latitude;
marker.setPosition(newLatLng(lat, lng));
if (t < 1.0) {
// Post again 16ms later.
handler.postDelayed(this, 16);
} else {
if (hideMarker) {
marker.setVisible(false);
} else {
marker.setVisible(true);
}
}
}
});
}
privatefloatWhichWayToTurn(float currentDirection, float targetDirection)
{
floatdiff= targetDirection - currentDirection;
if (Math.abs(diff) == 0)
{
return0;
}
if(diff > 180)
{
return -1;
}
else
{
return1;
}
}
Hope this would help you.
Solution 3:
Finally I found a way to accomplish this.May be this is not the best way but it solves the problem.I am posting this so in future if someone needs to do stuff like that,It can be helpful.
I have store all the points in an ArrayList called MarkerPoints
that we get from the API and use it to draw the image on ever point.
Here is the code:
publicclassMyActivityextendsFragmentActivityimplementsRunnable
{
privateThreadthread=null;
volatileboolean isRunning;
private ArrayList<LatLng> MarkerPoints; //This arrayList contains all points that are on the routeinti=0;
Marker marker;
@OverrideprotectedvoidonCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_on_trip_maps);
isRunning = true;
}
@Overridepublicvoidrun() {
while (isRunning) {
runOnUiThread(newRunnable() {
@Overridepublicvoidrun() {
update();
}
});
control();
}
}
publicvoidupdate() {
if (marker != null) {
marker.remove();
}
BitmapDescriptoricon= BitmapDescriptorFactory.fromResource(R.drawable.truck_16);
marker = mMap.addMarker(newMarkerOptions().position(MarkerPoints.get(i))
.title("Current Location")
.icon(icon));
System.out.println(MarkerPoints.get(i));
i++;
if (i > MarkerPoints.size() - 1) {
isRunning = false;
}
}
publicvoidcontrol() {
try {
thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
This will give the moving effect.
Post a Comment for "How To Move An Image Over The Path Between Two Location On Map In Android"