MKMapView setRegion not working

I am trying to set a custom launch area in an iOS application using MKMapView setRegion. The application works fine and a map appears, but no matter what I try, I can’t change the region. I tried a lot of tutorials and solutions, but no one works. Here is my code:

-(void)viewDidAppear:(BOOL)animated{
    MKCoordinateRegion region;
    MKCoordinateSpan span;
    span.latitudeDelta  = 0.001;
    span.longitudeDelta = 0.001;

    region.span = span;
    region.center.latitude = 100;
    region.center.longitude = 100;

    [mapView setRegion:(region) animated:(TRUE)];
}

I have MKMapView and Core Location structures that were added to projects correctly, I import MapKit.h and declare mapView, so I don’t know why it doesn’t work.

Thank,

Jacob

+4
source share
2 answers

It looks like the IBOutlet map view is not associated with the map display control in xib / storyboard.

, mapView nil, .

xib/ View Controller mapView .



, , delegate . , - . , .


:
center 100, 100. , . , center " ". -90 +90 ( -180 +180).

, , , (26, 80), . , , .

+3

, , , , , setRegion . , MKMapView , :

override func viewDidLoad() {
  ...
  self.mapView = MKMapView() // Wrong!
  ...
  self.mapView.setRegion(...) // Does not work!
}

, (CGRect). :

override func viewDidLoad() {
  ...
  self.mapView = MKMapView(frame: CGRect(0, 108, self.mapView.bounds.width, self.mapView.bounds.height))
  self.mapView.setRegion(...) // Oh, it works!
}

.

0

Source: https://habr.com/ru/post/1534179/


All Articles