【发布时间】:2017-02-14 17:33:51
【问题描述】:
大家好,我正在尝试设置MapView 的特定区域,如图所示
地图的一部分突出显示,其他看起来变暗。我在我的项目中使用MKMapView,并且我已成功在地图上画线,但我无法使所选区域之外的区域变暗。
我所做的是,允许用户在ImageView 上画线并从接触点获取CLLocationCoordinate2D 对象
这是我的代码
@IBAction func btnActionApply(sender: UIButton) {
viewDraw.hidden = true
mapView.scrollEnabled = true
for touches in arrTouches {
var coordinates = [CLLocationCoordinate2D]()
for p in touches {
let c = mapView.convertPoint(p, toCoordinateFromView: mapView)
coordinates.append(c)
}
let polygon = MKPolygon(coordinates: &coordinates, count: touches.count)
mapView.addOverlay(polygon)
}
viewDraw.image = nil
viewDraw.hidden = true
mapView.scrollEnabled = true
}
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) { if let touch = touches.first { startPoint = touch.locationInView(viewDraw) lastPoint = touch.locationInView(viewDraw) self.touches = [startPoint] } } override func touchesMoved(touches: Set<UITouch>, withEvent event: UIEvent?) { if let touch = touches.first { let currentPoint = touch.locationInView(viewDraw) self.touches.append(touch.locationInView(mapView)) drawLineFrom(lastPoint, toPoint: currentPoint) lastPoint = currentPoint } self.view.setNeedsDisplay() } override func touchesEnded(touches: Set<UITouch>, withEvent event: UIEvent?) { drawLineFrom(lastPoint, toPoint: startPoint) self.touches.append(startPoint) arrTouches.append(self.touches) }
func drawLineFrom(fromPoint: CGPoint, toPoint: CGPoint) {
UIGraphicsBeginImageContextWithOptions(viewDraw.frame.size, false, 0.0)
let context = UIGraphicsGetCurrentContext()
viewDraw.image?.drawInRect(CGRect(x: 0, y: 0, width: viewDraw.frame.size.width, height: viewDraw.frame.size.height))
CGContextMoveToPoint(context, fromPoint.x, fromPoint.y)
CGContextAddLineToPoint(context, toPoint.x, toPoint.y)
CGContextSetLineCap(context, CGLineCap.Round)
CGContextSetLineWidth(context, brushWidth)
CGContextSetRGBStrokeColor(context, red, green, blue, 1.0)
CGContextSetBlendMode(context, .Normal)
// 4
CGContextStrokePath(context)
// 5
viewDraw.image = UIGraphicsGetImageFromCurrentImageContext()
viewDraw.alpha = opacity
UIGraphicsEndImageContext()
}
func mapView(mapView: MKMapView, rendererForOverlay overlay: MKOverlay) -> MKOverlayRenderer {
let lineView = MKPolylineRenderer(overlay: overlay)
lineView.strokeColor = UIColor.greenColor()
return lineView
}`
这是我从这段代码中得到的
如果有人可以帮助我得到想要的结果,请告诉我
【问题讨论】:
-
我在 MapKit 中并不完美。但我会建议你,你可以在整个地图上添加一个黑暗的覆盖。之后,您可以在选定区域设置亮度/模糊效果。
-
@ZuhairHussain 你得到了满足你要求的解决方案吗……我也在寻找解决方案……
-
@sudhanshu.swastik 你我在 MKMapKit 方法下使用过它工作正常 let polygon = MKPolygon(coordinates: &outerCoordinates, count: outerCoordinates.count, interiorPolygons: innerPolygons) mapView.add(polygon)
-
@ZuhairHussain:你能在上面添加objective-c解决方案吗?
标签: ios objective-c swift google-maps mkmapview