State restoration of MKMapView zooms out slightly












0















When the state is restored for a mapView, it's slightly zoomed out from where it was during the save. I created an iOS Single View App containing only a MKMapView filling the whole view to try and troubleshoot this. My debug print statements show that the map values are being saved and restored properly but the span latitude or longitude eventually change. What am I missing?



EDIT:



This question is the same topic as:
MKMapView setRegion "snaps" to predefined zoom levels?
MKMapView show incorrectly saved region



As an experiment, I added a class variable of type MKCoordinateRegion and assigned the region read in the decodeRestorableState method to it. Also I added a button and action handler to the view controller which set the mapView's region to the value of the added variable. When tapping the button, the map zoomed into the position that was expected from the restoration. It's odd to me that the location coordinates work incorrectly in the decodeRestorableState method but those same coordinates work correctly in the button's action handler.



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)
}
}









share|improve this question





























    0















    When the state is restored for a mapView, it's slightly zoomed out from where it was during the save. I created an iOS Single View App containing only a MKMapView filling the whole view to try and troubleshoot this. My debug print statements show that the map values are being saved and restored properly but the span latitude or longitude eventually change. What am I missing?



    EDIT:



    This question is the same topic as:
    MKMapView setRegion "snaps" to predefined zoom levels?
    MKMapView show incorrectly saved region



    As an experiment, I added a class variable of type MKCoordinateRegion and assigned the region read in the decodeRestorableState method to it. Also I added a button and action handler to the view controller which set the mapView's region to the value of the added variable. When tapping the button, the map zoomed into the position that was expected from the restoration. It's odd to me that the location coordinates work incorrectly in the decodeRestorableState method but those same coordinates work correctly in the button's action handler.



    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)
    }
    }









    share|improve this question



























      0












      0








      0








      When the state is restored for a mapView, it's slightly zoomed out from where it was during the save. I created an iOS Single View App containing only a MKMapView filling the whole view to try and troubleshoot this. My debug print statements show that the map values are being saved and restored properly but the span latitude or longitude eventually change. What am I missing?



      EDIT:



      This question is the same topic as:
      MKMapView setRegion "snaps" to predefined zoom levels?
      MKMapView show incorrectly saved region



      As an experiment, I added a class variable of type MKCoordinateRegion and assigned the region read in the decodeRestorableState method to it. Also I added a button and action handler to the view controller which set the mapView's region to the value of the added variable. When tapping the button, the map zoomed into the position that was expected from the restoration. It's odd to me that the location coordinates work incorrectly in the decodeRestorableState method but those same coordinates work correctly in the button's action handler.



      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)
      }
      }









      share|improve this question
















      When the state is restored for a mapView, it's slightly zoomed out from where it was during the save. I created an iOS Single View App containing only a MKMapView filling the whole view to try and troubleshoot this. My debug print statements show that the map values are being saved and restored properly but the span latitude or longitude eventually change. What am I missing?



      EDIT:



      This question is the same topic as:
      MKMapView setRegion "snaps" to predefined zoom levels?
      MKMapView show incorrectly saved region



      As an experiment, I added a class variable of type MKCoordinateRegion and assigned the region read in the decodeRestorableState method to it. Also I added a button and action handler to the view controller which set the mapView's region to the value of the added variable. When tapping the button, the map zoomed into the position that was expected from the restoration. It's odd to me that the location coordinates work incorrectly in the decodeRestorableState method but those same coordinates work correctly in the button's action handler.



      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)
      }
      }






      ios swift mkmapview zoom region






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 18 '18 at 2:36







      user2129643

















      asked Nov 13 '18 at 20:35









      user2129643user2129643

      517




      517
























          0






          active

          oldest

          votes











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53289090%2fstate-restoration-of-mkmapview-zooms-out-slightly%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          0






          active

          oldest

          votes








          0






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes
















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53289090%2fstate-restoration-of-mkmapview-zooms-out-slightly%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Guess what letter conforming each word

          Port of Spain

          Run scheduled task as local user group (not BUILTIN)