首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >将MKMapRect转换为CGRect

将MKMapRect转换为CGRect
EN

Stack Overflow用户
提问于 2014-02-26 21:10:08
回答 2查看 3.6K关注 0票数 6

我正在试图找出MKMapRect的大小(即iPhone的320x568点)。

是否有类似于将协调点转换为点的东西?即

代码语言:javascript
复制
[self.mapView convertCoordinate:coordinate1 toPointToView:self.view];
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2014-02-27 02:18:55

映射视图具有convertRegion:toRectToView:方法,该方法接受MKCoordinateRegion并将其转换为相对于指定视图的CGRect

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

示例:

代码语言:javascript
复制
MKCoordinateRegion mkcr = MKCoordinateRegionForMapRect(someMKMapRect);

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

请记住,虽然某些固定区域的MKMapRect不会随着地图的缩放或平移而改变,但对应的CGRect将在其originsize中发生变化。

票数 7
EN

Stack Overflow用户

发布于 2017-01-25 10:58:39

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

此方法是MKOverlay类的一部分。我的UIViewController名为"MyWaysViewController“,屏幕上的映射名为"MapOnScreen”(只是为了理解代码)。

它的Swift 3/ IOS 10代码

代码语言:javascript
复制
/**
 -----------------------------------------------------------------------------------------------

 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)
            }
        }
    })
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/22053333

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档