7

I have two locations and i am in need two draw polyline between these two location, I am done with drawing the polyline between these locations.

Issue is, that polyline is of one color but due to requirement i have to draw polyline of two different colors as shown in below:- enter image description here

if anyone have some meaningful code snippet or some suggestion to this issue...Thanks in advance

Santosh Yadav
  • 338
  • 2
  • 4
  • 15

2 Answers2

8

Since February 15, 2017 you can change the stroke line of a polyline. From the Release Notes (emphasys mine)

This release introduces custom styling for polylines and for the outlines of polygons and circles. Change the stroke pattern from a solid line (default) to your choice of dashes, dots, and gaps. In polylines and polygons, you can specify a bevel or round joint type to replace the default fixed miter joints. You can also change the cap at each end of a polyline from a butt (default) to a square or round cap, or specify a custom bitmap to be used as the cap. The styling of stroke patterns, joint types and start/end caps is available in the full API but not in lite mode.

Take into account that you will need to use Google Play Services 10.2 or above. Thus, in your build.gradle you will need to add:

dependencies {
    compile 'com.google.android.gms:play-services-maps:10.2.0'
}

You can specify the stroke pattern of your polyline but you can't change the color, so you will need to draw a solid polyline and a dashed polypine on top of it to reach your desired pattern (take into account that you will be drawing two polylines instead of just one and this can affect the performance):

List<LatLng> latLngs = new ArrayList<>();
// Add all your LatLngs to the List

// Draw a solid green polyline
mMap.addPolyline(new PolylineOptions()
        .addAll(latLngs)
        .color(Color.GREEN));

// Draw a dashed (60px spaced) blue polyline
List<PatternItem> dashedPattern = Arrays.asList(new Dash(60), new Gap(60));
mMap.addPolyline(new PolylineOptions()
        .addAll(latLngs)
        .pattern(dashedPattern)
        .color(Color.BLUE));

The result looks like this:

enter image description here

You can find more information about the new styling polyline feature here.

antonio
  • 18,044
  • 4
  • 45
  • 61
  • 1
    Thanks Buddy...You Nailed it..made my day ,i was searching for this from a long time and wasn't able to figure out the gap.thanks for your efforts...happy coding. – Santosh Yadav Mar 01 '17 at 09:05
-1
Random rnd = new Random(); 
  int color = Color.argb(255, rnd.nextInt(256), rnd.nextInt(256), rnd.nextInt(256));  

--- loop for each lat lng and add----

   mMap.addPolyline(new PolylineOptions()
  .add(new LatLng(lats, lons), new LatLng(late,lone))
  .width(5)
  .color(color)); 

Change the color coding based on your requirements

rohitanand
  • 710
  • 1
  • 4
  • 26
  • i have two specific colors green and blue, i have to use those two colors. as per ur code it will draw a polyline of random colors. – Santosh Yadav Feb 28 '17 at 12:12
  • 2
    As i mentioned it. use the color coding based on your requirements. Please don't expect to be spoon fed. Sorry. – rohitanand Feb 28 '17 at 12:25
  • 1
    thanks for mentioning spoon fed... but for you kind information i have already gone through this approach before posting this issue. buddy don't judge person by your assumptions and mind set. – Santosh Yadav Feb 28 '17 at 12:30