I have googleMap (v2) with polyline that presents a route between the user current location and the destination point.
Now, I want to update the polyline according to the user moving.
I tried to redrawing the whole polyline when location is changed but the polyline is flickering.
I didn't find any appropriate function in the PolylineOptions class
(the function add() is only to add a vertex but not to update or remove)
do you have any idea how to update the polyline ???
thank you for giving your time.
update polyline according to the user moving android googleMaps v2
Asked Answered
hello can u plz tell me how its work i still cant understand I wanna remove polyline part as per i move to destination point –
Fumed
The only way as of version 3.1.36:
List<LatLng> points = polyline.getPoints();
points.add(newPoint);
polyline.setPoints(points);
Hopefully the API will be enhanced in later versions.
but if I remove points from the array and then call setPoints-will the polyline view update automatically? –
Neoplasty
@ MaciejGórski . Can you help me ? #39145692 –
Fete
Very good. But I wouldn't create a new
List
for polyline.getPoints();
all the time. I think it is a good practice to have a List that stores all the points
, update the List
when needed and use that in polyline.setPoints()
. This is because when you have a huge list, Garbage Collector has to clean the new instance of that List
as the app won't use it. –
Undercool can u give a sample project link @Shaw –
Perfecto
but it draw a straight between my current location and destination...But i need polyline update route wise..Plz Help me sir –
Cockroach
Have you find any solution for this issue? –
Grill
Its adding new lines with new locations, how could we remove old lines and update poly line with new location? –
Inefficient
@sksakil hi, i am facing the same issue of straight line between source and destination. How did you solve this issue? Pls help. –
Reckless
*I have already done working on updating polyline path without removing the polyline. We can do this by changing the points of that polyline. Check below code.
This is the logic of setting new points to polyline.
/*Here the routes contain the points(latitude and longitude)*/
for (int i = 0; i < routes.size(); i++) {
Route route = routes.get(i);
if(polyline_path != null){
polyline_path.setPoints(route.points);
}
}
Detail Explanation:
private GoogleMap map_object;
private Marker marker_driver;
private Marker marker_drop_off;
private Polyline polyline_path;
private PolylineOptions polylineOptions_path;
...
...
...
/*HelperDirectionFinder is a class that I create to call google API and I used this
class to get directions routes*/
/*I have created Service, and I'm calling this lines below after 5 sec. to get the
updated routes from google API.*/
HelperDirectionFinder directionFinder = new HelperDirectionFinder(
JobEndScreen.this, source, destinations);
try {
directionFinder.showDirection();
} catch (UnsupportedEncodingException e) {
HelperProgressDialog.closeDialog();
}
...
...
...
@Override
public void onDirectionFinderStart() {
if(polylineOptions_path == null){
HelperProgressDialog.showDialog(getActivity(), "", getString(R.string.text_loading));
}
}
/*This interface method is called after getting routes from google API.*/
/*Here the routes contains the list of path or routes returned by Google Api*/
@Override
public void onDirectionFinderSuccess(List<Route> routes) {
HelperProgressDialog.closeDialog();
/*When polylineOptions_path is null it means the polyline is not drawn.*/
/*If the polylineOptions_path is not null it means the polyline is drawn on map*/
if(polylineOptions_path == null){
for (Route route : routes) {
polylineOptions_path = new PolylineOptions().
geodesic(true).
color(ContextCompat.getColor(getActivity(), R.color.color_bg_gray_dark)).
width(10);
for (int i = 0; i < route.points.size(); i++)
polylineOptions_path.add(route.points.get(i));
polyline_path = map_object.addPolyline(polylineOptions_path);
}
}
else {
for (int i = 0; i < routes.size(); i++) {
Route route = routes.get(i);
if(polyline_path != null){
polyline_path.setPoints(route.points);
}
}
}
}
//Declare on global
PolylineOptions polyOptions, polyOptions2;
Polyline polyline2;
List<LatLng> ltln;
private Double lat_decimal = 0.0,lng_decimal=0.0;
//initialize
ltln = new ArrayList<>();
//if you are using routing library
compile 'com.github.jd-alexander:library:1.0.7'
@Override
public void onRoutingSuccess(ArrayList<Route> arrayList, int i) {
//Add this code snippet on onRoutingSuccess to store latlan list
ltln = new ArrayList<>();
System.out.println("-----arrayList------" + arrayList.get(0).getPoints());
ltln = arrayList.get(0).getPoints();
// NOTE here already we draw polyLine 1
}
//below mentioned how to update polyline
private void UpdatePoliline(){
System.out.println("-------runnablePolyline-------"+ltln.size());
try {
if (ltln.size() > 0) {
for (int i = 0; i < ltln.size(); i++) {
ltln.remove(i);
if (CalculationByDistance(ltln.get(i), new LatLng(lat_decimal, lng_decimal)) >= 10) {
break;
}
}
polyOptions2 = new PolylineOptions();
polyOptions2.color(getResources().getColor(R.color.app_color));
polyOptions2.width(7);
polyOptions2.addAll(ltln);
if (polyline2 == null) {
polyline2 = googleMap.addPolyline(polyOptions2);
if (polyLines.size() > 0) {
for (Polyline poly : polyLines) {
poly.remove();
}
}
polyLines.add(polyline2);
if (polyline != null) {
polyline.remove();
polyline = null;
}
} else {
polyline = googleMap.addPolyline(polyOptions2);
if (polyLines.size() > 0) {
for (Polyline poly : polyLines) {
poly.remove();
}
}
polyLines.add(polyline);
if (polyline2 != null) {
polyline2.remove();
polyline2 = null;
}
}
System.out.println("=====waypoints new===" + ltln);
}
} catch (IndexOutOfBoundsException e) {
e.printStackTrace();
}
}
// Calculating distance between 2 points
public float CalculationByDistance(LatLng StartP, LatLng EndP) {
Location locationA = new Location("Source");
locationA.setLatitude(StartP.latitude);
locationA.setLongitude(StartP.longitude);
Location locationB = new Location("Destination");
locationB.setLatitude(EndP.latitude);
locationB.setLongitude(EndP.longitude);
float distance = locationA.distanceTo(locationB);
return distance;
}
//Redraw Polyline vehicle is not in current polyline with particular distance
if (ltln.size() > 0) {
if (PolyUtil.isLocationOnPath(new LatLng(currentlat, currentLon), ltln, false, 60.0f) ){
System.out.println("===tolarance===" + true);
} else {
//Redraw Polyline
}
}
UpdatePoliline();
© 2022 - 2024 — McMap. All rights reserved.