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++; }
Related
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.
I searched a lot i didn't find any proper solution for it.Help and link could be appreciated :-)
maybe its so late !!! but i solve this problem and want put it for some people. maybe useful. and for detail i solve it by new polylineOption every 5 Latlng in map
private static void animateMarker(final GoogleMap map, final Marker marker, final List<LatLng> directionPoint,
final boolean hideMarker, final List<Float> degree, final List<Integer> colors) {
final Handler handler = new Handler();
final long start = SystemClock.uptimeMillis();
final long duration = 300000;
final PolylineOptions[] polylineOptions = {new PolylineOptions()};
final Interpolator interpolator = new LinearInterpolator();
if (map != null) {
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() - 1) {
final LatLng currentPosition = new LatLng(
directionPoint.get(i).latitude * (1 - t) + directionPoint.get(i + 1).latitude * t,
directionPoint.get(i).longitude * (1 - t) + directionPoint.get(i + 1).longitude * t);
marker.setRotation(degree.get(i));
marker.setPosition(currentPosition);
polylineOptions[0].add(directionPoint.get(i)).color(colors.get(i));
map.addPolyline(polylineOptions[0]);
if (i % 5 != 0) {
polylineOptions[0] = new PolylineOptions();
polylineOptions[0].add(directionPoint.get(i)).color(colors.get(i));
map.addPolyline(polylineOptions[0]);
}
CameraUpdate cameraUpdate = CameraUpdateFactory.newLatLng(currentPosition);
map.animateCamera(cameraUpdate);
i++;
}
if (t < 1.0) {
// Post again 100ms later.
handler.postDelayed(this, 100);
} else {
if (hideMarker) {
marker.setVisible(false);
} else {
marker.setVisible(true);
}
}
}
});
}
}
Ok it's my screenshot , color will be change by changing speed of car
Try this -
#Override
public void onMapReady(GoogleMap map) {
// Add a thin red line from A to B.
Polyline line1 = map.addPolyline(new PolylineOptions()
.add(new LatLng(40.1, -74.2), new LatLng(40.7, -74.0))
.width(5)
.color(Color.RED));
and then another line from B to C with a different color and so on
Polyline line2 = map.addPolyline(new PolylineOptions()
.add(new LatLng(40.7, -74.0), new LatLng(41.3, -74.5))
.width(5)
.color(Color.GREEN));
....
Note that getMapAsync() is the new preferred way to get the map object.
https://developers.google.com/maps/documentation/android-api/map
Polyline details here - https://developers.google.com/android/reference/com/google/android/gms/maps/model/Polyline
You could see this results in separated polylines.
This could be improved by adding round start caps and round end caps of each polylines.
polyline.setEndCap(new RoundCap());
polyline2.setStartCap(new RoundCap());
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?
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++;
}
});
}
I have placed a marker for my location. I would like to move my marker smoothly something like the Google maps app. The blue circle moves very smoothly when I keep moving in my car. I would like implement the same for my app. how do I implement this on my app?
As of now my location marker just jumps for different location on very location change and marks the marker there.
Here is what I have don:
googleMap.setMyLocationEnabled(true);
So 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 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 everytime mylocation is called the marker gets removed and calls mylocation method and then creates the marker in the new postition. Instead I would like to get a smooth transition of the marker like Google maps? How to implement this?
Update:
After working couple of times on this: I came to this code finally but then my markers are not showing up:
I am using the below method and calling this every time in 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)
{
// Post again 16ms later.
handler.postDelayed(this, 16);
}
else
{
if (hideMarker)
{
marker.setVisible(false);
}
else
{
marker.setVisible(true);
}
}
}
});
}
Thanks!
Hi I have also the same thing in one of my project and it works like charm.
Add the below piece of code after this line :
marker.setPosition(new LatLng(lat, lng));
//this code
if (googleMap != null)
googleMap.animateCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(lat, lng), 15.0f));
Hope it will help you.
Thanks
You'll have to use an interpolator a linear or maybe an acceleration interpolator between the new latlng of the location and the current latlng of the marker. Here's an example with a bounce interpolator.
final Handler handler = new Handler();
final long start = SystemClock.uptimeMillis();
final long duration = 2500;
final Interpolator interpolator = new BounceInterpolator();
marker.setVisible(true);
handler.post(new Runnable() {
#Override
public void run() {
long elapsed = SystemClock.uptimeMillis() - start;
float t = Math.max(
1 - interpolator.getInterpolation((float) elapsed
/ duration), 0);
marker.setAnchor(0.5f, 1.0f + 6 * t);
marker.setPosition(latlng)
if (t > 0.0) {
// Post again 16ms later.
handler.postDelayed(this, 16);
}
}
});