形状

请选择平台: Android iOS JavaScript

Maps SDK for iOS 为您提供了各种向地图添加形状的方法。可支持以下形状:

  • 多段线是一系列相连的线段,可组成您想要的任何形状,并可用于在地图上标记路径和路线。
  • 多边形:可用于在地图上标记区域的封闭形状。
  • 圆形是圆形在地球表面的准确地理位置投影。

您可以通过多种方式修改每个形状的外观。

多段线

您可以利用折线在地图上绘制线条。GMSPolyline 对象表示一系列有序的位置,显示为一系列线段。您可以使用 GMSStrokeStyle 设置多段线的颜色。

若要创建多段线,您需要指定其路径,方法是使用两个或多个点创建相应的 GMSMutablePath 对象。每个 CLLocationCoordinate2D 代表地球表面上的点。系统会按照您向路径添加点的顺序在各点之间绘制线段。您可以使用 addCoordinate:addLatitude:longitude: 方法向路径添加点。

Swift

let path = GMSMutablePath()
path.add(CLLocationCoordinate2D(latitude: -33.85, longitude: 151.20))
path.add(CLLocationCoordinate2D(latitude: -33.70, longitude: 151.40))
path.add(CLLocationCoordinate2D(latitude: -33.73, longitude: 151.41))
let polyline = GMSPolyline(path: path)
      

Objective-C

GMSMutablePath *path = [GMSMutablePath path];
[path addCoordinate:CLLocationCoordinate2DMake(-33.85, 151.20)];
[path addCoordinate:CLLocationCoordinate2DMake(-33.70, 151.40)];
[path addCoordinate:CLLocationCoordinate2DMake(-33.73, 151.41)];
GMSPolyline *polyline = [GMSPolyline polylineWithPath:path];
      

添加多段线

  1. 创建一个 GMSMutablePath 对象。
  2. 使用 addCoordinate:addLatitude:longitude: 方法设置路径中的点。
  3. 使用路径作为参数实例化新的 GMSPolyline 对象。
  4. 根据需要设置其他属性,例如 strokeWidthstrokeColor
  5. 设置 GMSPolylinemap 属性。
  6. 多边形现在将显示在地图上。

下面这段代码用于向地图添加矩形:

Swift

let rectanglePath = GMSMutablePath()
rectanglePath.add(CLLocationCoordinate2D(latitude: 37.36, longitude: -122.0))
rectanglePath.add(CLLocationCoordinate2D(latitude: 37.45, longitude: -122.0))
rectanglePath.add(CLLocationCoordinate2D(latitude: 37.45, longitude: -122.2))
rectanglePath.add(CLLocationCoordinate2D(latitude: 37.36, longitude: -122.2))
rectanglePath.add(CLLocationCoordinate2D(latitude: 37.36, longitude: -122.0))

let rectangle = GMSPolyline(path: path)
rectangle.map = mapView
      

Objective-C

GMSMutablePath *rectanglePath = [GMSMutablePath path];
[rectanglePath addCoordinate:CLLocationCoordinate2DMake(37.36, -122.0)];
[rectanglePath addCoordinate:CLLocationCoordinate2DMake(37.45, -122.0)];
[rectanglePath addCoordinate:CLLocationCoordinate2DMake(37.45, -122.2)];
[rectanglePath addCoordinate:CLLocationCoordinate2DMake(37.36, -122.2)];
[rectanglePath addCoordinate:CLLocationCoordinate2DMake(37.36, -122.0)];

GMSPolyline *rectangle = [GMSPolyline polylineWithPath:path];
rectangle.map = mapView;
      

矩形折线

移除多段线

您可以将 GMSPolylinemap 属性设置为 nil,以从地图中移除多段线。此外,通过调用 GMSMapView clear 方法,您也可以移除地图上的所有叠加层(包括多段线和其他形状)。

Swift

mapView.clear()
      

Objective-C

[mapView clear];
      

自定义多段线

GMSPolyline 对象提供了多个属性来控制线条的外观。它支持下列选项:

