带洞的Google-SDK-iOS多边形

时间:2015-10-21 19:03:43

标签: ios google-maps google-maps-sdk-ios

Hy,我正在尝试实现这一目标,但在iOS中:

Donut Google Maps Android

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

到目前为止我所做的事情:

  • 将mapview alpha设置为较低的值(透明度)
  • 在该视图上使用google sdk GMSCircle绘制一个简单的圆圈

但是这实现了圆圈的内部也逐渐淡出。我非常感谢任何想法或变通方法,谢谢。

1 个答案:

答案 0 :(得分:2)

不幸的是,你不能在GMSPolygon for iOS上打洞,此功能仅适用于Android。

解决方法是使用GMSProjection课程中的pointForCoordinate()方法。此方法可以将地球坐标转换为应用程序窗口中的某个点。

此外,为了使洞透明,您可能需要在视图中使用CAShapeLayer。您可以在此StackOverflow 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
    }
}

enter image description here