【问题标题】:Google-SDK-iOS Polygon with Hole带孔的 Google-SDK-iOS 多边形
【发布时间】:2015-10-21 19:03:42
【问题描述】:

嘿,我正在努力实现这一点,但在 iOS 中:

Donut Google Maps Android

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

到目前为止我做了什么:

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

但这实现的是圆圈的内部也淡出。非常感谢任何想法或解决方法,谢谢。

【问题讨论】:

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


    【解决方案1】:

    很遗憾,您还不能在 iOS 的 GMSPolygon 中打洞,此功能仅适用于 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
        }
    }
    

    【讨论】:

    • 谢谢@ztan,你的回答拯救了我的一天!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-11
    • 2010-09-26
    • 2016-05-07
    • 2017-12-08
    • 1970-01-01
    相关资源
    最近更新 更多