strokeWidth
整条线的宽度(以屏幕上的点为单位)。默认为1。该宽度不会随地图的缩放而缩放。
geodesic
如果设为 YES,此多段线边缘会以测地线形式渲染。测地线段沿地球表面沿着最短路径,在采用墨卡托投影法的地图上可能会显示为曲线。非测地线段在地图上绘制为直线。默认值为 NO
spans
用于指定多段线的一个或多个线段的颜色。spans 属性是 GMSStyleSpan 对象的数组。设置 spans 属性是更改多段线颜色的首选方式。
strokeColor
一个 UIColor 对象,用于指定多段线的颜色。默认值为 blueColor。如果设置了 spans,系统会忽略 strokeColor 属性。

以下代码段会使用测地插值,添加一条从墨尔本至珀斯的粗多段线。

Swift

let path = GMSMutablePath()
path.addLatitude(-37.81319, longitude: 144.96298)
path.addLatitude(-31.95285, longitude: 115.85734)
let polyline = GMSPolyline(path: path)
polyline.strokeWidth = 10.0
polyline.geodesic = true
polyline.map = mapView
      

Objective-C

GMSMutablePath *path = [GMSMutablePath path];
[path addLatitude:-37.81319 longitude:144.96298];
[path addLatitude:-31.95285 longitude:115.85734];
GMSPolyline *polyline = [GMSPolyline polylineWithPath:path];
polyline.strokeWidth = 10.f;
polyline.geodesic = YES;
polyline.map = mapView;
      

如需在将多段线添加到地图后对其进行修改,请务必保留 GMSPolyline 对象。

Swift

polyline.strokeColor = .blue
      

Objective-C

polyline.strokeColor = [UIColor blueColor];
      

更改多段线的颜色

多段线在地图上绘制为一系列线段。您可以使用 spans 属性更改各个线段或整条线的颜色。虽然您可以利用此属性对多段线的着色进行精细控制,但也存在一些便利性,让您能够将单个样式应用于整条多段线。

以下代码段使用 spanWithColor: 方法将整条线的颜色更改为红色。

Swift

polyline.spans = [GMSStyleSpan(color: .red)]
      

Objective-C

polyline.spans = @[[GMSStyleSpan spanWithColor:[UIColor redColor]]];
      

或者,如果您已经有权访问 GMSStrokeStyle 对象,则可以使用 spanWithStyle: 方法。

Swift

let solidRed = GMSStrokeStyle.solidColor(.red)
polyline.spans = [GMSStyleSpan(style: solidRed)]
      

Objective-C

GMSStrokeStyle *solidRed = [GMSStrokeStyle solidColor:[UIColor redColor]];
polyline.spans = @[[GMSStyleSpan spanWithStyle:solidRed]];
      

在 Maps SDK for iOS 1.7 版之前,使用单个属性 strokeColor 来设置 GMSPolyline 的整个颜色。spans 属性的优先级高于 strokeColor

Swift

polyline.strokeColor = .red
      

Objective-C

polyline.strokeColor = [UIColor redColor];
      

风格

如果您的应用多次应用相同的描边颜色,您可能会发现定义可重复使用的样式很有用。多段线样式是使用 GMSStrokeStyle 对象指定的。描边样式可以是纯色,也可以是从一种颜色到另一种颜色的渐变。创建样式后,您可以使用 spanWithStyle: 方法将其应用到 GMSStyleSpan

Swift

// Create two styles: one that is solid blue, and one that is a gradient from red to yellow
let solidBlue = GMSStrokeStyle.solidColor(.blue)
let solidBlueSpan = GMSStyleSpan(style: solidBlue)
let redYellow = GMSStrokeStyle.gradient(from: .red, to: .yellow)
let redYellowSpan = GMSStyleSpan(style: redYellow)
      

Objective-C

