简体   繁体   English

iOS MapKit:如何防止MKMapView setRegion改变区域?

[英]iOS MapKit: how to prevent MKMapView setRegion from changing region?

I'm making an app with a MapView and I want to persist the position of the map with NSUserDefaults . 我正在用MapView创建一个应用程序,我想用NSUserDefaults来保持地图的位置。 Whenever the map's position changes I save the map region. 每当地图的位置发生变化时,我都会保存地图区域。 When the view loads, if there is a saved map region then I call setRegion on the MapView with the saved region. 加载视图时,如果存在已保存的地图区域,则在MapView上使用保存的区域调用setRegion。 The issue is that the map position after loading is not the same as the position that was last saved. 问题是加载后的地图位置与上次保存的位置不同。 Here is the code: 这是代码:

override func viewDidLoad() {
    super.viewDidLoad()

    // Read saved coordinate region from NSUserDefaults
    if let array = self.readSavedMapPosition() {
        print("Loading saved region")
        let mkcr = MKCoordinateRegion(center: CLLocationCoordinate2D(latitude: array[0], longitude: array[1]), span: MKCoordinateSpan(latitudeDelta: array[2], longitudeDelta: array[3]))
            self.mapView.setRegion(mkcr, animated: true)
    }
}

func saveMapPosition(mkcr: MKCoordinateRegion) {
    let defaults = NSUserDefaults.standardUserDefaults()
    let array = [mkcr.center.latitude, mkcr.center.longitude, mkcr.span.latitudeDelta, mkcr.span.longitudeDelta]
    print("map position saved: \(array)")
    defaults.setObject(array, forKey: savedMKCRArray)
}

func readSavedMapPosition() -> [Double]? {
    let defaults = NSUserDefaults.standardUserDefaults()
    let array = defaults.objectForKey(savedMKCRArray) as? [Double]
    print("map position read: \(array)")
    return array
}

func mapView(mapView: MKMapView, regionDidChangeAnimated animated: Bool) {
    print("Map view region changed, saving new position")
    let currentRegion = mapView.region
    self.saveMapPosition(currentRegion)
}

When running the app in the simulator, I'll get output like this in the console: 在模拟器中运行应用程序时,我将在控制台中获得如下输出:

map position read: Optional([42.6751429892564, -79.4232294187297, 2.8355733828259, 3.85688659929976]) 地图位置读取:可选([42.6751429892564,-79.4232294187297,2.8355733828259,3.85688659929976])

Loading saved region 加载保存的区域

Map view region changed, saving new position 地图视图区域已更改,保存新位置

map position saved: [39.9600790604276, -79.4232294187296, 7.57379831196707, 9.88327223217576] 地图位置已保存:[39.9600790604276,-79.4232294187296,7.57379831196707,98.88327223217576]

After setRegion is called, the coordinates and the span of the region are changed. 调用setRegion后,将更改区域的坐标和跨度。 Apple's documentation for setRegion explains that the map may change the region to fit the visible area of the map: Apple的setRegion文档解释说,地图可能会更改区域以适合地图的可见区域:

When setting a new region, the map may adjust the value in the region parameter so that it fits the visible area of the map precisely. 设置新区域时,地图可以调整region参数中的值,使其精确地适合地图的可见区域。 This adjustment is normal and is done to ensure that the value in the region property always reflects the visible portion of the map. 此调整是正常的,用于确保region属性中的值始终反映地图的可见部分。 However, it does mean that if you get the value of that property right after calling this method, the returned value may not match the value you set. 但是,它确实意味着如果在调用此方法后立即获得该属性的值,则返回的值可能与您设置的值不匹配。 (You can use the regionThatFits: method to determine the region that will actually be set by the map.) (您可以使用regionThatFits:方法来确定地图实际设置的区域。)

Shouldn't the region parameter to setRegion already fit the visible area of the map, since this region came from a previously saved position of the map? setRegionregion参数setRegion已经适合地图的可见区域,因为此区域来自之前保存的地图位置? How can I get more reliable behavior from setRegion ? 如何从setRegion获得更可靠的行为?

I solved this Problem, that I don't save the last region on region change, instead of that I save the region if the view should disappear Action is triggered. 我解决了这个问题,我不保存区域更改的最后一个区域,而不是保存区域,如果视图应该消失,则触发Action。 Like Close button or Done button 如关闭按钮或完成按钮

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM