ios - 带孔的 Google-SDK-iOS 多边形
<p><p>嘿,我正在努力实现这一点,但在 iOS 中:</p>
<p> <a href="https://stackoverflow.com/questions/32860169/draw-donut-google-maps-android" rel="noreferrer noopener nofollow">Donut Google Maps Android</a> </p>
<p>基本上,主要想法是突出显示一个区域,其余部分淡化。这是 Google-SDk-iOS 中要求的功能,
<a href="https://code.google.com/p/gmaps-api-issues/issues/detail?id=5464" rel="noreferrer noopener nofollow">https://code.google.com/p/gmaps-api-issues/issues/detail?id=5464</a> </p>
<p>到目前为止我做了什么:</p>
<ul>
<li>将 mapView 的 alpha 设置为较低的值(透明度)</li>
<li>在该 View 上使用 google sdk GMSCircle 绘制一个简单的圆圈</li>
</ul>
<p>但这实现的是圆圈的内部也淡出。非常感谢任何想法或解决方法,谢谢。</p></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p>很遗憾,您还不能在 iOS 的 <code>GMSPolygon</code> 中打洞,此功能仅在 Android 中可用。</p>
<p>解决方法是使用 <a href="https://developers.google.com/maps/documentation/ios-sdk/reference/interface_g_m_s_projection.html#a8822e27ca2557b7f2f484781f956ff5c" rel="noreferrer noopener nofollow">pointForCoordinate()</a> <code>GMSProjection</code> 类的方法。此方法可以将地球坐标转换为应用程序窗口中的一个点。</p>
<p>另外,为了使孔透明,您可能需要在 View 中使用 <code>CAShapeLayer</code>。您可以在此 <a href="https://stackoverflow.com/a/28449791/4195406" rel="noreferrer noopener nofollow">StackOverflow answer</a> 中查看更多详细信息.</p>
<p>示例代码:</p>
<pre><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
}
}
</code></pre>
<p> <a href="/image/y0did.gif" rel="noreferrer noopener nofollow"><img src="/image/y0did.gif" alt="enter image description here"/></a> </p></p>
<p style="font-size: 20px;">关于ios - 带孔的 Google-SDK-iOS 多边形,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/33267237/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/33267237/
</a>
</p>
页:
[1]