// Create two styles: one that is solid blue, and one that is a gradient from red to yellow
GMSStrokeStyle *solidBlue = [GMSStrokeStyle solidColor:[UIColor blueColor]];
GMSStyleSpan *solidBlueSpan = [GMSStyleSpan spanWithStyle:solidBlue];
GMSStrokeStyle *redYellow =
    [GMSStrokeStyle gradientFromColor:[UIColor redColor] toColor:[UIColor yellowColor]];
GMSStyleSpan *redYellowSpan = [GMSStyleSpan spanWithStyle:redYellow];
      

span 的样式将持续有效,直到多段线结束或者设置了新的样式。您可以通过将多段线的 spans 属性设置为单个 GMSStyleSpan 来更改整条线的颜色。此示例演示了如何对多段线的整个长度应用渐变。

Swift

polyline.spans = [GMSStyleSpan(style: redYellow)]
      

Objective-C

polyline.spans = @[[GMSStyleSpan spanWithStyle:redYellow]];
      

更改各个线段的颜色

如果您想单独设置多段线的每个线段的样式,则可以创建一个 GMSStyleSpan 对象数组,并将该数组传递给 spans 属性。默认情况下,该数组中的每一项用于设置相应线段的颜色。如果数组中的元素比多段线中的线段多,多余的元素将被忽略。如果该数组中的元素较少,最后一个 GMSStyleSpan 将描述该线条其余部分的颜色。

您可以使用颜色块和/或渐变多段线来指示多段线沿线的变化,例如海拔或速度。以下代码段会将多段线前两个线段的颜色设为红色,而该多段线的其余部分则是从红色到黄色的渐变。

Swift

polyline.spans = [
  GMSStyleSpan(style: solidRed),
  GMSStyleSpan(style: solidRed),
  GMSStyleSpan(style: redYellow)
]
      

Objective-C

polyline.spans = @[[GMSStyleSpan spanWithStyle:solidRed],
                   [GMSStyleSpan spanWithStyle:solidRed],
                   [GMSStyleSpan spanWithStyle:redYellow]];
      

您可以使用 spanWithStyle:segments: 方法一次为多个片段设置样式。例如,以下代码等同于上面的代码。 最终 GMSStyleSpan 的线段长度始终会被忽略,因为样式将用于描述线条的其余部分。

Swift

polyline.spans = [
  GMSStyleSpan(style: solidRed, segments:2),
  GMSStyleSpan(style: redYellow, segments:10)
]
      

Objective-C

polyline.spans = @[[GMSStyleSpan spanWithStyle:solidRed segments:2],
                   [GMSStyleSpan spanWithStyle:redYellow segments:10]];
      

分数细分

线段也可以指定为分数值。这会将样式应用于线段的小数部分,可能会导致单个线段拆分。每个 GMSStyleSpan 都紧随上一个片段之后开始:在下面的示例中,灰色从 1⁄2 开始到第二个片段,并持续到第三个片段的 1⁄2。

Swift

polyline.spans = [
  GMSStyleSpan(style: solidRed, segments: 2.5),
  GMSStyleSpan(color: .gray),
  GMSStyleSpan(color: .purple, segments: 0.75),
  GMSStyleSpan(style: redYellow)
]
      

Objective-C

polyline.spans = @[[GMSStyleSpan spanWithStyle:solidRed segments:2.5],
                   [GMSStyleSpan spanWithColor:[UIColor grayColor]],
                   [GMSStyleSpan spanWithColor:[UIColor purpleColor] segments:0.75],
                   [GMSStyleSpan spanWithStyle:redYellow]];
      

向多段线添加重复的颜色模式

如果您想要向多段线添加图案,可以使用 GMSGeometryUtils 中的 GMSStyleSpans 实用程序方法。GMSStyleSpans 方法接受用于定义重复模式的两个数组。其中一个数组用于设置应重复的样式,另一个数组用于指定重复的间隔。将它们结合使用时,您可以创建一个可应用于任何多段线的图案,无论多段线的长度或可用线段的数量如何。

