I have searched all over and I haven't found an answer. Similar to this
How can I change the color to the alternate route? urlDestination +"&alternatives=true"
Adding that code will display the shortest route and alternate routes. Problem is, I don't know how to change the color of the alternate route to a specific color.
Example: Shortest route should be blue and alternate routes should be grey.
Help is much needed.
Something like this... that the alternate routes should be grey
Check this Tutorial
You Need to add BelowLine.
polyLineOptions.color(Color.BLUE);
Hope this may help.
It's pretty straight forward. I believe you have a list or array of Routes.
Iterate through that to find the min distance index. (usually it's the first one, but just to make sure).
int minDistanceIndex = 0;
int minDistance = Integer.MAX_VALUE;
for(int i = 0; i < routes.size(); i++){
Route route = routes.get(i);
int distance = route.getDistanceValue();
if(distance < minDistance){
minDistance = distance;
minDistanceIndex = i;
}
}
Now use the minDistanceIndex to show the default route(blue) and others as gray like below.
PolylineOptions lineOptions;
for (int i = 0; i < routes.size(); i++) {
points = routes.get(i).getPoints();
lineOptions = new PolylineOptions();
// Adding all the points in the route to LineOptions
lineOptions.addAll(points);
if(minDistanceIndex != i) {
lineOptions.width(15);
lineOptions.color(ContextCompat.getColor(getActivity(), android.R.color.darker_gray));
}
lineOptions.clickable(true);
// Drawing polyline in the Google Map for the i-th route
if(map != null) {
polylines.add(map.addPolyline(lineOptions));
map.setOnPolylineClickListener(polylineListener);
}
}
//finally draw the shortest route
lineOptions = new PolylineOptions();
lineOptions.width(18);
lineOptions.color(ContextCompat.getColor(getActivity(), android.R.color.holo_blue_dark));
// Drawing polyline in the Google Map for the i-th route
if(map != null) {
polylines.add(map.addPolyline(lineOptions));
}
You can add an on click listener to polylines to change the color when user selects another polyline just like in Google Maps.
IMP: You need to draw the shortest route at last cause otherwise part of that route can be overlapped by alternate route, hence leaving you with part blue and part gray route.
Hope this helps!
Related
We are drawing polyline according to different zone colors (Different colors assigned to the different zone where the person is in) and the below issue, we are facing constantly. Is anyone know the solution?
Getting issue on line no 8: polyline.setPoints(points) where points contain array of lat/lng. eg:lat/lng: (34.282619,-119.149895) AlexMamo
PolylineOptions options = new PolylineOptions();
Polyline polyline = null;
for (int i = 1; i < coloredPointArrayLit.size(); i++) {
if (coloredPointArrayLit.get(i).color == coloredPointArrayLit.get(i - 1).color) {
if (polyline != null) {
List<LatLng> points = polyline.getPoints();
points.add(coloredPointArrayLit.get(i).coordinates);
polyline.setPoints(points);
} else {
options.add(coloredPointArrayLit.get(i - 1).coordinates);
options.add(coloredPointArrayLit.get(i).coordinates);
options.color(coloredPointArrayLit.get(i).color);
options.jointType(JointType.ROUND);
options.endCap(roundCap);
options.startCap(roundCap);
// Add the polyline to the map colored polyline
polyline = this.googleMap.addPolyline(options
.width(DensityTool.adjustToDensity(context, 7)));
}
} else {
options = new PolylineOptions();
options.add(coloredPointArrayLit.get(i - 1).coordinates);
options.add(coloredPointArrayLit.get(i).coordinates);
polyline = null;
}
}
Any help will be appreciated.
You are using new PolyLineOptions instance for each and every line you draw to the maps. This is make the drawing slow.
Only use one instance of polyline options and use only the .add(LatLng) function inside the loops.
Please refer this link: https://stackoverflow.com/a/39616306/4896829
Am working on an Android project where I want to draw route between 2 point on Google Map. I have successfully drawn route between source and destination point. But I have 1 problem in that, i.e. Some times I want to draw a path between more than 2 point, That time the code that I have written is drawing route between first and the last position and leaving the mid point position. What I exactly want is my route should go through the mid point to the destination point. How can I achieve this?
You can separately draw routes between three point. So, first draw the route from starting point to mid point then from mid point to end point. Then if you want add the required markers.
You can use PolylineOptions from GoogleMaps API Dcoumented Here and keep adding all the points which should be a part of your route. You can do something like this
ArrayList<LatLng> points;
PolylineOptions lineOptions = null;
// Traversing through all the routes
for (int i = 0; i < result.size(); i++) {
points = new ArrayList<>();
lineOptions = new PolylineOptions();
// Fetching i-th route
List<HashMap<String, String>> path = result.get(i);
// Fetching all the points in i-th route
for (int j = 0; j < path.size(); j++) {
HashMap<String, String> point = path.get(j);
double lat = Double.parseDouble(point.get("lat"));
double lng = Double.parseDouble(point.get("lng"));
LatLng position = new LatLng(lat, lng);
points.add(position);
}
// Adding all the points in the route to LineOptions
lineOptions.addAll(points);
lineOptions.width(10);
lineOptions.color(Color.RED);
}
// Drawing polyline in the Google Map for the i-th route
if(lineOptions != null) {
mMap.addPolyline(lineOptions);
}
Hope this solves your problem.
I draw routes on my google map using next code:
private void drawRoute(List<Route> route) {
for (int i = route.size() - 1; i >= 0 ; i--) {
final Route currentRoute = route.get(i);
final PolylineOptions polyOptions = new PolylineOptions();
polyOptions.color(Color.parseColor(allColors[inc_color]));
polyOptions.width(10);
polyOptions.addAll(currentRoute.getPoints());
mMap.addPolyline(polyOptions);
}
}
After this step I want to get from my SQLite database all PoI that are at max 5 km far from my routes and to draw them on map.
How can I filter my database as result to show all locations that are located at max 5 km along the route?
Maybe getting the bounds of route?
I am trying to plot a complex polygon around a route, following its steps with a given radius. To do so I drew 50-sided uniform polygons (which are practically circles) around each step (coordinate) of the route. Now I obtain a set of coordinates of all the plotted circles around the route, and I can see them on the map but they are overlapped which is not very good looking, and it's not a good practice to add such a huge number of overlays on the map.
So what I need to do now is to merge all the polygons I have now into one polygon and plot it in the map.
I tried deleting the intersection points of every two polygons (testing if points of polygon1 lie inside polygon2 and vice versa) and merging all the rest of the coordinates in one array and then construct my new polygon but it didn't work. Here's a code snippet of how I'm doing this:
public ArrayList<PolygonOptions> polygons = new ArrayList<>();
// lineOptions is the set of route coordinates
for (int i = 0; i < lineOptions.getPoints().size() - 1; i++) {
// Draw a circle around each point of the route
PolygonOptions circle1 = drawCircle(lineOptions.getPoints().get(i), 0.1);
PolygonOptions circle2 = drawCircle(lineOptions.getPoints().get(i + 1), 0.1);
// Draw a convex hull between every two successive circles
PolygonOptions convexHull = convexHull(circle1, circle2);
convexHull.strokeWidth(0);
convexHull.fillColor(0x7F729E47);
activity.range.add(activity.mMap.addPolygon(convexHull));
polygons.add(convexHull);
}
if (polygons.size() == 1) {
pts.addAll(polygons.get(0).getPoints());
} else {
for (int i = 0; i < polygons.size() - 1; i++) {
ArrayList<LatLng> pts1 = new ArrayList<>();
ArrayList<LatLng> pts2 = new ArrayList<>();
pts1.addAll(polygons.get(i).getPoints());
pts2.addAll(polygons.get(i + 1).getPoints());
for (int j = 0; j < pts1.size(); j++) {
if (pointInPolygon(pts1.get(j), pts2)) {
pts1.remove(j);
}
}
for (int j = 0; j < pts2.size(); j++) {
if (pointInPolygon(pts2.get(j), pts1)) {
pts2.remove(j);
}
}
pts.addAll(pts1);
pts.addAll(pts2);
}
}
// This part didn't work
// PolygonOptions range = new PolygonOptions();
// range.addAll(pts);
// range.strokeWidth(0);
// range.fillColor(0x7F729E47);
// activity.range.add(activity.mMap.addPolygon(range));
Following #antonio's directions, and using JTS Topology Suit I was able to plot a polygon around the route (buffer the route) with a defined radius. When I used the buffer function in the JTS library I obtained a buffer of the route but the caps were oval, I read about it and this happens because the computed coordinates are projected on the earth's map which is not flat, the caps get more oval when the route is closer to the earth's poles, and more round when closer to the equator line (0ยบ latitude). Anyway, I used another functionality from thhe library to union all the polygons that I already had in my question, and this was the result:
public ArrayList<Polygon> polys = new ArrayList<>();
//lineOptions is the set of route coordinates
for (int i = 0; i < lineOptions.getPoints().size() - 1; i++) {
// Draw a circle around each point of the route
PolygonOptions circle1 = drawCircle(lineOptions.getPoints().get(i), 0.1);
PolygonOptions circle2 = drawCircle(lineOptions.getPoints().get(i + 1), 0.1);
// Draw a convex hull between every two successive circles
PolygonOptions convexHull = convexHull(circle1, circle2);
List<LatLng> latLngs = convexHull.getPoints();
List<Coordinate> coords = new ArrayList<>();
for (int j=0; j<latLngs.size(); j++) {
coords.add(new Coordinate(latLngs.get(j).latitude, latLngs.get(j).longitude));
if(j==latLngs.size()-1)
coords.add(new Coordinate(latLngs.get(0).latitude, latLngs.get(0).longitude));
}
Coordinate[] coordinates = coords.toArray(new Coordinate[coords.size()]);
GeometryFactory fact = new GeometryFactory();
LinearRing linear = new GeometryFactory().createLinearRing(coordinates);
Polygon poly = new Polygon(linear, null, fact);
polys.add(poly);
}
PolygonOptions combine = combineIntoOnePolygon(polys);
combine.strokeWidth(0);
combine.fillColor(0x7F729E47);
activity.range = activity.mMap.addPolygon(combine);
The function that does the combining:
static PolygonOptions combineIntoOnePolygon(Collection<Polygon> geometries ){
Geometry all = null;
PolygonOptions allPolygon = new PolygonOptions();
for(Iterator<Polygon> i = geometries.iterator(); i.hasNext(); ){
Polygon geometry = i.next();
if( geometry == null ) continue;
if( all == null ){
all = geometry;
}
else {
all = all.union( geometry );
}
}
List<Coordinate> bufferCoordinates = Arrays.asList(all.getCoordinates());
for (Coordinate c : bufferCoordinates) {
allPolygon.add(new LatLng(c.x, c.y));
}
return allPolygon;
}
You need to compute the buffer of the line. According to Wikipedia:
A buffer is an area defined by the bounding region determined by a set of points at a specified maximum distance from all nodes along segments of an object.
The buffer of a geometry is computed using the Minkowski sum. The Minkowsky sum takes two polygons (one is your polyline and the other is a circle if you want rounded caps) and moves the second one throught the path of the first one.
You can find some concrete implementations of the Minkowski sum that you can study. For example https://github.com/perelo/computational-geometry/blob/master/src/computational_geometry/model/algorithms/Minkowski.java
Following
How to draw a path between two markers
I had to add lot of polylines between two markers, to make a path.
One of the markers is draggable, lets say source is draggable.
So, when user starts dragging the marker, the path previously drawn must be erased and a new path between new source and destination must be draw.
I am able to draw the new path, but how can i erase the previous path?
This is how the path is drawn:
for (int z = 0; z < list.size() - 1; z++) {
LatLng src = list.get(z);
LatLng dest = list.get(z + 1);
Polyline line = map.addPolyline(new PolylineOptions()
.add(new LatLng(src.latitude, src.longitude),
new LatLng(dest.latitude, dest.longitude))
.width(2).color(Color.RED).geodesic(true));
}
One solution i can get is
map.clear();
To clear all the polylines, markers etc.. and add the markers again, then drawn the path.
But as soon as I start dragging, the marker is cleared, hence not visible on the map :(
Thank You
Keep track of the Polyline as you add it to the map:
Polyline polyline = this.mMap.addPolyline(new PolylineOptions().....);
Then when you want to remove it:
polyline.remove();
If you have lots of Polylines, just add them to a List as they are put on the map:
List<Polyline> polylines = new ArrayList<Polyline>();
for(....)
{
polylines.add(this.mMap.addPolyline(new PolylineOptions()....));
}
And when you want to delete:
for(Polyline line : polylines)
{
line.remove();
}
polylines.clear();
The key is to keep a reference to the Polyline objects and call .remove() on each one.
I know this is very old question but I noticed that this is very common need. I found another way and I wanted to share it.
Here is the basic idea:
Polyline polylineFinal;
PolylineOptions polylineOptions;
loop {
polylineOptions.add( new LatLng( latitude, longitude ) );
}
polylineOptions.width(2);
polylineOptions.color(Color.RED);
polylineOptions.geodesic(true);
polylineFinal = map.addPolyline (polylineOptions);
Map's "addPolyline" method returns a single polyline which contains all the points. When I need to remove the points, I call polylineFinal's "remove" method.
polylineFinal.remove();
Update
If you want to removes all markers, polylines, polygons, overlays, etc from the map use
mMap.clear(); //it will remove all additional objects from map
and if you only want to remove all or single polyline, polygon, marker etc see #DiskDev Answer above. In this case you must keep track of every single additional object you add to map