Getting the bounds of an MKMapView

前端 未结 10 1113
既然无缘
既然无缘 2020-11-28 20:28

In order to setup a query to an external server I want to get the bounds of the current Map View in an iPhone app I\'m building. UIView should respond to bounds but it seems

10条回答
  •  悲&欢浪女
    2020-11-28 20:47

    Swift away... (Based on @deadroxy's answer...)

    typealias Edges = (ne: CLLocationCoordinate2D, sw: CLLocationCoordinate2D)
    
    extension MKMapView {
        func edgePoints() -> Edges {
              let nePoint = CGPoint(x: self.bounds.maxX, y: self.bounds.origin.y)
        let swPoint = CGPoint(x: self.bounds.minX, y: self.bounds.maxY)
        
        let neCoord = self.convert(nePoint, toCoordinateFrom: self)
        let swCoord = self.convert(swPoint, toCoordinateFrom: self)
        
        return (ne: neCoord, sw: swCoord)
        }
    }
    

提交回复
热议问题