例如,以下代码段定义了一条采用黑白交替模式的多段线。由于类型指定为 kGMSLengthRhumb,因此其长度被视为沿恒温线(在墨卡托线中,这是一条直线)的米长。

Swift

let styles = [
  GMSStrokeStyle.solidColor(.white),
  GMSStrokeStyle.solidColor(.black)
]
let lengths: [NSNumber] = [100000, 50000]
polyline.spans = GMSStyleSpans(
  polyline.path!,
  styles,
  lengths,
  GMSLengthKind.rhumb
)
      

Objective-C

NSArray *styles = @[[GMSStrokeStyle solidColor:[UIColor whiteColor]],
                    [GMSStrokeStyle solidColor:[UIColor blackColor]]];
NSArray *lengths = @[@100000, @50000];
polyline.spans = GMSStyleSpans(polyline.path, styles, lengths, kGMSLengthRhumb);
      

精灵印花多段线

借助精灵印花多段线,您可以使用自己选择的重复位图图像来创建多段线。形状显示有清晰的背景描边,但线角处的图章没有被截断,这对于用点表示步行路线等情况很有用。

精灵印花多段线

您可以使用 GMSSpriteStyle 并借助 GMSStrokeStylestampStyle 属性将其设为戳记来使用此功能。

Swift

let path = GMSMutablePath()
path.addLatitude(-37.81319, longitude: 144.96298)
path.addLatitude(-31.95285, longitude: 115.85734)
let polyline = GMSPolyline(path: path)
polyline.strokeWidth = 20
let image = UIImage(named: "imageFromBundleOrAsset")! // Image could be from anywhere
let stampStyle = GMSSpriteStyle(image: image)
let transparentStampStroke = GMSStrokeStyle.transparentStroke(withStamp: stampStyle)
let span = GMSStyleSpan(style: transparentStampStroke)
polyline.spans = [span]
polyline.map = mapView
      

Objective-C

GMSMutablePath *path = [GMSMutablePath path];
[path addLatitude:-37.81319 longitude:144.96298];
[path addLatitude:-31.95285 longitude:115.85734];
polyline.strokeWidth = 20;
GMSPolyline *polyline = [GMSPolyline polylineWithPath:path];

UIImage *image = [UIImage imageNamed:@"imageFromBundleOrAsset"];
GMSStrokeStyle *transparentStampStroke = [GMSStrokeStyle transparentStrokeWithStampStyle:[GMSSpriteStyle spriteStyleWithImage:image]];

GMSStyleSpan *span = [GMSStyleSpan spanWithStyle:transparentStampStroke];
polyline.spans = @[span];
polyline.map = _mapView;
      

纹理印花多段线

借助纹理印花多段线,您可以使用自己选择的重复纹理创建多段线。形状可以在显示时采用清晰的纯色或渐变背景描边。纹理会随着缩放级别的变化而调整大小。在某些缩放级别,路径或路径点结束或起始处的图像会被截断。

带纹理的多段线

您可以使用 GMSTextureStyle 来使用此功能,并通过 GMSStrokeStylestampStyle 属性将其设为戳记。

Swift

let path = GMSMutablePath()
path.addLatitude(-37.81319, longitude: 144.96298)
path.addLatitude(-31.95285, longitude: 115.85734)
let polyline = GMSPolyline(path: path)
polyline.strokeWidth = 20
let redWithStamp = GMSStrokeStyle.solidColor(.red)
let image = UIImage(named: "imageFromBundleOrAsset")! // Image could be from anywhere
redWithStamp.stampStyle = GMSTextureStyle(image: image)
let span = GMSStyleSpan(style: redWithStamp)
polyline.spans = [span]
polyline.map = mapView
      

Objective-C

GMSMutablePath *path = [GMSMutablePath path];
[path addLatitude:-37.81319 longitude:144.96298];
[path addLatitude:-31.95285 longitude:115.85734];
GMSPolyline *polyline = [GMSPolyline polylineWithPath:path];
polyline.strokeWidth = 20;
GMSStrokeStyle *redWithStamp = [GMSStrokeStyle solidColor:[UIColor redColor]];

