Move Markers smoothly android maps - android

I have a list of coordinates, I want to move my marker over the polyline. I have created the polyline and I am able to move the marker also, but it is not smooth. It just moves from one co-ordinate to another.
The code I am using is the one I found on internet.
public void setAnimation(GoogleMap myMap, final List<LatLng> directionPoint, final Bitmap bitmap) {
Marker marker = myMap.addMarker(new MarkerOptions()
.icon(BitmapDescriptorFactory.fromBitmap(bitmap))
.position(directionPoint.get(0))
.title(Speed.get(0))
.flat(true));
System.out.println("SIZZEE: " + Speed.size() + ", " + directionPoint.size());
marker.showInfoWindow();
animateMarker(myMap, marker, directionPoint, true);
}
private void animateMarker(final GoogleMap myMap, final Marker marker, final List<LatLng> directionPoint,
final boolean hideMarker) {
final Handler handler = new Handler();
final long start = SystemClock.uptimeMillis();
Projection proj = myMap.getProjection();
final long duration = 30000;
final Interpolator interpolator = new LinearInterpolator();
handler.post(new Runnable() {
int i = 0;
#Override
public void run() {
long elapsed = SystemClock.uptimeMillis() - start;
float t = interpolator.getInterpolation((float) elapsed
/ duration);
if (i < directionPoint.size()) {
marker.setPosition(directionPoint.get(i));
marker.setTitle(Speed.get(i) + "Km/h");
marker.showInfoWindow();
myMap.moveCamera(CameraUpdateFactory.newLatLngZoom(directionPoint.get(i) , 17.0f));
}
i++;
if (t < 1.0) {
// Post again 16ms later.
handler.postDelayed(this, 500);
} else {
if (hideMarker) {
marker.setVisible(false);
} else {
marker.setVisible(true);
}
}
}
});
}
What can I do so that the marker moves very smoothly?

Related

draw line on google map on button click

