2

How to reproduce the error:

let mapView = MKMapView.init(frame: UIScreen.mainScreen().bounds)
mapView.region.center = CLLocationCoordinate2D.init(latitude: 60, longitude: 100)
mapView.region.span = MKCoordinateSpanMake(20, 20)

print(mapView.region.center)

self.view = mapView

And the print statement prints this:

CLLocationCoordinate2D(latitude: 44.880507991448255, longitude: 100.00000000000004)

The problem is that I actually set the latitude to 60 at line 2. However the resulted latitude is 44.88x. And I have tried other values above 45, and they are not correct also. Any ideas? Thanks!

Huang C.
  • 398
  • 6
  • 17
  • http://stackoverflow.com/questions/15965166/what-is-the-maximum-length-of-latitude-and-longitude I suspect is has something to do with the map not being able to make a valid square with that high of a range. Try with a smaller span or a lower center. – Oscar Apeland Jun 09 '16 at 12:27
  • 1
    Have you tried `let region = MKCoordinateRegionMake(CLLocationCoordinate2DMake(60.0,100.0), MKCoordinateSpanMake(20, 20)) mapView.region = region` ? – Paulw11 Jun 09 '16 at 12:46
  • @Paulw11 OMG it works! Could you give some explanations (or even guess is fine) about why this works but my previous separate statements doesn't work? And would you mind putting your comment as an answer so I can accept it? Thanks. – Huang C. Jun 10 '16 at 03:15
  • @OscarApeland Actually Paulw11's code works. And I really need such region as I am using both GoogleMap and MapKit (kind of weird but...), and the coordinates I get from GoogleMap can certainly reach above 45. Thanks for your comment also! – Huang C. Jun 10 '16 at 03:16
  • When I tried to put your code into a test app I got an error that `region.center` is not assignable. My test app was Objective C, so it seems to be an issue with Swift not reporting the problem. – Paulw11 Jun 10 '16 at 04:23

1 Answers1

2

This seems to be an issue with Swift. If you try this code in Objective-C

mapView.region.center = CLLocationCoordinate2D.init(latitude: 60, longitude: 100)

The compiler gives an error expression is not assignable. The correct approach is to create a new region and then assign this region to the map view:

let region = MKCoordinateRegionMake(CLLocationCoordinate2DMake(60.0,100.0), MKCoordinateSpanMake(20, 20))
mapView.region = region
Paulw11
  • 108,386
  • 14
  • 159
  • 186