大家好我想设置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
}`
以下是我从此代码中获得的内容
如果有人可以帮助我获得理想的结果
答案 0 :(得分:3)
所有人的拳头都像
var outerCoordinates = [CLLocationCoordinate2D]()
for i in -90...90 {
outerCoordinates.append(CLLocationCoordinate2D(latitude: Double(i), longitude: -180))
}
for i in -180...180 {
outerCoordinates.append(CLLocationCoordinate2D(latitude: 90, longitude: Double(i)))
}
for i in -90...90 {
outerCoordinates.append(CLLocationCoordinate2D(latitude: Double(i * -1), longitude: 180))
}
for i in -180...180 {
outerCoordinates.append(CLLocationCoordinate2D(latitude: -90, longitude: Double(i * -1)))
}
现在内部多边形
var innerPolygons = [MKPolygon]()
将所有内部多边形插入innerPolygons数组中,即
var coordinates = [CLLocationCoordinate2D]()
// TODO: add all coordinates for inner polygon
let poly = MKPolygon(coordinates: &coordinates, count: coordinates.count)
innerPolygons.append(poly)
添加所有内部多边形后
let polygon = MKPolygon(coordinates: &outerCoordinates, count: outerCoordinates.count, interiorPolygons: innerPolygons)
mapView.add(polygon)