如何在不同的郊区覆盖带有色调的ta GMSMapView?

时间:2017-04-08 08:25:26

标签: ios swift google-maps gmsmapview

我正在使用Swift构建应用程序。我已成功将谷歌地图添加到我的应用程序作为主视图控制器上的子视图,其中包含以下代码:

let camera = GMSCameraPosition.camera(withLatitude: -33.86, longitude: 151.20, zoom: 6.0)
var mapView = GMSMapView.map(withFrame: CGRect(x: screenWidth*0.03, y: 245, width: screenWidth*0.94, height: screenHeight*0.45), camera: camera)
self.view.addSubview(mapView)

我现在想做一些有点棘手的声音,但我确信这是可能的。我希望能够在我的mapView中覆盖地图的不同区域,由名称或邮政编码指定,具有可以点击的不同颜色的色调。这似乎是制作地图漂亮的常用方法,我确信现有解决方案可以轻松完成。我应该怎么做呢?

1 个答案:

答案 0 :(得分:1)

我找到了办法! Google Maps API提供!在我的原始问题中设置地图后,您可以执行此操作以向地图添加圆圈:

let circleCenter = CLLocationCoordinate2D(latitude: -33.86, longitude: 151.20)
let circ = GMSCircle(position: circleCenter, radius: 2000)
circ.fillColor = UIColor.green.withAlphaComponent(0.5)
circ.map = mapView

您还可以绘制更复杂的形状,包括一系列纬度/长度,如下所示:

let rect = GMSMutablePath()
rect.add(CLLocationCoordinate2D(latitude: 37.886080, longitude: -122.137585))
rect.add(CLLocationCoordinate2D(latitude: 37.899356, longitude: -122.130203))
rect.add(CLLocationCoordinate2D(latitude: 37.900101, longitude: -122.104282))
rect.add(CLLocationCoordinate2D(latitude: 37.879644, longitude: -122.101192))
let polygon = GMSPolygon(path: rect)
polygon.fillColor = UIColor.flatWatermelonDark.withAlphaComponent(0.5)
polygon.strokeColor = .black
polygon.strokeWidth = 2
polygon.isTappable = true
polygon.map = mapView

我仍然没有找到一种无缝覆盖任何带有覆盖图的邮政编码的方法,但这可以通过查找/估计邮政编码区域的纬度/长边来手动完成上述代码片段。