-1

In my Storyboard in Swift app I have a UIViewController with MKMapView stretched to each edge of the screen. It was enough for me to import a MapKit, then I set up the delegate:

class MyClass: MKMapViewDelegate {

    @IBOutlet weak var mapView: MKMapView!

    override func viewDidLoad(){
        super.viewDidLoad()

        mapView.delegate = self
    }
}

and then, when I ran the app, I saw the map nicely centered above my country.

I want to implement a feature, that works like this:

when user opens this view, he sees the map, but centered on a place next to my home country, then the map could scroll automatically to center to my home country. Is that even achievable?

For example, if I lived in the US and opened the map, I would see the Pacific Ocean and then the map would scroll to show me the US territory.

user3766930
  • 5,629
  • 10
  • 51
  • 104
  • You can configure the map however you like. You can show any spot at any level of zoom. Or you can show the spot where the user is (if the user grants permission). – matt Oct 16 '16 at 00:58
  • Ok, but is there a possibility of somehow calculating the spot next to my country and then scroll from there to the original position? – user3766930 Oct 16 '16 at 01:15
  • Certainly. You can show any latitude/longitude you like, and you can scroll to anywhere else you like. – matt Oct 16 '16 at 01:20

1 Answers1

1

Of course it's possible. MKMapView has the method setRegion(_:animated:).

You could set the map region to a spot in the pacific, wait a few seconds (using a timer or dispatch_after) and then call setRegion(_:animated:) to move the map to be centered over your current location.

Duncan C
  • 128,072
  • 22
  • 173
  • 272
  • Thanks Duncan, just two additional questions - set region will of course work, but it will scroll to my location really fast - is there a way of - instead of delaying it with dispatch_after - coding it so the scrolling itself is smooth and slow? I mean that if the first region is pacific ocean then it would scroll to the US territory in e.g. 3-5 seconds, slowly showing everything else on the way? – user3766930 Oct 16 '16 at 09:41
  • Not that I know of. Perhaps a series of setRegion calls with delays between. That would be fragile, though, because if there is a delay loading the map contents it wouldn't work right. – Duncan C Oct 16 '16 at 11:37