UIImage *image = [UIImage imageNamed:@"imageFromBundleOrAsset"]; // Image could be from anywhere
redWithStamp.stampStyle = [GMSTextureStyle textureStyleWithImage:image];

GMSStyleSpan *span = [GMSStyleSpan spanWithStyle:redWithStamp];
polyline.spans = @[span];
polyline.map = _mapView;
      

地图功能

GMSMapView 上的 mapCapabilities 属性添加了对地图专用地图项的程序化检查。这对于想要在调用特定 API 之前了解特定地图 capabilities 是否可用时非常有用。此查询可确定地图视图是否支持精灵印花多段线。

Swift

let path = GMSMutablePath()
path.addLatitude(-37.81319, longitude: 144.96298)
path.addLatitude(-31.95285, longitude: 115.85734)
let polyline = GMSPolyline(path: path)
polyline.strokeWidth = 20
let image = UIImage(named: "imageFromBundleOrAsset")! // Image could be from anywhere
let spans: [GMSStyleSpan]
if (mapView.mapCapabilities.contains(.spritePolylines)) {
  let spriteStyle = GMSSpriteStyle(image: image)
  let stroke = GMSStrokeStyle.transparentStroke(withStamp: spriteStyle)
  spans = [ GMSStyleSpan(style: stroke) ]
} else {
  let stroke = GMSStrokeStyle.solidColor(.clear)
  stroke.stampStyle = GMSTextureStyle(image: image)
  spans = [ GMSStyleSpan(style: stroke) ]
}
polyline.spans = spans
polyline.map = mapView
      

Objective-C

GMSMutablePath *path = [GMSMutablePath path];
[path addLatitude:-37.81319 longitude:144.96298];
[path addLatitude:-31.95285 longitude:115.85734];

UIImage *_Nonnull image = [UIImage imageNamed:@"imageFromBundleOrAsset"]; // Image could be from anywhere

NSArray<GMSStyleSpan *> * spans;
if (_mapView.mapCapabilities & GMSMapCapabilityFlagsSpritePolylines) {
  GMSSpriteStyle *spriteStyle = [GMSSpriteStyle spriteStyleWithImage:image];
  GMSStrokeStyle *stroke = [GMSStrokeStyle transparentStrokeWithStampStyle:spriteStyle];
  spans = @[ [GMSStyleSpan spanWithStyle:stroke] ];
} else {
  GMSStrokeStyle *stroke = [GMSStrokeStyle solidColor:UIColor.clearColor];
  stroke.stampStyle = [GMSTextureStyle textureStyleWithImage:image];
  spans = @[ [GMSStyleSpan spanWithStyle:stroke] ];
}

GMSPolyline *polyline = [GMSPolyline polylineWithPath:path];
polyline.strokeWidth = 20;
polyline.spans = spans;
polyline.map = _mapView;
      

此模式可让您订阅更改,并使用地图视图状态对更新做出响应。您还可以在 GMSMapViewDelegate 上实现 didChangeMapCapabilities,以获取有关功能可用性的最新动态。

多边形

多边形与多段线类似,也由一系列有序的坐标组成。不过,多边形并不是开放式的,它们用于定义闭合环路内的实心区域。在 Maps SDK for iOS 中,多边形由 GMSPolygon 类定义。

您可以按照添加 GMSPolyline 的方法向地图添加 GMSPolygon。首先,通过创建相应的 GMSMutablePath 对象并为其添加点来指定其路径。这些点构成了多边形的轮廓。每个 CLLocationCoordinate2D 代表地球表面上的点。系统会按照您向路径添加点的顺序在各点之间绘制线段。

添加多边形

  1. 创建一个 GMSMutablePath 对象。
  2. 使用 addCoordinate:addLatitude:longitude: 方法设置路径中的点。这些点构成了多边形的轮廓。
  3. 使用路径作为参数实例化新的 GMSPolygon 对象。
  4. 根据需要设置其他属性,如 strokeWidthstrokeColorfillColor
  5. 通过设置 GMSPolygon.map 属性,将多边形分配给 GMSMapView 对象。
  6. 多边形就会显示在地图上。

