Reputation: 2096
I am trying to remove previously added Polyline and redraw new Polyline when the location has been changed. I tried both
this.routeToDestination.setPoints(pointsToDestination) and this.routeToDestination.remove()
but neither of them worked.
I followed How to draw a dynamic line (route) with Google Maps Android API v2 but could not resolved the issue
@Override
public void onResume() {
super.onResume();
routeToDestination = mMap.addPolyline(new PolylineOptions()
.add(new LatLng(location.getLatitude(), location.getLongitude()),
new LatLng(this.destinationLatitude, this.destinationLongitude))
.width(1)
.color(Color.DKGRAY)
);
}
@Override
public void onLocationChanged(Location location) {
List<LatLng> pointsToDestination = new ArrayList<LatLng>();
pointsToDestination.add(new LatLng(location.getLatitude(), location.getLongitude()));
pointsToDestination.add(new LatLng(destinationLatitude, destinationLongitude));
this.routeToDestination.setPoints(pointsToDestination);
}
}
Upvotes: 18
Views: 23906
Reputation: 333
The Simplest way to do that
implementation 'com.akexorcist:google-direction-library:1.2.1'
Use this library Get an API key from
https://developers.google.com/maps/documentation/directions/get-api-key
You must enable the billing method for this for direction API.
https://console.cloud.google.com/projectselector2/billing/enable
Set this code in any function but not in the onmapready function
String serverKey = "YOUR_API_KEY";
GoogleDirection.
GoogleDirection.withServerKey(serverKey)
.from(new LatLng(30.677717,73.106812))
.to(new LatLng(31.345394,73.429810))
.transitMode(TransportMode.WALKING)
.unit(Unit.METRIC)
.execute(new DirectionCallback() {
@Override
public void onDirectionSuccess(@Nullable Direction direction) {
Log.d("eeeeee", direction.getStatus());
if(direction.isOK()) {
// Do something
Route route = direction.getRouteList().get(0);
Leg leg = route.getLegList().get(0);
ArrayList<LatLng> directionPositionList = leg.getDirectionPoint();
Log.d("eeeeee", directionPositionList.size()+"eeeeee");
if (polylineOptions_final!=null)
{
polylineOptions_final.remove();
}
PolylineOptions polylineOptions = DirectionConverter.createPolyline(getApplication(), directionPositionList, 10, Color.RED);
polylineOptions_final = mMap.addPolyline(polylineOptions);
} else {
// Do something
}
// Log.d("eeeeee"+direction.getGeocodedWaypointList().toString(), "onDirectionSuccess: ");
}
@Override
public void onDirectionFailure(@NonNull Throwable t) {
Toast.makeText(ClientMap.this, "d"+t.getMessage().toLowerCase(), Toast.LENGTH_SHORT).show();
}
});
new LatLng(31.345394,73.429810));
Upvotes: 1
Reputation: 333
Make a Member variable in map activity of polyline
Polyline polyline_final = null;
Every time when a polyline is added to map than assign value of map like this
polyline_final = mMap.addPolyline(polylineOptions);
And next to make new polyline at same time remove the previous polyline form map Apply this if condition before polyline_final = mMap.addPolyline(polylineOptions);
if (polylineOptions_final!=null)
{
polylineOptions_final.remove();
}
Now this is done
Upvotes: 0
Reputation: 13936
To remove a polyline you should simply use remove() method as stated in the API.
//Add line to map
Polyline line = mMap.addPolyline(new PolylineOptions()
.add(new LatLng(location.getLatitude(), location.getLongitude()),
new LatLng(this.destinationLatitude, this.destinationLongitude))
.width(1)
.color(Color.DKGRAY)
//Remove the same line from map
line.remove();
Upvotes: 47