I have noticed that iOS has a built in function to draw dotted polylines using Google Maps, but I am unable to find the equivalent for Android. The idea is that iOS can set two colors, one being the color of the line and the other being the space between (transparent) and this will create dotted lines. iOS implementation looks like this
- (GMSPolyline )drawDashPath:(GMSPath ) path{
GMSPolyline *polyline = [GMSPolyline polylineWithPath:path];
polyline.map = self.googleMapView;
polyline.strokeWidth = 5.0;
NSArray *styles = @[[GMSStrokeStyle solidColor:[UIColor blueColor]],[GMSStrokeStyle solidColor:[UIColor clearColor]]];
NSArray *lengths = @[@5, @5];
polyline.spans = GMSStyleSpans(polyline.path, styles, lengths, kGMSLengthRhumb);
return polyline;
}
For Android, I have achieved dotted lines, but I had to do it using calculations and manipulation. Here is what I am doing for Android
// calculate the difference in latitude and longitude between start and end points
double difLat = latLngDest.latitude - latLngOrig.latitude;
double difLng = latLngDest.longitude - latLngOrig.longitude;
// retrieve current zoom level
double zoom = mMap.getCameraPosition().zoom;
// come up with distance between dotted lines that adjusts according to zoom levels
double divLat = difLat / (zoom * 1.2);
double divLng = difLng / (zoom * 1.2);
LatLng tmpLatOri = latLngOrig;
// add polyline to list
for (int i = 0; i < (zoom * 1.2); i++) {
LatLng loopLatLng = tmpLatOri;
if (i > 0) {
loopLatLng = new LatLng(tmpLatOri.latitude + (divLat * 0.40f), tmpLatOri.longitude + (divLng * 0.40f));
}
polylineList.add(mMap.addPolyline(new PolylineOptions()
.add(loopLatLng)
.add(new LatLng(tmpLatOri.latitude + divLat, tmpLatOri.longitude + divLng))
.color(Utils.getColor(mContext, R.color.orange))
.width(18f)));
tmpLatOri = new LatLng(tmpLatOri.latitude + divLat, tmpLatOri.longitude + divLng);
}
This looks OK, but I rather use a built in function if it exists. I have looked over the documentation and am unable to find anything for Android.
In February 2017 Google released a new set of customizations for polylines and polygons in Google Maps Android API v2. Particularly you can now create dashed and dotted polylines.
See information about new features in the Shapes Guide. See an example in the Polylines and Polygons tutorial.
You can also read the corresponding blog post here:
https://maps-apis.googleblog.com/2017/02/styling-and-custom-data-for-polylines.html