Using Maps SDK for Android v3.1.0 BETA

Version 3.1.0 beta of the Maps SDK for Android is a new implementation with the same API surface as the previous version (all classes and methods remain the same), and some new features for you to try:

  • Maps customization
  • Marker collision handling
  • Polyline customization

This guide shows you how to use the new features.

Maps customization (beta)

cloud-based maps styling provides a variety of tools and features that let you more easily customize and manage how your maps are styled. Rather than styling your map in code using the Maps APIs and SDKs, you can manage and style your maps in the Google Cloud Console. For more information, see Android Map Customization Overview.

Marker collision handling (beta)

You can specify whether custom markers should override default basemap labels when there is a collision, and to indicate relative priority between custom markers. For more information, see Marker Collision Handling (Android).

Polyline Customization (beta)

There are now several new ways to customize the appearance of polylines:

  • Multicolored polylines set polyline segments to different colors.
  • Gradient polylines color a polyline using a gradient of two colors.
  • Stamped polylines style a polyline using repeating bitmaps.

Creating a multicolored polyline

You can use spans to individually color segments of a polyline, by creating StyleSpan objects, and adding them to PolylineOptions using the addSpan() or addSpans() methods. By default, each item in the array will set the color of the corresponding line segment. The following example shows setting segment colors to create a polyline with red and green segments:

Kotlin



val line = map.addPolyline(
    PolylineOptions()
        .add(LatLng(47.6677146, -122.3470447), LatLng(47.6442757, -122.2814693))
        .addSpan(StyleSpan(Color.RED))
        .addSpan(StyleSpan(Color.GREEN))
)

      

Java


Polyline line = map.addPolyline(new PolylineOptions()
        .add(new LatLng(47.6677146,-122.3470447), new LatLng(47.6442757,-122.2814693))
        .addSpan(new StyleSpan(Color.RED))
        .addSpan(new StyleSpan(Color.GREEN)));

      

Creating a gradient polyline

You can define a gradient by specifying two 32-bit alpha-red-green-blue (ARGB) integers, to specify the beginning and ending colors of the stroke. Set this property on the shape's options object by calling PolylineOptions.addSpan(). The following example shows creating a red to yellow gradient polyline from Woodland Park Zoo to Kirkland, WA.

Kotlin



val line = map.addPolyline(
    PolylineOptions()
        .add(LatLng(47.6677146, -122.3470447), LatLng(47.6442757, -122.2814693))
        .addSpan(
            StyleSpan(
                StrokeStyle.gradientBuilder(
                    Color.RED,
                    Color.YELLOW
                ).build()
            )
        )
)

      

Java


Polyline line = map.addPolyline(new PolylineOptions()
        .add(new LatLng(47.6677146,-122.3470447), new LatLng(47.6442757,-122.2814693))
        .addSpan(new StyleSpan(StrokeStyle.gradientBuilder(Color.RED, Color.YELLOW).build())));

      

Creating a stamped polyline

You can set the appearance of a polyline to a repeating bitmap texture. To do this, create a StampStyle of TextureStyle, then set this property on the shape's options object by calling PolylineOptions.addSpan() as shown here:

Kotlin



val stampStyle =
    TextureStyle.newBuilder(BitmapDescriptorFactory.fromResource(R.drawable.walking_dot)).build()
val span = StyleSpan(StrokeStyle.colorBuilder(Color.RED).stamp(stampStyle).build())
map.addPolyline(
    PolylineOptions()
        .add(LatLng(47.6677146, -122.3470447), LatLng(47.6442757, -122.2814693))
        .addSpan(span)
)

      

Java


StampStyle stampStyle =
        TextureStyle.newBuilder(BitmapDescriptorFactory.fromResource(R.drawable.walking_dot)).build();
StyleSpan span = new StyleSpan(StrokeStyle.colorBuilder(Color.RED).stamp(stampStyle).build());
map.addPolyline(new PolylineOptions()
        .add(new LatLng(47.6677146,-122.3470447), new LatLng(47.6442757,-122.2814693))
        .addSpan(span));

      

Import the beta-compatible utility library

If you are using the Google Maps Android API utility library, you will need to also update your project dependencies to replace the existing version with the beta-compatible version by doing the following:

  1. Import the beta-compatible util library in your build.gradle file:
    implementation 'com.google.maps.android:android-maps-utils-v3:1.3.1'
    
  2. Remove the following from your build.gradle to remove non-beta-compatible utility library:
    implementation 'com.google.maps.android:android-maps-utils:1.3.1'
    

Run the samples

The Google Samples repository on GitHub includes sample apps that demonstrate the use of the v3.1.0 Beta Maps SDK for Android.