ja. ja. - 1 month ago 19
iOS Question

MKCoordinateRegionMakeWithDistance displaying wrong coordinates

I'm trying to set approx a 10 mile range around the current location of the user when my mapView loads.
Here is my Objective C code in viewDidLoad:

self.mapView = [[MKMapView alloc] initWithFrame:self.view.frame];
self.mapView.delegate = self;
self.mapView.showsUserLocation = YES;
[self.view addSubview:self.mapView];


//Set initial scope of map
CLLocationDistance mapWidith = 16000;
CLLocationDistance mapHeight = 16000;
CLLocationCoordinate2D userLocation = self.mapView.userLocation.coordinate;

MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(userLocation, mapHeight , mapWidith);
[self.mapView setRegion: region
animated:false];


When I run it's starting with the correct zoom (I think) but randomly centered in the atlantic ocean somewhere. Not sure if I'm accessing the userLocation coordinate correctly but it seems right. Im pretty new to MKMapView so Im struggling a bit.

Answer

The userLocation property on mapView isn't populated yet. You should implement the mapView:didUpdateUserLocation: method and after that point you can reliably use that property (or use the location sent to you).

Comments