为mapView恢复状态后,它会从保存过程中的位置稍微缩小。我创建了一个仅包含MKMapView的iOS单视图应用程序,该应用程序填充了整个视图,以尝试解决此问题。我的调试打印语句显示地图值已正确保存和恢复,但跨度的纬度或经度最终会改变。我想念什么?
编辑:
该问题与以下主题相同:
MKMapView setRegion "snaps" to predefined zoom levels?
MKMapView show incorrectly saved region
作为一个实验,我添加了一个类型为MKCoordinateRegion的类变量,并向其分配了在encodeRestorableState方法中读取的区域。另外,我在视图控制器中添加了按钮和动作处理程序,该控件将mapView的区域设置为所添加变量的值。轻击按钮时,地图将放大到还原所需的位置。对我来说,奇怪的是,位置坐标在decodeRestorableState方法中无法正常工作,但是那些相同的坐标在按钮的动作处理程序中可以正常工作。
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)
}
}
答案 0 :(得分:1)
Had the same issue. Workaround for me was to set mapView.camera in viewDidAppear of my view controller.
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
}