以下代码段的作用是向地图添加一个矩形。

Swift

// Create a rectangular path
let rect = GMSMutablePath()
rect.add(CLLocationCoordinate2D(latitude: 37.36, longitude: -122.0))
rect.add(CLLocationCoordinate2D(latitude: 37.45, longitude: -122.0))
rect.add(CLLocationCoordinate2D(latitude: 37.45, longitude: -122.2))
rect.add(CLLocationCoordinate2D(latitude: 37.36, longitude: -122.2))

// Create the polygon, and assign it to the map.
let polygon = GMSPolygon(path: rect)
polygon.fillColor = UIColor(red: 0.25, green: 0, blue: 0, alpha: 0.05);
polygon.strokeColor = .black
polygon.strokeWidth = 2
polygon.map = mapView
      

Objective-C

// Create a rectangular path
GMSMutablePath *rect = [GMSMutablePath path];
[rect addCoordinate:CLLocationCoordinate2DMake(37.36, -122.0)];
[rect addCoordinate:CLLocationCoordinate2DMake(37.45, -122.0)];
[rect addCoordinate:CLLocationCoordinate2DMake(37.45, -122.2)];
[rect addCoordinate:CLLocationCoordinate2DMake(37.36, -122.2)];

// Create the polygon, and assign it to the map.
GMSPolygon *polygon = [GMSPolygon polygonWithPath:rect];
polygon.fillColor = [UIColor colorWithRed:0.25 green:0 blue:0 alpha:0.05];
polygon.strokeColor = [UIColor blackColor];
polygon.strokeWidth = 2;
polygon.map = mapView;
      

在将多边形添加到地图之前和之后,您都可以自定义其外观。

移除多边形

如需移除多边形,请将其 GMSPolygon.map 属性设置为 nil 并将 layer 与其父级分离。

Swift

 polygon.map = nil
 polygon.layer.removeFromSuperLayer()

Objective-C

 polygon.map = nil;
 [polygon.layer removeFromSuperlayer];
 

圆形

除了常规的 GMSPolygon 类之外,Maps SDK for iOS 还包含 GMSCircle,可让您在地球表面上绘制圆形。

如需构建圆形,您必须指定以下两个属性:

  • position,以 CLLocationCoordinate2D 形式表示。
  • radius(以米为单位)。

然后便可将圆形定义为地球表面上所有与给定 center 的距离为 radius 米的点的集合。鉴于 Maps API 使用的墨卡托投影法在平面上渲染球体的方式,该球形在位于赤道附近时会在地图上显示为近乎完美的圆,而随着圆形向远离赤道的远离而愈发(在屏幕上)呈现为非圆形状。

添加圆形

下面这段代码用于向地图添加圆:

Swift

let circleCenter = CLLocationCoordinate2D(latitude: 37.35, longitude: -122.0)
let circle = GMSCircle(position: circleCenter, radius: 1000)
circle.map = mapView
      

Objective-C

CLLocationCoordinate2D circleCenter = CLLocationCoordinate2DMake(37.35, -122.0);
GMSCircle *circle = [GMSCircle circleWithPosition:circleCenter
                                         radius:1000];
circle.map = mapView;
      

在将圆形添加到地图之前和之后,您都可以自定义其外观。

自定义圆

您可以通过修改 GMSCircle 的属性来指定自定义颜色和描边宽度。它支持下列选项:

fillColor
一个 UIColor 对象,用于指定圆形的内部颜色。默认为透明。
strokeColor
一个 UIColor 对象,用于指定圆形轮廓的颜色。默认值为 blackColor
strokeWidth
圆形轮廓的粗细(以屏幕点数为单位)。默认值为 1。 厚度不会随地图的缩放而缩放。

以下代码段将添加一个粗红色圆圈,其内部是半透明的红色。

Swift

