Select platform: [Android](https://developers.google.com/maps/documentation/android-sdk/dds-boundaries/style-polygon.md.txt "View this page for the Android platform docs.") [iOS](https://developers.google.com/maps/documentation/ios-sdk/dds-boundaries/style-polygon.md.txt "View this page for the iOS platform docs.") [JavaScript](https://developers.google.com/maps/documentation/javascript/dds-boundaries/style-polygon.md.txt "View this page for the JavaScript platform docs.")

<br />

To apply styles for stroke and fill to boundary polygons in a feature layer:

1. Create a style factory function that implements the
   [`FeatureLayer.StyleFactory`](https://developers.google.com/android/reference/com/google/android/gms/maps/model/FeatureLayer.StyleFactory.md.txt)
   interface. This function defines the styling logic for a feature layer.

2. Call
   [`FeatureLayer.setFeatureStyle()`](https://developers.google.com/android/reference/com/google/android/gms/maps/model/FeatureLayer.md.txt#setFeatureStyle(com.google.android.gms.maps.model.FeatureLayer.StyleFactory))
   to apply the style factory function to the feature layer.

The following example map demonstrates highlighting the boundary polygon for a
single region in a Locality feature layer.

![A screenshot showing Hana Hawaii polygon.](https://developers.google.com/static/maps/documentation/android-sdk/images/hana-hi-polygon.png)

## Create a style factory function

The style factory function is applied to every polygon in the affected feature
layer at the time you set the function on the feature layer. This function must
return a [`FeatureStyle`](https://developers.google.com/android/reference/com/google/android/gms/maps/model/FeatureStyle.md.txt)
object that specifies how to style the polygon.

> [!NOTE]
> **Note:** If you update or modify the style factory function for a feature layer, you must call `FeatureLayer.setFeatureStyle()` again to reset the function on the feature layer.

Maps SDK for Android passes a
[`Feature`](https://developers.google.com/android/reference/com/google/android/gms/maps/model/Feature.md.txt)
instance to the style factory function. The `Feature` instance represents the
feature's metadata, giving you access to the metadata in the style factory
function.

The style factory function should always return consistent results when it is
applied. For example, if you wanted to randomly color a set of features, the
random part shouldn't take place in the feature style function, as that would
cause unintended results.

Because this function runs over every feature in a layer, optimization is
important. To avoid impacting rendering times:

- Enable only the feature layers you need.

- Call `FeatureLayer.setFeatureStyle(null)` when a feature layer is no longer
  in use.

### Set polygon stroke and fill

When styling a boundary polygon in the style factory function, you can set the:

- **Stroke color and opacity** of the polygon border in the ARGB color format,
  as defined by the
  [`Color`](https://developer.android.com/reference/android/graphics/Color.html)
  class. The default value is transparent (0x00000000).

- **Stroke width** of the polygon border in screen pixels. The default value
  is 2.

- **Fill color and opacity** of the polygon in the ARGB color format, as
  defined by the
  [`Color`](https://developer.android.com/reference/android/graphics/Color.html)
  class. The default value is transparent (0x00000000).

### Lookup place IDs to target features

Many applications apply styles to a feature based on the feature location. For
example, you might want to apply styling to different countries, territories, or
regions. The feature location is represented by a
[place ID](https://developers.google.com/maps/documentation/places/android-sdk/place-id.md.txt).

Place IDs uniquely identify a place in the Google Places database and on Google
Maps. To get a place ID:

- [Use Places APIs and Geocoding](https://developers.google.com/maps/documentation/android-sdk/dds-boundaries/dds-use-maps-places-apis.md.txt) to search for regions by name, and get place IDs for regions within specified bounds.
- [Get data from click events](https://developers.google.com/maps/documentation/android-sdk/dds-boundaries/handle-events.md.txt). This returns the Feature corresponding to a region clicked, which provides access to its place ID and feature type category.

Coverage varies by region. See
[Google boundaries coverage](https://developers.google.com/maps/documentation/android-sdk/dds-boundaries/coverage.md.txt)
for details.

Geographic names are available from many sources, such as the
[USGS Board on Geographic Names](https://edits.nationalmap.gov/apps/gaz-domestic/public/search/names),
and the
[U.S. Gazetteer Files](https://www.census.gov/geographies/reference-files/time-series/geo/gazetteer-files.html).

### Use PlaceFeature to get a place ID

The [`PlaceFeature`](https://developers.google.com/android/reference/com/google/android/gms/maps/model/PlaceFeature.md.txt)
class is a subclass of the `Feature` class.
It represents a *place feature* (a feature with a place ID) which includes
features of type `ADMINISTRATIVE_AREA_LEVEL_1`, `ADMINISTRATIVE_AREA_LEVEL_2`,
`COUNTRY`, `LOCALITY`, `POSTAL_CODE`, and `SCHOOL_DISTRICT`.

When the place ID is available, the
Maps SDK for Android passes an instance of `PlaceFeature` to the style factory
function so that you can determine the location of the feature.

## Style factory example

This example applies a style factory function to a polygon in the Locality
feature layer. The style factory function determines the place ID of the feature
by using the `PlaceFeature` instance. If the place ID is for Hana, Hawaii then
the function applies a custom fill and stroke style to the polygon:

1. If you haven't already done so, follow the steps in
   [Get Started](https://developers.google.com/maps/documentation/android/dds-boundaries/start)
   to create a new map ID and map style. Be sure to enable the **Locality**
   feature layer.

2. Get a reference to the Locality feature layer when the map initializes.

   ### Java


   ```java
   private FeatureLayer localityLayer;

   @Override
   public void onMapReady(GoogleMap map) {
     // Get the LOCALITY feature layer.
     localityLayer = map.getFeatureLayer(new FeatureLayerOptions.Builder()
       .featureType(FeatureType.LOCALITY)
       .build());

     // Apply style factory function to LOCALITY layer.
     styleLocalityLayer();
   }
   ```

   <br />

   ### Kotlin


   ```java
   private var localityLayer: FeatureLayer? = null

   override fun onMapReady(googleMap: GoogleMap) {
     // Get the LOCALITY feature layer.
     localityLayer = googleMap.getFeatureLayer(FeatureLayerOptions.Builder()
       .featureType(FeatureType.LOCALITY)
       .build())

     // Apply style factory function to LOCALITY layer.
     styleLocalityLayer()
   }
   ```

   <br />

3. Create a style factory function and apply it to the Locality
   feature layer.

   The following example only applies the function if the place
   ID of the feature is for Hana, Hawaii ("ChIJ0zQtYiWsVHkRk8lRoB1RNPo").
   If the specified place ID is not for Hana, Hawaii then the style is not
   applied.

   ### Java


   ```java
   private void styleLocalityLayer() {

     // Create the style factory function.
     FeatureLayer.StyleFactory styleFactory = (Feature feature) -> {

       // Check if the feature is an instance of PlaceFeature,
       // which contains a place ID.
       if (feature instanceof PlaceFeature) {
         PlaceFeature placeFeature = (PlaceFeature) feature;

         // Determine if the place ID is for Hana, HI.
         if (placeFeature.getPlaceId().equals("ChIJ0zQtYiWsVHkRk8lRoB1RNPo")) {

           // Use FeatureStyle.Builder to configure the FeatureStyle object
           // returned by the style factory function.
           return new FeatureStyle.Builder()
             // Define a style with purple fill at 50% opacity and solid purple border.
             .fillColor(0x80810FCB)
             .strokeColor(0xFF810FCB)
             .build();
         }
       }
       return null;
     };

     // Apply the style factory function to the feature layer.
     localityLayer.setFeatureStyle(styleFactory);
   }
   ```

   <br />

   ### Kotlin


   ```java
   private fun styleLocalityLayer() {

     // Create the style factory function.
     val styleFactory = FeatureLayer.StyleFactory { feature: Feature ->

         // Check if the feature is an instance of PlaceFeature,
         // which contains a place ID.
         if (feature is PlaceFeature) {
             val placeFeature: PlaceFeature = feature as PlaceFeature

             // Determine if the place ID is for Hana, HI.
             if (placeFeature.getPlaceId().equals("ChIJ0zQtYiWsVHkRk8lRoB1RNPo")) {

                 // Use FeatureStyle.Builder to configure the FeatureStyle object
                 // returned by the style factory function.
                 return@StyleFactory FeatureStyle.Builder()
                     // Define a style with purple fill at 50% opacity and
                     // solid purple border.
                     .fillColor(0x80810FCB.toInt())
                     .strokeColor(0xFF810FCB.toInt())
                     .build()
             }
         }
         return@StyleFactory null
     }

     // Apply the style factory function to the feature layer.
     localityLayer?.setFeatureStyle(styleFactory)
   }
   ```

   <br />

## Remove styling from a layer

To remove styling from a layer, call `FeatureLayer.setFeatureStyle(null)`.