letmapView=GMSMapView(frame:.zero,mapID:GMSMapID(identifier:"YOUR_MAP_ID"),camera:GMSCameraPosition(latitude:40.7,longitude:-74.0,zoom:12))letlayer=mapView.datasetFeatureLayer(of:"YOUR_DATASET_ID")// Define a style with green fill and stroke.// Apply the style to all features in the dataset.layer.style={featureinletstyle=MutableFeatureStyle()style.fillColor=.green.withAlphaComponent(0.1)style.strokeColor=.greenstyle.strokeWidth=2.0returnstyle}
Objective-C
GMSMapView*mapView=[GMSMapViewmapWithFrame:CGRectZeromapID:[GMSMapIDmapIDWithIdentifier:@"MAP_ID"]camera:[GMSCameraPositioncameraWithLatitude:40.7longitude:-74.0zoom:12]];GMSDatasetFeatureLayer*layer=[mapViewdatasetFeatureLayerOfDatasetID:@"YOUR_DATASET_ID"];// Define a style with green fill and stroke.// Apply the style to all features in the dataset.layer.style=^(GMSDatasetFeature*feature){GMSMutableFeatureStyle*style=[GMSMutableFeatureStylestyle];style.fillColor=[[UIColorgreenColor]colorWithAlphaComponent:0.1];style.strokeColor=[UIColorgreenColor];style.strokeWidth=2.0;returnstyle;};
layer.style={featureinvarattributeColor:String=feature.datasetAttributes["highlightColor"]// Conditionalize styling based on the value of the "highlightColor" attribute....}
Objective-C
// Apply the style to a single dataset feature.layer.style=^(GMSDatasetFeature*feature){NSString*attributeColor=feature.datasetAttributes[@"highlightColor"];// Conditionalize styling based on the value of the "highlightColor" attribute....};
[null,null,["最后更新时间 (UTC):2025-08-31。"],[[["\u003cp\u003eThis guide demonstrates how to add a dataset to a Google Map and style its features using the iOS SDK.\u003c/p\u003e\n"],["\u003cp\u003eBefore you begin, ensure you have a map ID, map style, and dataset ID created in the Google Cloud console.\u003c/p\u003e\n"],["\u003cp\u003eTo style the dataset, associate it with your map style through the Google Cloud console and then apply a style function to the dataset feature layer in your code.\u003c/p\u003e\n"],["\u003cp\u003eStyling options include setting stroke, fill, and point radius, and you can define simple or declarative style rules based on feature attributes.\u003c/p\u003e\n"],["\u003cp\u003eTo remove styling, set the \u003ccode\u003estyle\u003c/code\u003e property of the dataset feature layer to \u003ccode\u003enil\u003c/code\u003e.\u003c/p\u003e\n"]]],["To add a dataset to a map, first associate a dataset ID with a map style via the Google Cloud console. Then, apply styles to the dataset's features by using a styling closure. This closure defines stroke color/width, fill color, and point radius. Styles can be constant or dependent on feature attributes. To remove styling, set the layer's style to `nil`. A subset of the features can remain invisible by returning `nil`.\n"],null,["Select platform: [Android](/maps/documentation/android-sdk/dds-datasets/add-dataset-to-map \"View this page for the Android platform docs.\") [iOS](/maps/documentation/ios-sdk/dds-datasets/add-dataset-to-map \"View this page for the iOS platform docs.\") [JavaScript](/maps/documentation/javascript/dds-datasets/add-dataset-to-map \"View this page for the JavaScript platform docs.\")\n\n\u003cbr /\u003e\n\nThis page shows you how to add a dataset to a map, and apply styling.\n\nPrerequisites\n\nBefore proceeding, you should have a map ID and map style, and a dataset ID.\n\n- [Create a map ID and map style](/maps/documentation/ios-sdk/dds-datasets/start#create_a_map_id)\n- [Create and upload a dataset](/maps/documentation/ios-sdk/dds-datasets/create-dataset)\n\n | **Note:** You need the ID of your dataset to initialize your app.\n\nAssociate a dataset ID with a map style\n\nTo style the features of a dataset, you apply a style function to the\n*dataset feature layer* of a map. The dataset feature layer is created when you\n*associate a dataset with a map style*.\n| **Experimental:** This feature can only be set for light map styles. When you link a light map style that has this feature enabled to a [map\n| ID](../map-ids/mapid-over), the enabled layers are also available for the dark map style.\n\nTake the following steps to associate your dataset with the map style you're\nusing:\n\n1. In the Google Cloud console, [go to the **Datasets** page](https://console.cloud.google.com/google/maps-apis/datasets).\n2. Click the name of the dataset. The **Dataset details** page appears.\n3. Click the **Preview** tab.\n4. In the **Associated map styles** section, click **ADD MAP STYLE** . \n5. Click the checkbox(es) for the Map Style(s) to associate and then click **SAVE**.\n\n| **Caution:** Rendering multiple datasets on a map can affect performance depending on the size and number of datasets.\n\nApply styles to the dataset\n\nTo style a feature of the dataset layer, use a styling closure that accepts a\n[`GMSDatasetFeature`](/maps/documentation/ios-sdk/reference/objc/Classes/GMSDatasetFeature) and returns a [`GMSFeatureStyle`](/maps/documentation/ios-sdk/reference/objc/Classes/GMSFeatureStyle) to define style\nattributes. Then set the style property to a styling closure, which contains\nstyling logic.\n\nThe styling closure is required to be deterministic and return consistent\nresults when it is applied. If any styling specifications of any feature are\nchanged, the style must be applied again.\n\nSet stroke, fill, and point radius\n\nWhen styling a feature in the style factory function, you can set the:\n\n- **Stroke color and opacity** of the border as defined by the `UIColor`\n class. The default value is transparent (`UIColor.clearColor`).\n\n- **Stroke width** of the border in screen pixels. The default value is 2.\n\n- **Fill color and opacity** as defined by the `UIColor` class. The default\n value is transparent (`UIColor.clearColor`).\n\n- **Point radius** of a point feature between 0 and 128 pixels.\n\n| **Note:** If you want a feature to be invisible, return `nil` from the style function for that feature.\n\nUse simple style rules\n\nThe simplest way to style features is to define constant style attributes such\nas color, opacity, and line width. Apply feature style options directly to a\ndataset feature layer, or use them in conjunction with custom styling. \n\nSwift \n\n```swift\nlet mapView = GMSMapView(frame: .zero, mapID: GMSMapID(identifier: \"YOUR_MAP_ID\"), camera: GMSCameraPosition(latitude: 40.7, longitude: -74.0, zoom: 12))\n\nlet layer = mapView.datasetFeatureLayer(of: \"\u003cvar translate=\"no\"\u003eYOUR_DATASET_ID\u003c/var\u003e\")\n\n// Define a style with green fill and stroke.\n// Apply the style to all features in the dataset.\nlayer.style = { feature in\n let style = MutableFeatureStyle()\n style.fillColor = .green.withAlphaComponent(0.1)\n style.strokeColor = .green\n style.strokeWidth = 2.0\n return style\n}\n```\n\nObjective-C \n\n```objective-c\nGMSMapView *mapView = [GMSMapView mapWithFrame:CGRectZero mapID:[GMSMapID mapIDWithIdentifier:@\"MAP_ID\"] camera:[GMSCameraPosition cameraWithLatitude: 40.7 longitude: -74.0 zoom:12]];\n\nGMSDatasetFeatureLayer *layer = [mapView datasetFeatureLayerOfDatasetID:@\"\u003cvar translate=\"no\"\u003eYOUR_DATASET_ID\u003c/var\u003e\"];\n\n// Define a style with green fill and stroke.\n// Apply the style to all features in the dataset.\nlayer.style = ^(GMSDatasetFeature *feature) {\n GMSMutableFeatureStyle *style = [GMSMutableFeatureStyle style];\n style.fillColor = [[UIColor greenColor] colorWithAlphaComponent:0.1];\n style.strokeColor = [UIColor greenColor];\n style.strokeWidth = 2.0;\n return style;\n};\n```\n\nUse declarative style rules\n\nYou can set style rules declaratively based on an attribute of the feature,\nand apply them across your entire dataset. You can return `nil` from your\nfeature style function, for example if you want a subset of features to remain\ninvisible.\n\nFor example, use the\n[`GMSDatasetFeature.datasetAttributes`](/maps/documentation/ios-sdk/reference/objc/Classes/GMSDatasetFeature#datasetattributes)\nto return the value of a dataset attribute for a feature. You can then customize\nstyling of the feature based on its attributes.\n\nThis example determines the value of the \"highlightColor\" attribute of each\nfeature of a dataset to control the styling: \n\nSwift \n\n```swift\nlayer.style = { feature in\n var attributeColor: String = feature.datasetAttributes[\"highlightColor\"]\n // Conditionalize styling based on the value of the \"highlightColor\" attribute.\n ...\n}\n```\n\nObjective-C \n\n```objective-c\n// Apply the style to a single dataset feature.\nlayer.style = ^(GMSDatasetFeature *feature) {\n NSString *attributeColor = feature.datasetAttributes[@\"highlightColor\"];\n // Conditionalize styling based on the value of the \"highlightColor\" attribute.\n ...\n};\n```\n\nRemove styling from a layer\n\nTo remove styling from a layer, set the `style` to `null`: \n\nSwift \n\n```swift\nlayer.style = nil\n```\n\nObjective-C \n\n```objective-c\nlayer.style = nil;\n```\n\nYou can also return `nil` from your feature style function, for example if you\nwant a subset of features to remain invisible."]]