6

我试图找出以点为单位的 MKMapRect 的大小(即 iPhone 的 320x568 点)。

有没有类似于将坐标转换为点的东西?IE

[self.mapView convertCoordinate:coordinate1 toPointToView:self.view];
4

2 回答 2

7

地图视图具有convertRegion:toRectToView:接受 anMKCoordinateRegion并将其转换为CGRect相对于指定视图的方法。

如果你有MKMapRect,首先MKCoordinateRegion使用MKCoordinateRegionForMapRect函数将其转换为 ,然后调用convertRegion:toRectToView:

例子:

MKCoordinateRegion mkcr = MKCoordinateRegionForMapRect(someMKMapRect);

CGRect cgr = [mapView convertRegion:mkcr toRectToView:self.view];


请记住,虽然MKMapRect某些固定区域的 不会随着地图的缩放或平移而改变,但对应的和CGRect 有所不同。originsize

于 2014-02-27T02:18:55.253 回答
0

也许作为一个实际的例子......我使用这段代码在屏幕上的地图上添加了一个叠加层,然后检查屏幕的哪些部分是否需要更新。

此方法是 MKOverlay 类的一部分。我的 UIViewController 被命名为“MyWaysViewController”,屏幕上的地图被称为“MapOnScreen”(只是为了理解代码)

它的 Swift 3 / IOS 10 代码

/**
 -----------------------------------------------------------------------------------------------

 adds the overlay to the map and sets "setNeedsDisplay()" for the visible part of the overlay

 -----------------------------------------------------------------------------------------------

 - Parameters:

 - Returns: nothing

 */
func switchOverlayON() {

    DispatchQueue.main.async(execute: {
        // add the new overlay

        // if the ViewController is already initialised
        if MyWaysViewController != nil {

            // add the overlay
            MyWaysViewController!.MapOnScreen.add(self)

            // as we are good citizens on that device, we check if and for what region we setNeedsDisplay()

            // get the intersection of the overlay and the visible region of the map
            let visibleRectOfOverlayMK = MKMapRectIntersection(
                    self.boundingMapRect,
                    MyWaysViewController!.MapOnScreen.visibleMapRect
            )

            // check if it is null (no intersection -> not visible at the moment)
            if MKMapRectIsNull(visibleRectOfOverlayMK) == false {

                // It is not null, so at least parts are visible, now a two steps aproach to
                // convert MKMapRect to cgRect. first step: get a coordinate region
                let visibleRectCoordinateRegion = MKCoordinateRegionForMapRect(visibleRectOfOverlayMK)

                // second step, convert the region to a cgRect
                let visibleRectOfOverlayCG = MyWaysViewController!.MapOnScreen.convertRegion(visibleRectCoordinateRegion, toRectTo: MyWaysViewController!.MapOnScreen)

                // ask to refresh that cgRect
                MyWaysViewController!.MapOnScreen.setNeedsDisplay(visibleRectOfOverlayCG)
            }
        }
    })
}
于 2017-01-25T10:58:39.700 回答