首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >MKMapView的状态恢复略微缩小

MKMapView的状态恢复略微缩小
EN

Stack Overflow用户
提问于 2018-11-14 04:35:31
回答 1查看 207关注 0票数 1

当恢复mapView的状态时,它会从保存时的位置略微缩小。我创建了一个iOS单视图应用程序,其中只包含一个填充整个视图的MKMapView,以尝试解决此问题。我的调试print语句显示,映射值被正确地保存和恢复,但是跨度、纬度或经度最终发生了变化。我遗漏了什么?

编辑:

此问题的主题与:

MKMapView setRegion "snaps" to predefined zoom levels?

MKMapView show incorrectly saved region

作为实验,我添加了一个MKCoordinateRegion类型的类变量,并将decodeRestorableState方法中读取的区域分配给它。此外,我还向视图控制器添加了一个按钮和操作处理程序,用于将mapView的区域设置为添加的变量的值。当点击按钮时,地图放大到恢复时所期望的位置。我感到奇怪的是,位置坐标在decodeRestorableState方法中不能正常工作,但在按钮的操作处理程序中,这些相同的坐标却能正常工作。

代码语言:javascript
复制
import UIKit
import MapKit

class ViewController: UIViewController {
    @IBOutlet weak var mapView: MKMapView!
}

extension MKMapView {
    override open func encodeRestorableState(with coder: NSCoder) {
        super.encodeRestorableState(with: coder)
        region.encodeRestorableState(with: coder)
        camera.encodeRestorableState(with: coder)
    }

    override open func decodeRestorableState(with coder: NSCoder) {
        super.decodeRestorableState(with: coder)
        region.decodeRestorableState(with: coder)
        camera.decodeRestorableState(with: coder)
    }
}

extension MKCoordinateRegion {
    private static let KEY_LATITUDE  = "Region.latitude"
    private static let KEY_LONGITUDE = "Region.longitude"
    private static let KEY_WIDTH     = "Region.spanWidth"
    private static let KEY_HEIGHT    = "Region.spanHeight"

    func encodeRestorableState(with coder: NSCoder) {
        coder.encode(center.latitude, forKey: MKCoordinateRegion.KEY_LATITUDE)
        coder.encode(center.longitude, forKey: MKCoordinateRegion.KEY_LONGITUDE)
        coder.encode(span.latitudeDelta, forKey: MKCoordinateRegion.KEY_HEIGHT)
        coder.encode(span.longitudeDelta, forKey: MKCoordinateRegion.KEY_WIDTH)
    }

    mutating func decodeRestorableState(with coder: NSCoder) {
        center.latitude = coder.decodeDouble(forKey: MKCoordinateRegion.KEY_LATITUDE)
        center.longitude = coder.decodeDouble(forKey: MKCoordinateRegion.KEY_LONGITUDE)
        span.latitudeDelta = coder.decodeDouble(forKey: MKCoordinateRegion.KEY_HEIGHT)
        span.longitudeDelta = coder.decodeDouble(forKey: MKCoordinateRegion.KEY_WIDTH)
    }
}

extension MKMapCamera {
    private static let KEY_ALTITUDE  = "Camera.altitude"
    private static let KEY_HEADING   = "Camera.heading"
    private static let KEY_PITCH     = "Camera.pitch"
    private static let KEY_LATITUDE  = "Camera.latitude"
    private static let KEY_LONGITUDE = "Camera.longitude"

    func encodeRestorableState(with coder: NSCoder) {
        coder.encode(altitude, forKey: MKMapCamera.KEY_ALTITUDE)
        coder.encode(heading, forKey: MKMapCamera.KEY_HEADING)
        coder.encode(Double(pitch), forKey: MKMapCamera.KEY_PITCH)
        coder.encode(centerCoordinate.latitude, forKey: MKMapCamera.KEY_LATITUDE)
        coder.encode(centerCoordinate.longitude, forKey: MKMapCamera.KEY_LONGITUDE)
    }

    func decodeRestorableState(with coder: NSCoder) {
        altitude = coder.decodeDouble(forKey: MKMapCamera.KEY_ALTITUDE)
        heading = coder.decodeDouble(forKey: MKMapCamera.KEY_HEADING)
        pitch = CGFloat(coder.decodeDouble(forKey: MKMapCamera.KEY_PITCH))
        centerCoordinate.latitude = coder.decodeDouble(forKey: MKMapCamera.KEY_LATITUDE)
        centerCoordinate.longitude = coder.decodeDouble(forKey: MKMapCamera.KEY_LONGITUDE)
    }
}
EN

回答 1

Stack Overflow用户

发布于 2019-04-08 21:16:06

也有同样的问题。我的变通方法是在我的视图控制器的viewDidAppear中设置mapView.camera。

代码语言:javascript
复制
let mapCameraKey = "MapCameraKey"
var restoredCamera: MKMapCamera?

override func viewDidAppear(_ animated: Bool) {
    super.viewDidAppear(animated)

    if let restoredCamera = restoredCamera {
        mapView.camera = restoredCamera
    }
}

override func encodeRestorableState(with coder: NSCoder) {
    super.encodeRestorableState(with: coder)

    coder.encode(mapView.camera, forKey: mapCameraKey)
}

override func decodeRestorableState(with coder: NSCoder) {
    super.decodeRestorableState(with: coder)

    restoredCamera = coder.decodeObject(forKey: mapCameraKey) as? MKMapCamera
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/53289090

复制
相关文章

相似问题

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