i have Array list of LatLong which i got from database, i need to draw polyline on map but one by one(after some interval) and also want to move the marker.
here is my code.
if (v.getId() == R.id.btnPlayBack) {
googleMap.clear();
mStopHandler = true;
for (int i = 0; i < movementLine.size(); i++) {
marker.remove();
LatLng latlng = new LatLng(movementLine.get(i).latitude, movementLine.get(i).longitude);
marker = googleMap.addMarker(new MarkerOptions()
.position(latlng)
.title("Position")
.snippet("Latitude:" + latlng.latitude + ", Longitude:" + latlng.longitude));
googleMap.addPolyline(new PolylineOptions()
.add(new LatLng(latlng.latitude, latlng.longitude)));
animateMarker(googleMap, marker, new LatLng(latlng.latitude, latlng.longitude), false);
}
}
private static void animateMarker(GoogleMap googleMap, 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) {
// Post again 16ms later.
handler.postDelayed(this, 16);
} else {
if (hideMarker) {
marker.setVisible(false);
} else {
marker.setVisible(true);
}
}
}
});
}
but its not working is there i did anything wrong?
i want show movement of marker with polyline.
here when i use
googleMap.addPolyline(new PolylineOptions()
.addAll(movementLine));
it will draw route with all point from array list.but i need show one by one.
please help me out..
Just create one like here:
PolylineOptions options = new PolylineOptions().width(5).color(Color.RED).geodesic(true);
for (int z = 0; z < list.size(); z++) {
LatLng point = list.get(z);
options.add(point);
}
line = myMap.addPolyline(options);
I'm also not sure you should use geodesic
if (v.getId() == R.id.btnPlayBack) {
final Handler handler = new Handler();
timer = new Timer();
TimerTask doAsynchronousTask = new TimerTask() {
#Override
public void run() {
handler.post(new Runnable() {
public void run() {
//callToFetchRecords();
try {
PlayBack();
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
};
timer.schedule(doAsynchronousTask, 0, 2000); //execute in every 2 sec
}
}
private void PlayBack() {
LatLng latlng = new LatLng(movementLine.get(pos).latitude, movementLine.get(pos).longitude);
marker = googleMap.addMarker(new MarkerOptions()
.position(latlng)
.title("Staring Point")
.snippet("Latitude:" + latlng.latitude + ", Longitude:" + latlng.longitude));
googleMap.animateCamera(CameraUpdateFactory.newLatLngZoom(latlng, 19));
options.add(latlng);
RoutePolyline = googleMap.addPolyline(options);
animateMarker(googleMap, marker, latlng, false);
pos++; }

How to move google map marker smoothly between two location with waypoint in android

I need to move google map marker smoothly like this example. Also in my case has some waypoints additional to start and end location. Already I used below code. but it hasn't constant speed at all point.
public static void setAnimation(GoogleMap myMap, final List<LatLng> directionPoint, final Bitmap bitmap) {
Marker marker = myMap.addMarker(new MarkerOptions()
.icon(BitmapDescriptorFactory.fromBitmap(bitmap))
.position(directionPoint.get(0))
.flat(true));
Marker marker1 = myMap.addMarker(new MarkerOptions()
.icon(BitmapDescriptorFactory.fromBitmap(bitmap))
.position(directionPoint.get(10))
.flat(true));
myMap.animateCamera(CameraUpdateFactory.newLatLngZoom(directionPoint.get(0), 10));
animateMarker(myMap, marker, marker1, directionPoint, false);
}
private static void animateMarker(GoogleMap myMap, final Marker marker, final Marker marker1, final List<LatLng> directionPoint,
final boolean hideMarker) {
final Handler handler = new Handler();
final long start = SystemClock.uptimeMillis();
Projection proj = myMap.getProjection();
final long duration = (long) (30000);
final Interpolator interpolator = new LinearInterpolator();
handler.post(new Runnable() {
int i = 0;
#Override
public void run() {
long elapsed = SystemClock.uptimeMillis() - start;
float t = interpolator.getInterpolation((float) elapsed
/ duration);
if (i < directionPoint.size()){
marker.setPosition(directionPoint.get(i));
marker1.setPosition(directionPoint.get(i+10));
}
i++;
if (t < 1.0) {
// Post again 16ms later.
handler.postDelayed(this, 16);
} else {
if (hideMarker) {
marker.setVisible(false);
} else {
marker.setVisible(true);
}
}
}
});
}
Also I need to turn marker according to route between two location.

How to move marker from one location to another location when we have a list of locations on google map android

i am creating google map with multiple markers.Now i want to move a car icon between that markers.Using this Move markers in google map v2 android
I can move a car icon between from first point to second point.But it is not moving from second to third.When i used for loop for this it strightly goes to final point.I added delay also but nothing else worked for me.
Thanks in advance.
This is my code::
public void setAnimation(GoogleMap myMap, final List<LatLng> directionPoint) {
anim_map = myMap;
anim_marker = myMap.addMarker(new MarkerOptions()
.icon(BitmapDescriptorFactory.fromResource(R.drawable.bus_icon))
.position(directionPoint.get(0))
.flat(true));
myMap.animateCamera(CameraUpdateFactory.newLatLngZoom(directionPoint.get(0), 10));
if (directionPoint.size() >= 2) {
for (int i = 0; i < directionPoint.size() - 1; i++) {
h.post(new Runnable() {
#Override
public void run() {
animateMarker(anim_map, anim_marker, directionPoint, false);
}
});
}
}
}
private void animateMarker(GoogleMap myMap, final Marker marker, final List<LatLng> directionPoint,
final boolean hideMarker) {
final long start = SystemClock.uptimeMillis();
final long duration = 350000;
final Interpolator interpolator = new LinearInterpolator();
h.post(new Runnable() {
int i = 0;
#Override
public void run() {
long elapsed = SystemClock.uptimeMillis() - start;
float t = interpolator.getInterpolation((float) elapsed
/ duration);
// Log.e("T Location", t + "");
double lng = t * directionPoint.get(i + 1).longitude + (1 - t) * directionPoint.get(i).longitude;
double lat = t * directionPoint.get(i + 1).latitude + (1 - t) * directionPoint.get(i).latitude;
marker.setPosition(new LatLng(lat, lng));
if (t < 1.0) {
h.postDelayed(this, 1);
} else {
if (hideMarker) {
marker.setVisible(false);
} else {
marker.setVisible(true);
}
}
}
});
}
You can go try this way to make marker move in GoogleMap similar to live driving direction tracking
private Marker mCurrentMarker;
private float ZOOMLEVEL=18.0f;
private LatLng previousLatLon;
private Handler mLocalHandler;
private GoogleMap mGoogleMap;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mLocalHandler = new Handler();
previousLatLon=new LatLng(45.320372, 2.460161);
//Initialize Marker once Google Map object is created
mMarkerOptions = new MarkerOptions().icon(BitmapDescriptorFactory.fromResource(R.drawable.custom_marker_icon));
mMarkerOptions.position(previousLatLon);
mCurrentMarker = mGoogleMap.addMarker(mMarkerOptions);
}
/**
* Call this method to move marker in map to new location in map with updated location
* #param marker
* #param toPosition
* #param fromPosition
*/
public void animateMarker(final Marker marker, final LatLng toPosition,final LatLng fromPosition) {
final long duration = 500;
final Interpolator interpolator = new LinearInterpolator();
mLocalHandler.post(new Runnable() {
#Override
public void run() {
long elapsed = SystemClock.uptimeMillis() - mStartTime;
float t = interpolator.getInterpolation((float) elapsed
/ duration);
marker.setPosition(toPosition);
marker.setAnchor(Constants.MAPANCHOR, Constants.MAPANCHOR);
mGoogleMap.animateCamera(CameraUpdateFactory.newLatLngZoom(toPosition, ZOOMLEVEL));
if (t < 1.0) {
// Post again 16ms later.
mLocalHandler.postDelayed(this, 16);
} else {
marker.setVisible(true);
}
}
}
});
previousLatLon=toPosition;// reassign the previous location to current location
}
Go through my answer for more details here
In your case put a sleep for some milliseconds after for loop
for (int i = 0; i < directionPoint.size() - 1; i++) {
try {
Thread.sleep(5000);
} catch (InterruptedException e)
{
e.printStackTrace();
}
// rest of your code
Finally, i came up with this answer.
We have to update all the values with respect to the size of arraylist.
public void setAnimation(GoogleMap myMap, final List<LatLng> directionPoint) {
anim_map = myMap;
anim_marker = myMap.addMarker(new MarkerOptions()
.icon(BitmapDescriptorFactory.fromResource(R.drawable.bus_icon))
.position(directionPoint.get(0))
.flat(true));
myMap.animateCamera(CameraUpdateFactory.newLatLngZoom(directionPoint.get(0), 10));
animateMarker(anim_map, anim_marker, directionPoint, false);
}
private void animateMarker(GoogleMap myMap, final Marker marker, final List<LatLng> directionPoint,
final boolean hideMarker) {
final long start = SystemClock.uptimeMillis();
long duration = 3500;
final Interpolator interpolator = new LinearInterpolator();
float t = 0;
for (int j = 0; j < directionPoint.size() - 1; j++) {
Log.e("Location Value", j + " " + directionPoint.get(j).latitude + ", " + directionPoint.get(j).longitude + " : " + directionPoint.get(j + 1).latitude + ", " + directionPoint.get(j + 1).longitude);
while (t < j + 1) {
t = t - j;
double lng = t * directionPoint.get(j + 1).longitude + (1 - t) * directionPoint.get(j).longitude;
double lat = t * directionPoint.get(j + 1).latitude + (1 - t) * directionPoint.get(j).latitude;
LatList.add(new LatLng(lat, lng));
long elapsed = SystemClock.uptimeMillis() - start;
t = interpolator.getInterpolation((float) elapsed / duration);
}
}
Log.e("LatList value", LatList.size() + "");
h.post(new Runnable() {
int i = 0;
#Override
public void run() {
marker.setPosition(LatList.get(i));
if ((LatList.size() - 1) > i)
h.postDelayed(this, 1);
Log.e("I value", i + "");
i++;
}
});
}

Implement smooth transition to emulate car marker moving on the map

I want to implement smooth transition to emulate car marker moving on the map. From current postion to selected position.
Even if I suppose that you hadn't try anything, I give you a great method to achieve that .. just because I'm in a good mood
public static void animateMarker(final GoogleMap map, final Marker marker, final LatLng toPosition,
final boolean hideMarker) {
final Handler handler = new Handler();
final long start = SystemClock.uptimeMillis();
Projection proj = map.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) {
// Post again 16ms later.
handler.postDelayed(this, 16);
} else {
if (hideMarker) {
marker.setVisible(false);
} else {
marker.setVisible(true);
}
}
}
});
}
Enjoy it!

Marker drawn multiple times instead of animating on Map V2

I am trying to animate a car marker along the street when I move. But the marker is drawn multiple times instead of moving on the street. I have tried multiple codes including this but the same result every time. Can someone point me what is wrong?
This is another one that isn't working for me.
public void animateMarker(GoogleMap myMap, final Marker marker, final ArrayList<LatLng> directionPoint) {
final Handler handler = new Handler();
final long start = SystemClock.uptimeMillis();
Projection proj = myMap.getProjection();
final long duration = 30000;
final Interpolator interpolator = new LinearInterpolator();
handler.post(new Runnable() {
int i = 0;
#Override
public void run() {
long elapsed = SystemClock.uptimeMillis() - start;
float t = interpolator.getInterpolation((float) elapsed
/ duration);
if (i < directionPoint.size())
marker.setPosition(directionPoint.get(i));
i++;
if (t < 1.0) {
// Post again 16ms later.
handler.postDelayed(this, 16);
} else {
if (marker.isVisible()) {
marker.remove();
} else {
marker.setVisible(true);
}
}
}
});
}
before calling
marker.setPosition(directionPoint.get(i));
remove the marker and then call it
before set marker clear the map and reset marker its work for you
if (mMap != null) {
mMap.clear();
}

Categories

Resources