I know that quite a lot of time has passed since this was set, but there are still no gradient polylines (starting from recording ~ until 2015), and drawing several polylines doesn’t really cut (jagged edges, quite a bit of lag when working with several hundred points, it’s just not very visually attractive).
When I had to implement gradient polylines, what I ended up with was implementing TileOverlay , which would make the polyline on the canvas and then rasterize it (see this meaning for the specific code I wrote to do this https: //gist.github .com / Dagothig / 5f9cf0a4a7a42901a7b2 ).
The implementation does not try to make any selection from the viewport, because I did not need to achieve this, I wanted to (I am not sure about the numbers, but it was under the second on the tiles, and several tiles will be displayed at the same time).
Rendering a gradient polyline can be quite complicated because you are dealing with different viewports (positions and sizes): moreover, I hit several problems with limiting the accuracy of float at high zoom levels (20 +). In the end, I did not use scale and did not translate functions from the canvas, because I would get strange corruption problems.
Something else to make sure to use a similar data structure for what I had (latitude, longitude and timestamps) is that you need several segments for the gradient to display correctly (I ended up working with 3 points at a time).
For posterity, I also left the code from the bottom line: (forecasts are done using https://github.com/googlemaps/android-maps-utils if you are interested in where com.google.maps.android.projection.SphericalMercatorProjection comes from)
import android.content.Context; import android.graphics.Bitmap; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.LinearGradient; import android.graphics.Matrix; import android.graphics.Paint; import android.graphics.Shader; import com.google.android.gms.maps.model.LatLng; import com.google.android.gms.maps.model.Tile; import com.google.android.gms.maps.model.TileProvider; import com.google.maps.android.SphericalUtil; import com.google.maps.android.geometry.Point; import com.google.maps.android.projection.SphericalMercatorProjection; import java.io.ByteArrayOutputStream; import java.util.List; public class ColoredPolylineTileOverlay<T extends ColoredPolylineTileOverlay.PointHolder> implements TileProvider { public static final double LOW_SPEED_CLAMP_KMpH = 0; public static final double LOW_SPEED_CLAMP_MpS = 0;
Note that this implementation involves two relatively large things:
- polyline is already completed
- that there is only one polyline.
I would suggest that handling (1) would not be very difficult. However, if you intend to draw multiple polylines this way, you may need several ways to improve performance (keeping the bounding box of polylines so that you can easily discard those that are not suitable for the viewport, for one).
Another thing to keep in mind regarding the use of TileOverlay is that it is displayed after the movements are made, and not during; therefore, you can back up the overlay with the actual monochrome polyline below it to give it some continuity.
PS: this is the first time I'm trying to answer a question, so if there is something that I have to fix or do differently, tell me.