【问题标题】:How to Set Particular Area/Region Selected MapView如何设置特定区域/区域选定的 MapView
【发布时间】: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


【解决方案1】:

首先获取外部坐标,例如

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)

【讨论】:

  • 你能在上面添加objective-c解决方案吗?
猜你喜欢
  • 2014-05-08
  • 2012-05-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-06-16
  • 1970-01-01
  • 1970-01-01
  • 2011-08-20
相关资源
最近更新 更多