设置边界多边形的样式
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
若要为边界多边形设置填充和描边样式,可以使用接受 GMSPlaceFeature
并返回 GMSFeatureStyle
的样式设置闭包来定义样式属性。然后将样式属性设置为包含样式设置逻辑的样式设置闭包。
Swift
let mapView = GMSMapView(frame: .zero, mapID: GMSMapID(identifier: "YOUR_MAP_ID"), camera: GMSCameraPosition(latitude: 20.773, longitude: -156.01, zoom: 12))
let layer = mapView.featureLayer(of: .locality)
// Define a style with purple
let style = FeatureStyle(fill: .purple.withAlphaComponent(0.5), stroke: .purple, strokeWidth: 3.0)
// Apply the style to a single boundary.
layer.style = { ($0.placeID == "ChIJ0zQtYiWsVHkRk8lRoB1RNPo"/* Hana, HI */) ? style : nil }
Objective-C
GMSMapView *mapView = [GMSMapView mapWithFrame:CGRectZero mapID:[GMSMapID mapIDWithIdentifier:@"MAP_ID"] camera:[GMSCameraPosition cameraWithLatitude: 20.773 longitude: -156.01 zoom:12]];
GMSFeatureLayer<GMSPlaceFeature *> *layer = [mapView featureLayerOfFeatureType:GMSFeatureTypeLocality];
// Define a style with purple fill and border.
GMSFeatureStyle *style = [GMSFeatureStyle styleWithFillColor:[[UIColor purpleColor] colorWithAlphaComponent:0.5] strokeColor:[UIColor purpleColor] strokeWidth:3.0];
// Apply the style to a single boundary.
layer.style = ^(GMSPlaceFeature *feature) {
return [feature.placeID isEqual:@"ChIJ0zQtYiWsVHkRk8lRoB1RNPo"/* Hana, HI */] ? style : nil;
};
如未另行说明,那么本页面中的内容已根据知识共享署名 4.0 许可获得了许可,并且代码示例已根据 Apache 2.0 许可获得了许可。有关详情,请参阅 Google 开发者网站政策。Java 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2025-08-31。
[null,null,["最后更新时间 (UTC):2025-08-31。"],[[["\u003cp\u003eThis documentation explains how to style the fill and stroke of boundary polygons using a styling closure that takes a \u003ccode\u003eGMSPlaceFeature\u003c/code\u003e and returns a \u003ccode\u003eGMSFeatureStyle\u003c/code\u003e.\u003c/p\u003e\n"],["\u003cp\u003eThe styling closure defines the style attributes for the polygon, such as fill color, stroke color, and stroke width.\u003c/p\u003e\n"],["\u003cp\u003eStyling Places data might require integrating the Places SDK for iOS, although it's not always mandatory.\u003c/p\u003e\n"],["\u003cp\u003eCode examples in Swift and Objective-C demonstrate how to apply a purple style to a specific boundary polygon using its place ID.\u003c/p\u003e\n"]]],[],null,["Select platform: [Android](/maps/documentation/android-sdk/dds-boundaries/style-polygon \"View this page for the Android platform docs.\") [iOS](/maps/documentation/ios-sdk/dds-boundaries/style-polygon \"View this page for the iOS platform docs.\") [JavaScript](/maps/documentation/javascript/dds-boundaries/style-polygon \"View this page for the JavaScript platform docs.\")\n\n\u003cbr /\u003e\n\nTo style the fill and stroke for a boundary polygon, use a styling closure that\naccepts a [`GMSPlaceFeature`](/maps/documentation/ios-sdk/reference/objc/Classes/GMSPlaceFeature) and returns a\n[`GMSFeatureStyle`](/maps/documentation/ios-sdk/reference/objc/Classes/GMSFeatureStyle) to define style attributes. Then set the\nstyle property to a styling closure, which contains styling logic.\n\n**Important:** While not required for all capabilities, styling of Places data may require integration with the [Places SDK for iOS](/maps/documentation/places/ios-sdk). \n\nSwift \n\n```swift\nlet mapView = GMSMapView(frame: .zero, mapID: GMSMapID(identifier: \"YOUR_MAP_ID\"), camera: GMSCameraPosition(latitude: 20.773, longitude: -156.01, zoom: 12))\n\nlet layer = mapView.featureLayer(of: .locality)\n\n// Define a style with purple\nlet style = FeatureStyle(fill: .purple.withAlphaComponent(0.5), stroke: .purple, strokeWidth: 3.0)\n\n// Apply the style to a single boundary.\nlayer.style = { ($0.placeID == \"ChIJ0zQtYiWsVHkRk8lRoB1RNPo\"/* Hana, HI */) ? style : nil }\n```\n\nObjective-C \n\n```objective-c\nGMSMapView *mapView = [GMSMapView mapWithFrame:CGRectZero mapID:[GMSMapID mapIDWithIdentifier:@\"MAP_ID\"] camera:[GMSCameraPosition cameraWithLatitude: 20.773 longitude: -156.01 zoom:12]];\n\nGMSFeatureLayer\u003cGMSPlaceFeature *\u003e *layer = [mapView featureLayerOfFeatureType:GMSFeatureTypeLocality];\n\n// Define a style with purple fill and border.\nGMSFeatureStyle *style = [GMSFeatureStyle styleWithFillColor:[[UIColor purpleColor] colorWithAlphaComponent:0.5] strokeColor:[UIColor purpleColor] strokeWidth:3.0];\n\n// Apply the style to a single boundary.\nlayer.style = ^(GMSPlaceFeature *feature) {\n return [feature.placeID isEqual:@\"ChIJ0zQtYiWsVHkRk8lRoB1RNPo\"/* Hana, HI */] ? style : nil;\n};\n```"]]