circle.fillColor = UIColor(red: 0.35, green: 0, blue: 0, alpha: 0.05)
circle.strokeColor = .red
circle.strokeWidth = 5
      

Objective-C

circle.fillColor = [UIColor colorWithRed:0.25 green:0 blue:0 alpha:0.05];
circle.strokeColor = [UIColor redColor];
circle.strokeWidth = 5;
      

创建空心多边形

您可以在单个 GMSPolygon 对象中组合多个路径,以创建复杂形状,例如填充环或甜甜圈(其中多边形区域在多边形内显示为单独的形状)。复杂形状由多条路径组成。

创建一个多边形,并使用一个路径指定多边形覆盖的最大面积。然后,将多边形的 holes 属性指定为一个或多个 GMSPath 对象的数组,这些对象用于定义多边形中的孔。

如果较小的路径被较大路径完全包围,就会产生好像多边形的一部分被被移除的效果。

下面的代码示例将创建一个带两个洞的多边形:

Swift

let hydeParkLocation = CLLocationCoordinate2D(latitude: -33.87344, longitude: 151.21135)
let camera = GMSCameraPosition.camera(withTarget: hydeParkLocation, zoom: 16)
let mapView = GMSMapView.map(withFrame: .zero, camera: camera)
mapView.animate(to: camera)

let hydePark = "tpwmEkd|y[QVe@Pk@BsHe@mGc@iNaAKMaBIYIq@qAMo@Eo@@[Fe@DoALu@HUb@c@XUZS^ELGxOhAd@@ZB`@J^BhFRlBN\\BZ@`AFrATAJAR?rAE\\C~BIpD"
let archibaldFountain = "tlvmEqq|y[NNCXSJQOB[TI"
let reflectionPool = "bewmEwk|y[Dm@zAPEj@{AO"

let hollowPolygon = GMSPolygon()
hollowPolygon.path = GMSPath(fromEncodedPath: hydePark)
hollowPolygon.holes = [GMSPath(fromEncodedPath: archibaldFountain)!, GMSPath(fromEncodedPath: reflectionPool)!]
hollowPolygon.fillColor = UIColor(red: 1.0, green: 0.0, blue: 0.0, alpha: 0.2)
hollowPolygon.strokeColor = UIColor(red: 1.0, green: 0.0, blue: 0.0, alpha: 1.0)
hollowPolygon.strokeWidth = 2
hollowPolygon.map = mapView
      

Objective-C

CLLocationCoordinate2D hydeParkLocation = CLLocationCoordinate2DMake(-33.87344, 151.21135);
GMSCameraPosition *camera = [GMSCameraPosition cameraWithTarget:hydeParkLocation
                                                           zoom:16];
mapView = [GMSMapView mapWithFrame:CGRectZero camera:camera];

NSString *hydePark = @"tpwmEkd|y[QVe@Pk@BsHe@mGc@iNaAKMaBIYIq@qAMo@Eo@@[Fe@DoALu@HUb@c@XUZS^ELGxOhAd@@ZB`@J^BhFRlBN\\BZ@`AFrATAJAR?rAE\\C~BIpD";
NSString *archibaldFountain = @"tlvmEqq|y[NNCXSJQOB[TI";
NSString *reflectionPool = @"bewmEwk|y[Dm@zAPEj@{AO";

GMSPolygon *hollowPolygon = [[GMSPolygon alloc] init];
hollowPolygon.path = [GMSPath pathFromEncodedPath:hydePark];
hollowPolygon.holes = @[[GMSPath pathFromEncodedPath:archibaldFountain],
                  [GMSPath pathFromEncodedPath:reflectionPool]];
hollowPolygon.fillColor = [UIColor colorWithRed:1.0 green:0.0 blue:0.0 alpha:0.2];
hollowPolygon.strokeColor = [UIColor colorWithRed:1.0 green:0.0 blue:0.0 alpha:1.0];
hollowPolygon.strokeWidth = 2;
hollowPolygon.map = mapView;