带孔的Google-SDK-iOS多边形

时间:2022-12-03 12:58:24

Hy, i am trying to achieve this, but in iOS:

Hy,我正在努力实现这一目标,但在iOS中:

Donut Google Maps Android

甜甜圈谷歌地图Android

Basically the main idea is to have a zone highlighted and the rest faded. This is a requested feature in the Google-SDk-iOS, https://code.google.com/p/gmaps-api-issues/issues/detail?id=5464

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

So far what i've done:

到目前为止我做了什么:

  • Set the mapview alpha to a low value (Transparency)
  • 将mapview alpha设置为较低的值(透明度)
  • Draw a simple circle using google sdk GMSCircle on that view
  • 在该视图上使用google sdk GMSCircle绘制一个简单的圆圈

But what this achieves is the inside of the circle also fades out. Any ideas or workarounds would me much appreciated, thank you.

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

1 个解决方案

#1


2  

Unfortunately, you can not make a hole in GMSPolygon for iOS yet, this feature is only available in Android.

不幸的是,你不能在iOS的GMSPolygon上搞错,这个功能仅在Android中可用。

A workaround will be using pointForCoordinate() method from GMSProjection class. This method can convert a Earth coordinate to a point in your application's window.

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

Also, in order to make the hole transparent, you probably need to use CAShapeLayer in your view. You can see more details in this * answer.

此外,为了使孔透明,您可能需要在视图中使用CAShapeLayer。您可以在此*答案中查看更多详细信息。

Sample code:

示例代码:

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
    }
}

带孔的Google-SDK-iOS多边形

#1


2  

Unfortunately, you can not make a hole in GMSPolygon for iOS yet, this feature is only available in Android.

不幸的是,你不能在iOS的GMSPolygon上搞错,这个功能仅在Android中可用。

A workaround will be using pointForCoordinate() method from GMSProjection class. This method can convert a Earth coordinate to a point in your application's window.

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

Also, in order to make the hole transparent, you probably need to use CAShapeLayer in your view. You can see more details in this * answer.

此外,为了使孔透明,您可能需要在视图中使用CAShapeLayer。您可以在此*答案中查看更多详细信息。

Sample code:

示例代码:

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
    }
}

带孔的Google-SDK-iOS多边形