Google-SDK-iOS 带孔的多边形

Google-SDK-iOS Polygon with Hole

嗨,我正在努力实现这一点,但是在 iOS:

基本上主要的想法是让一个区域突出显示,其余区域淡化。这是 Google-SDK-iOS 中请求的功能, https://code.google.com/p/gmaps-api-issues/issues/detail?id=5464

到目前为止我所做的:

但是这样实现的是圈内也淡出。如果有任何想法或解决方法,我将不胜感激,谢谢。

遗憾的是,您无法在 GMSPolygon 中为 iOS 打洞,此功能仅在 Android 中可用。

解决方法是使用 GMSProjection class 中的 pointForCoordinate() 方法。此方法可以将地球坐标转换为应用程序 window.

中的一个点

此外,为了使孔透明,您可能需要在视图中使用 CAShapeLayer。您可以在 Whosebug answer.

中查看更多详细信息

示例代码:

class ViewController: UIViewController, GMSMapViewDelegate {

    var mapView: GMSMapView!

    override func viewDidLoad() {
        super.viewDidLoad()
        let camera = GMSCameraPosition.cameraWithLatitude(-33.86,
            longitude: 151.20, zoom: 10)
        mapView = GMSMapView.mapWithFrame(CGRectZero, camera: camera)
        mapView.myLocationEnabled = true
        self.view = mapView

        self.mapView.delegate = self
    }

    func mapView(mapView: GMSMapView!, didChangeCameraPosition position: GMSCameraPosition!) {
        let point = mapView.projection.pointForCoordinate(CLLocationCoordinate2DMake(-33.86, 151.20))
        print("the screen point: \(point.x) \(point.y)")


        for subview in view.subviews {
            if subview.tag == 1 {
                subview.layer.mask = nil
                createHole(subview, holeX: point.x, holeY: point.y, radius: 100)
            }
        }
    }

    override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)

        let overlayView = UIView(frame: view.bounds)
        overlayView.tag = 1
        overlayView.alpha = 0.6
        overlayView.backgroundColor = UIColor.blackColor()
        overlayView.userInteractionEnabled = false
        self.view.addSubview(overlayView)
    }


    func createHole(overlayView : UIView, holeX : CGFloat, holeY : CGFloat, radius: CGFloat)
    {
        let maskLayer = CAShapeLayer()

        // Create a path with the rectangle in it.
        let path = CGPathCreateMutable()

        CGPathAddArc(path, nil, holeX, holeY, radius, 0.0, 2 * 3.14, false)
        CGPathAddRect(path, nil, CGRectMake(0, 0, overlayView.frame.width, overlayView.frame.height))

        maskLayer.backgroundColor = UIColor.blackColor().CGColor

        maskLayer.path = path;
        maskLayer.fillRule = kCAFillRuleEvenOdd

        overlayView.layer.mask = maskLayer
        overlayView.clipsToBounds = true
    }
}