如何设置特定 Area/Region 选中的 MapView
How to Set Particular Area/Region Selected MapView
大家好,我正在尝试设置 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
}`
这是我从这段代码中得到的结果
如果有人能帮助我获得想要的结果,请多多指教
Fist of all get outer coordinates like
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)))
}
Now inner Polygon
var innerPolygons = [MKPolygon]()
Insert all your inner polygons in innerPolygons array i.e.
var coordinates = [CLLocationCoordinate2D]()
// TODO: add all coordinates for inner polygon
let poly = MKPolygon(coordinates: &coordinates, count: coordinates.count)
innerPolygons.append(poly)
After adding all inner polygons
let polygon = MKPolygon(coordinates: &outerCoordinates, count: outerCoordinates.count, interiorPolygons: innerPolygons)
mapView.add(polygon)
大家好,我正在尝试设置 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
}`
这是我从这段代码中得到的结果
如果有人能帮助我获得想要的结果,请多多指教
Fist of all get outer coordinates like
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)))
}
Now inner Polygon
var innerPolygons = [MKPolygon]()
Insert all your inner polygons in innerPolygons array i.e.
var coordinates = [CLLocationCoordinate2D]()
// TODO: add all coordinates for inner polygon
let poly = MKPolygon(coordinates: &coordinates, count: coordinates.count)
innerPolygons.append(poly)
After adding all inner polygons
let polygon = MKPolygon(coordinates: &outerCoordinates, count: outerCoordinates.count, interiorPolygons: innerPolygons)
mapView.add(polygon)