形状

请选择平台: 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]];
      

在 1.7 版 Maps SDK for iOS 之前,单个属性 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);
      

Sprite 冲压多段线

通过 Sprite 印花多段线,您可以使用自己选择的重复位图图像创建多段线。形状在显示时会有清晰的背景描边,但图章不会围绕线角截断,这在点(例如用于表示步行路线的点)中非常有用。

精灵印花多段线

您可以使用 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 是否可用,这会非常有用。此查询用于确定地图视图是否支持 Sprite 印花多段线。

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;