Gradient polyline in google maps Android

523 views Asked by At

I am trying to add a gradient polyline while showing routes in android maps. I tried using addSpan but this is creating a black line.

lineOption.addSpan(
                    StyleSpan(
                        StrokeStyle.gradientBuilder(
                            Color.RED,
                            Color.YELLOW
                        ).build()
                    )
                )

can anyone help me figure out this? I am new to using google maps in android

1

There are 1 answers

1
ChrisWolfDev On

Your need to enable the new map renderer to draw gradient polylines.

The use the new map renderer create an application class:

https://developers.google.com/maps/documentation/android-sdk/renderer?_ga=2.28370680.-55268474.1677776288

import com.google.android.gms.maps.MapsInitializer;
import com.google.android.gms.maps.MapsInitializer.Renderer;
import com.google.android.gms.maps.OnMapsSdkInitializedCallback;

class MyApplication extends Application implements OnMapsSdkInitializedCallback {

  @Override
  public void onCreate() {
    super.onCreate();
    MapsInitializer.initialize(getApplicationContext(), Renderer.LATEST, this);
  }

  @Override
  public void onMapsSdkInitialized(MapsInitializer.Renderer renderer) {
    switch (renderer) {
      case LATEST:
        Log.d("MapsDemo", "The latest version of the renderer is used.");
        break;
      case LEGACY:
        Log.d("MapsDemo", "The legacy version of the renderer is used.");
        break;
    }
  }
}

Make sure to set your application class in the AndroidManifest:

<application
    android:name=".MyApplication"
    ...

Make sure to check the log if "The latest version of the renderer is used." is there. For the new render the device needs:

  • Android 5.0 (API level 21) or later
  • 2 GB or more of data storage
  • using Google Play services version 21.39.14 or later

so might not work on your emulator